forked from redis/redis
-
Notifications
You must be signed in to change notification settings - Fork 4
Collapse file tree
Files
Search this repository
/
Copy pathzmalloc.c
More file actions
More file actions
Latest commit
429 lines (381 loc) · 12.7 KB
/
zmalloc.c
File metadata and controls
429 lines (381 loc) · 12.7 KB
Edit and raw actions
OlderNewer
1
/* zmalloc - total amount of allocated memory aware version of malloc()
2
*
3
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4
* All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are met:
8
*
9
* * Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* * Neither the name of Redis nor the names of its contributors may be used
15
* to endorse or promote products derived from this software without
16
* specific prior written permission.
17
*
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
* POSSIBILITY OF SUCH DAMAGE.
29
*/
30
31
#include <stdio.h>
32
#include <stdlib.h>
33
34
/* This function provide us access to the original libc free(). This is useful
35
* for instance to free results obtained by backtrace_symbols(). We need
36
* to define this function before including zmalloc.h that may shadow the
37
* free implementation if we use jemalloc or another non standard allocator. */
38
void zlibc_free(void *ptr) {
39
free(ptr);
40
}
41
42
#include <string.h>
43
#include <pthread.h>
44
#include "config.h"
45
#include "zmalloc.h"
46
#include "atomicvar.h"
47
48
#ifdef HAVE_MALLOC_SIZE
49
#define PREFIX_SIZE (0)
50
#else
51
#if defined(__sun) || defined(__sparc) || defined(__sparc__)
52
#define PREFIX_SIZE (sizeof(long long))
53
#else
54
#define PREFIX_SIZE (sizeof(size_t))
55
#endif
56
#endif
57
58
/* Explicitly override malloc/free etc when using tcmalloc. */
59
#if defined(USE_TCMALLOC)
60
#define malloc(size) tc_malloc(size)
61
#define calloc(count,size) tc_calloc(count,size)
62
#define realloc(ptr,size) tc_realloc(ptr,size)
63
#define free(ptr) tc_free(ptr)
64
#elif defined(USE_JEMALLOC)
65
#define malloc(size) je_malloc(size)
66
#define calloc(count,size) je_calloc(count,size)
67
#define realloc(ptr,size) je_realloc(ptr,size)
68
#define free(ptr) je_free(ptr)
69
#define mallocx(size,flags) je_mallocx(size,flags)
70
#define dallocx(ptr,flags) je_dallocx(ptr,flags)
71
#endif
72
73
#define update_zmalloc_stat_alloc(__n) do { \
74
size_t _n = (__n); \
75
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
76
if (zmalloc_thread_safe) { \
77
atomicIncr(used_memory,__n); \
78
} else { \
79
used_memory += _n; \
80
} \
81
} while(0)
82
83
#define update_zmalloc_stat_free(__n) do { \
84
size_t _n = (__n); \
85
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
86
if (zmalloc_thread_safe) { \
87
atomicDecr(used_memory,__n); \
88
} else { \
89
used_memory -= _n; \
90
} \
91
} while(0)
92
93
static size_t used_memory = 0;
94
static int zmalloc_thread_safe = 0;
95
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
96
97
static void zmalloc_default_oom(size_t size) {
98
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
99
size);
100
fflush(stderr);
101
abort();