Skip to content

Files

Latest commit

2a51bac · May 4, 2017

History

History
429 lines (381 loc) · 12.7 KB

zmalloc.c

File metadata and controls

429 lines (381 loc) · 12.7 KB
antirezmattstabaishaofeipieternoranagraguiquanzlambyanydotd0k
Mar 22, 2009
Mar 22, 2009
1
/* zmalloc - total amount of allocated memory aware version of malloc()
2
*
Feb 19, 2010
Feb 19, 2010
3
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
Mar 22, 2009
Mar 22, 2009
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
Oct 23, 2009
Oct 23, 2009
31
#include <stdio.h>
Mar 22, 2009
Mar 22, 2009
32
#include <stdlib.h>
Mar 27, 2012
Mar 27, 2012
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
Mar 22, 2009
Mar 22, 2009
42
#include <string.h>
Jan 11, 2010
Jan 11, 2010
43
#include <pthread.h>
Jun 5, 2009
Jun 5, 2009
44
#include "config.h"
Jan 9, 2011
Jan 9, 2011
45
#include "zmalloc.h"
Oct 1, 2015
Oct 1, 2015
46
#include "atomicvar.h"
Jun 4, 2009
Jun 4, 2009
47
Oct 23, 2010
Oct 23, 2010
48
#ifdef HAVE_MALLOC_SIZE
49
#define PREFIX_SIZE (0)
50
#else
Nov 16, 2011
Nov 16, 2011
51
#if defined(__sun) || defined(__sparc) || defined(__sparc__)
Oct 23, 2010
Oct 23, 2010
52
#define PREFIX_SIZE (sizeof(long long))
Oct 27, 2009
Oct 27, 2009
53
#else
Oct 23, 2010
Oct 23, 2010
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)
Jun 20, 2011
Jun 20, 2011
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)
Dec 30, 2016
Dec 30, 2016
69
#define mallocx(size,flags) je_mallocx(size,flags)
70
#define dallocx(ptr,flags) je_dallocx(ptr,flags)
Oct 27, 2009
Oct 27, 2009
71
#endif
72
Nov 14, 2012
Nov 14, 2012
73
#define update_zmalloc_stat_alloc(__n) do { \
Apr 20, 2010
Apr 20, 2010
74
size_t _n = (__n); \
75
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
Jan 15, 2010
Jan 15, 2010
76
if (zmalloc_thread_safe) { \
May 4, 2017
May 4, 2017
77
atomicIncr(used_memory,__n); \
Jan 15, 2010
Jan 15, 2010
78
} else { \
79
used_memory += _n; \
80
} \
81
} while(0)
82
Jan 9, 2011
Jan 9, 2011
83
#define update_zmalloc_stat_free(__n) do { \
Apr 20, 2010
Apr 20, 2010
84
size_t _n = (__n); \
85
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
Jan 15, 2010
Jan 15, 2010
86
if (zmalloc_thread_safe) { \
May 4, 2017
May 4, 2017
87
atomicDecr(used_memory,__n); \
Jan 15, 2010
Jan 15, 2010
88
} else { \
89
used_memory -= _n; \
90
} \
91
} while(0)
92
Mar 22, 2009
Mar 22, 2009
93
static size_t used_memory = 0;
Jan 15, 2010
Jan 15, 2010
94
static int zmalloc_thread_safe = 0;
Jan 11, 2010
Jan 11, 2010
95
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
Mar 22, 2009
Mar 22, 2009
96
Aug 24, 2012
Aug 24, 2012
97
static void zmalloc_default_oom(size_t size) {
Oct 31, 2009
Oct 31, 2009
98
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
Oct 23, 2009
Oct 23, 2009
99
size);
100
fflush(stderr);
101
abort();