aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/zmalloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/src/zmalloc.h')
-rw-r--r--examples/redis-unstable/src/zmalloc.h168
1 files changed, 0 insertions, 168 deletions
diff --git a/examples/redis-unstable/src/zmalloc.h b/examples/redis-unstable/src/zmalloc.h
deleted file mode 100644
index 3dda503..0000000
--- a/examples/redis-unstable/src/zmalloc.h
+++ /dev/null
@@ -1,168 +0,0 @@
1/* zmalloc - total amount of allocated memory aware version of malloc()
2 *
3 * Copyright (c) 2009-Present, Redis Ltd.
4 * All rights reserved.
5 *
6 * Licensed under your choice of (a) the Redis Source Available License 2.0
7 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
8 * GNU Affero General Public License v3 (AGPLv3).
9 */
10
11#ifndef __ZMALLOC_H
12#define __ZMALLOC_H
13
14/* Double expansion needed for stringification of macro values. */
15#define __xstr(s) __str(s)
16#define __str(s) #s
17
18#if defined(USE_TCMALLOC)
19#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
20#include <google/tcmalloc.h>
21#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)
22#define HAVE_MALLOC_SIZE 1
23#define zmalloc_size(p) tc_malloc_size(p)
24#else
25#error "Newer version of tcmalloc required"
26#endif
27
28#elif defined(USE_JEMALLOC)
29#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX))
30#include <jemalloc/jemalloc.h>
31#if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
32#define HAVE_MALLOC_SIZE 1
33#define zmalloc_size(p) je_malloc_usable_size(p)
34#else
35#error "Newer version of jemalloc required"
36#endif
37
38#elif defined(__APPLE__)
39#include <malloc/malloc.h>
40#define HAVE_MALLOC_SIZE 1
41#define zmalloc_size(p) malloc_size(p)
42#endif
43
44/* On native libc implementations, we should still do our best to provide a
45 * HAVE_MALLOC_SIZE capability. This can be set explicitly as well:
46 *
47 * NO_MALLOC_USABLE_SIZE disables it on all platforms, even if they are
48 * known to support it.
49 * USE_MALLOC_USABLE_SIZE forces use of malloc_usable_size() regardless
50 * of platform.
51 */
52#ifndef ZMALLOC_LIB
53#define ZMALLOC_LIB "libc"
54
55#if !defined(NO_MALLOC_USABLE_SIZE) && \
56 (defined(__GLIBC__) || defined(__FreeBSD__) || \
57 defined(__DragonFly__) || defined(__HAIKU__) || \
58 defined(USE_MALLOC_USABLE_SIZE))
59
60/* Includes for malloc_usable_size() */
61#ifdef __FreeBSD__
62#include <malloc_np.h>
63#else
64#ifndef _GNU_SOURCE
65#define _GNU_SOURCE
66#endif
67#include <malloc.h>
68#endif
69
70#define HAVE_MALLOC_SIZE 1
71#define zmalloc_size(p) malloc_usable_size(p)
72
73#endif
74#endif
75
76/* We can enable the Redis defrag capabilities only if we are using Jemalloc
77 * and the version used is our special version modified for Redis having
78 * the ability to return per-allocation fragmentation hints. */
79#if (defined(USE_JEMALLOC) && defined(JEMALLOC_FRAG_HINT)) || defined(DEBUG_DEFRAG_FORCE)
80#define HAVE_DEFRAG
81#endif
82
83/* We can enable allocation with usable size capabilities only if we are using Jemalloc
84 * and the version used is our special version modified for Redis having
85 * the ability to return usable size during allocation or deallocation. */
86#if defined(USE_JEMALLOC) && defined(JEMALLOC_ALLOC_WITH_USIZE)
87#define HAVE_ALLOC_WITH_USIZE
88#endif
89
90#include <time.h>
91
92/* 'noinline' attribute is intended to prevent the `-Wstringop-overread` warning
93 * when using gcc-12 later with LTO enabled. It may be removed once the
94 * bug[https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503] is fixed. */
95__attribute__((malloc,alloc_size(1),noinline)) void *zmalloc(size_t size);
96__attribute__((malloc,alloc_size(1),noinline)) void *zcalloc(size_t size);
97__attribute__((malloc,alloc_size(1,2),noinline)) void *zcalloc_num(size_t num, size_t size);
98__attribute__((alloc_size(2),noinline)) void *zrealloc(void *ptr, size_t size);
99__attribute__((malloc,alloc_size(1),noinline)) void *ztrymalloc(size_t size);
100__attribute__((malloc,alloc_size(1),noinline)) void *ztrycalloc(size_t size);
101__attribute__((alloc_size(2),noinline)) void *ztryrealloc(void *ptr, size_t size);
102void zfree(void *ptr);
103void *zmalloc_usable(size_t size, size_t *usable);
104void *zcalloc_usable(size_t size, size_t *usable);
105void *zrealloc_usable(void *ptr, size_t size, size_t *usable, size_t *old_usable);
106void *ztrymalloc_usable(size_t size, size_t *usable);
107void *ztrycalloc_usable(size_t size, size_t *usable);
108void *ztryrealloc_usable(void *ptr, size_t size, size_t *usable, size_t *old_usable);
109void zfree_usable(void *ptr, size_t *usable);
110__attribute__((malloc)) char *zstrdup(const char *s);
111__attribute__((malloc)) char *zstrdup_usable(const char *s, size_t *usable);
112size_t zmalloc_used_memory(void);
113size_t zmalloc_get_peak_memory(void);
114time_t zmalloc_get_peak_memory_time(void);
115void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
116size_t zmalloc_get_rss(void);
117int zmalloc_get_allocator_info(int refresh_stats, size_t *allocated, size_t *active, size_t *resident,
118 size_t *retained, size_t *muzzy, size_t *frag_smallbins_bytes);
119int zmalloc_get_allocator_info_by_arena(unsigned int arena, int refresh_stats, size_t *allocated,
120 size_t *active, size_t *resident, size_t *frag_smallbins_bytes);
121void set_jemalloc_bg_thread(int enable);
122int jemalloc_purge(void);
123size_t zmalloc_get_private_dirty(long pid);
124size_t zmalloc_get_smap_bytes_by_field(char *field, long pid);
125size_t zmalloc_get_memory_size(void);
126void zlibc_free(void *ptr);
127void zmadvise_dontneed(void *ptr);
128
129#if defined(USE_JEMALLOC)
130void *zmalloc_with_flags(size_t size, int flags);
131void *zrealloc_with_flags(void *ptr, size_t size, int flags);
132void zfree_with_flags(void *ptr, int flags);
133#endif
134
135#if (defined(USE_JEMALLOC) && defined(HAVE_DEFRAG))
136void zfree_no_tcache(void *ptr);
137__attribute__((malloc)) void *zmalloc_no_tcache(size_t size);
138#endif
139
140#ifndef HAVE_MALLOC_SIZE
141size_t zmalloc_size(void *ptr);
142size_t zmalloc_usable_size(void *ptr);
143#else
144/* If we use 'zmalloc_usable_size()' to obtain additional available memory size
145 * and manipulate it, we need to call 'extend_to_usable()' afterwards to ensure
146 * the compiler recognizes this extra memory. However, if we use the pointer
147 * obtained from z[*]_usable() family functions, there is no need for this step. */
148#define zmalloc_usable_size(p) zmalloc_size(p)
149
150/* derived from https://github.com/systemd/systemd/pull/25688
151 * We use zmalloc_usable_size() everywhere to use memory blocks, but that is an abuse since the
152 * malloc_usable_size() isn't meant for this kind of use, it is for diagnostics only. That is also why the
153 * behavior is flaky when built with _FORTIFY_SOURCE, the compiler can sense that we reach outside
154 * the allocated block and SIGABRT.
155 * We use a dummy allocator function to tell the compiler that the new size of ptr is newsize.
156 * The implementation returns the pointer as is; the only reason for its existence is as a conduit for the
157 * alloc_size attribute. This cannot be a static inline because gcc then loses the attributes on the function.
158 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96503 */
159__attribute__((alloc_size(2),noinline)) void *extend_to_usable(void *ptr, size_t size);
160#endif
161
162int get_proc_stat_ll(int i, long long *res);
163
164#ifdef REDIS_TEST
165int zmalloc_test(int argc, char **argv, int flags);
166#endif
167
168#endif /* __ZMALLOC_H */