1#ifndef JEMALLOC_H_
2#define JEMALLOC_H_
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/* Defined if __attribute__((...)) syntax is supported. */
8#define JEMALLOC_HAVE_ATTR
9
10/* Defined if alloc_size attribute is supported. */
11#define JEMALLOC_HAVE_ATTR_ALLOC_SIZE
12
13/* Defined if format_arg(...) attribute is supported. */
14#define JEMALLOC_HAVE_ATTR_FORMAT_ARG
15
16/* Defined if format(gnu_printf, ...) attribute is supported. */
17#define JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
18
19/* Defined if format(printf, ...) attribute is supported. */
20#define JEMALLOC_HAVE_ATTR_FORMAT_PRINTF
21
22/*
23 * Define overrides for non-standard allocator-related functions if they are
24 * present on the system.
25 */
26#define JEMALLOC_OVERRIDE_MEMALIGN
27#define JEMALLOC_OVERRIDE_VALLOC
28
29/*
30 * At least Linux omits the "const" in:
31 *
32 * size_t malloc_usable_size(const void *ptr);
33 *
34 * Match the operating system's prototype.
35 */
36#define JEMALLOC_USABLE_SIZE_CONST
37
38/*
39 * If defined, specify throw() for the public function prototypes when compiling
40 * with C++. The only justification for this is to match the prototypes that
41 * glibc defines.
42 */
43#define JEMALLOC_USE_CXX_THROW
44
45#ifdef _MSC_VER
46# ifdef _WIN64
47# define LG_SIZEOF_PTR_WIN 3
48# else
49# define LG_SIZEOF_PTR_WIN 2
50# endif
51#endif
52
53/* sizeof(void *) == 2^LG_SIZEOF_PTR. */
54#define LG_SIZEOF_PTR 3
55
56/*
57 * Name mangling for public symbols is controlled by --with-mangling and
58 * --with-jemalloc-prefix. With default settings the je_ prefix is stripped by
59 * these macro definitions.
60 */
61#ifndef JEMALLOC_NO_RENAME
62# define je_aligned_alloc je_aligned_alloc
63# define je_calloc je_calloc
64# define je_dallocx je_dallocx
65# define je_free je_free
66# define je_mallctl je_mallctl
67# define je_mallctlbymib je_mallctlbymib
68# define je_mallctlnametomib je_mallctlnametomib
69# define je_malloc je_malloc
70# define je_malloc_conf je_malloc_conf
71# define je_malloc_message je_malloc_message
72# define je_malloc_stats_print je_malloc_stats_print
73# define je_malloc_usable_size je_malloc_usable_size
74# define je_mallocx je_mallocx
75# define je_smallocx_0 je_smallocx_0
76# define je_nallocx je_nallocx
77# define je_posix_memalign je_posix_memalign
78# define je_rallocx je_rallocx
79# define je_realloc je_realloc
80# define je_sallocx je_sallocx
81# define je_sdallocx je_sdallocx
82# define je_xallocx je_xallocx
83# define je_memalign je_memalign
84# define je_valloc je_valloc
85#endif
86
87#include <stdlib.h>
88#include <stdbool.h>
89#include <stdint.h>
90#include <limits.h>
91#include <strings.h>
92
93#define JEMALLOC_VERSION "5.2.1-0-g0"
94#define JEMALLOC_VERSION_MAJOR 5
95#define JEMALLOC_VERSION_MINOR 2
96#define JEMALLOC_VERSION_BUGFIX 1
97#define JEMALLOC_VERSION_NREV 0
98#define JEMALLOC_VERSION_GID "0"
99#define JEMALLOC_VERSION_GID_IDENT 0
100
101#define MALLOCX_LG_ALIGN(la) ((int)(la))
102#if LG_SIZEOF_PTR == 2
103# define MALLOCX_ALIGN(a) ((int)(ffs((int)(a))-1))
104#else
105# define MALLOCX_ALIGN(a) \
106 ((int)(((size_t)(a) < (size_t)INT_MAX) ? ffs((int)(a))-1 : \
107 ffs((int)(((size_t)(a))>>32))+31))
108#endif
109#define MALLOCX_ZERO ((int)0x40)
110/*
111 * Bias tcache index bits so that 0 encodes "automatic tcache management", and 1
112 * encodes MALLOCX_TCACHE_NONE.
113 */
114#define MALLOCX_TCACHE(tc) ((int)(((tc)+2) << 8))
115#define MALLOCX_TCACHE_NONE MALLOCX_TCACHE(-1)
116/*
117 * Bias arena index bits so that 0 encodes "use an automatically chosen arena".
118 */
119#define MALLOCX_ARENA(a) ((((int)(a))+1) << 20)
120
121/*
122 * Use as arena index in "arena.<i>.{purge,decay,dss}" and
123 * "stats.arenas.<i>.*" mallctl interfaces to select all arenas. This
124 * definition is intentionally specified in raw decimal format to support
125 * cpp-based string concatenation, e.g.
126 *
127 * #define STRINGIFY_HELPER(x) #x
128 * #define STRINGIFY(x) STRINGIFY_HELPER(x)
129 *
130 * mallctl("arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".purge", NULL, NULL, NULL,
131 * 0);
132 */
133#define MALLCTL_ARENAS_ALL 4096
134/*
135 * Use as arena index in "stats.arenas.<i>.*" mallctl interfaces to select
136 * destroyed arenas.
137 */
138#define MALLCTL_ARENAS_DESTROYED 4097
139
140#if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)
141# define JEMALLOC_CXX_THROW throw()
142#else
143# define JEMALLOC_CXX_THROW
144#endif
145
146#if defined(_MSC_VER)
147# define JEMALLOC_ATTR(s)
148# define JEMALLOC_ALIGNED(s) __declspec(align(s))
149# define JEMALLOC_ALLOC_SIZE(s)
150# define JEMALLOC_ALLOC_SIZE2(s1, s2)
151# ifndef JEMALLOC_EXPORT
152# ifdef DLLEXPORT
153# define JEMALLOC_EXPORT __declspec(dllexport)
154# else
155# define JEMALLOC_EXPORT __declspec(dllimport)
156# endif
157# endif
158# define JEMALLOC_FORMAT_ARG(i)
159# define JEMALLOC_FORMAT_PRINTF(s, i)
160# define JEMALLOC_NOINLINE __declspec(noinline)
161# ifdef __cplusplus
162# define JEMALLOC_NOTHROW __declspec(nothrow)
163# else
164# define JEMALLOC_NOTHROW
165# endif
166# define JEMALLOC_SECTION(s) __declspec(allocate(s))
167# define JEMALLOC_RESTRICT_RETURN __declspec(restrict)
168# if _MSC_VER >= 1900 && !defined(__EDG__)
169# define JEMALLOC_ALLOCATOR __declspec(allocator)
170# else
171# define JEMALLOC_ALLOCATOR
172# endif
173#elif defined(JEMALLOC_HAVE_ATTR)
174# define JEMALLOC_ATTR(s) __attribute__((s))
175# define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))
176# ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE
177# define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
178# define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))
179# else
180# define JEMALLOC_ALLOC_SIZE(s)
181# define JEMALLOC_ALLOC_SIZE2(s1, s2)
182# endif
183# ifndef JEMALLOC_EXPORT
184# define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
185# endif
186# ifdef JEMALLOC_HAVE_ATTR_FORMAT_ARG
187# define JEMALLOC_FORMAT_ARG(i) JEMALLOC_ATTR(__format_arg__(3))
188# else
189# define JEMALLOC_FORMAT_ARG(i)
190# endif
191# ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
192# define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))
193# elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)
194# define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))
195# else
196# define JEMALLOC_FORMAT_PRINTF(s, i)
197# endif
198# define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
199# define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
200# define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
201# define JEMALLOC_RESTRICT_RETURN
202# define JEMALLOC_ALLOCATOR
203#else
204# define JEMALLOC_ATTR(s)
205# define JEMALLOC_ALIGNED(s)
206# define JEMALLOC_ALLOC_SIZE(s)
207# define JEMALLOC_ALLOC_SIZE2(s1, s2)
208# define JEMALLOC_EXPORT
209# define JEMALLOC_FORMAT_PRINTF(s, i)
210# define JEMALLOC_NOINLINE
211# define JEMALLOC_NOTHROW
212# define JEMALLOC_SECTION(s)
213# define JEMALLOC_RESTRICT_RETURN
214# define JEMALLOC_ALLOCATOR
215#endif
216
217/* This version of Jemalloc, modified for Redis, has the je_get_defrag_hint()
218 * function. */
219#define JEMALLOC_FRAG_HINT
220
221/*
222 * The je_ prefix on the following public symbol declarations is an artifact
223 * of namespace management, and should be omitted in application code unless
224 * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle.h).
225 */
226extern JEMALLOC_EXPORT const char *je_malloc_conf;
227extern JEMALLOC_EXPORT void (*je_malloc_message)(void *cbopaque,
228 const char *s);
229
230JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
231 void JEMALLOC_NOTHROW *je_malloc(size_t size)
232 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
233JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
234 void JEMALLOC_NOTHROW *je_calloc(size_t num, size_t size)
235 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);
236JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_posix_memalign(void **memptr,
237 size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));
238JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
239 void JEMALLOC_NOTHROW *je_aligned_alloc(size_t alignment,
240 size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)
241 JEMALLOC_ALLOC_SIZE(2);
242JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
243 void JEMALLOC_NOTHROW *je_realloc(void *ptr, size_t size)
244 JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);
245JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_free(void *ptr)
246 JEMALLOC_CXX_THROW;
247
248JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
249 void JEMALLOC_NOTHROW *je_mallocx(size_t size, int flags)
250 JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
251JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
252 void JEMALLOC_NOTHROW *je_rallocx(void *ptr, size_t size,
253 int flags) JEMALLOC_ALLOC_SIZE(2);
254JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_xallocx(void *ptr, size_t size,
255 size_t extra, int flags);
256JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_sallocx(const void *ptr,
257 int flags) JEMALLOC_ATTR(pure);
258JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_dallocx(void *ptr, int flags);
259JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_sdallocx(void *ptr, size_t size,
260 int flags);
261JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_nallocx(size_t size, int flags)
262 JEMALLOC_ATTR(pure);
263
264JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctl(const char *name,
265 void *oldp, size_t *oldlenp, void *newp, size_t newlen);
266JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctlnametomib(const char *name,
267 size_t *mibp, size_t *miblenp);
268JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctlbymib(const size_t *mib,
269 size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
270JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_malloc_stats_print(
271 void (*write_cb)(void *, const char *), void *je_cbopaque,
272 const char *opts);
273JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_malloc_usable_size(
274 JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
275
276#ifdef JEMALLOC_OVERRIDE_MEMALIGN
277JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
278 void JEMALLOC_NOTHROW *je_memalign(size_t alignment, size_t size)
279 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);
280#endif
281
282#ifdef JEMALLOC_OVERRIDE_VALLOC
283JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
284 void JEMALLOC_NOTHROW *je_valloc(size_t size) JEMALLOC_CXX_THROW
285 JEMALLOC_ATTR(malloc);
286#endif
287
288typedef struct extent_hooks_s extent_hooks_t;
289
290/*
291 * void *
292 * extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
293 * size_t alignment, bool *zero, bool *commit, unsigned arena_ind);
294 */
295typedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t, bool *,
296 bool *, unsigned);
297
298/*
299 * bool
300 * extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,
301 * bool committed, unsigned arena_ind);
302 */
303typedef bool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, bool,
304 unsigned);
305
306/*
307 * void
308 * extent_destroy(extent_hooks_t *extent_hooks, void *addr, size_t size,
309 * bool committed, unsigned arena_ind);
310 */
311typedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, bool,
312 unsigned);
313
314/*
315 * bool
316 * extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,
317 * size_t offset, size_t length, unsigned arena_ind);
318 */
319typedef bool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
320 unsigned);
321
322/*
323 * bool
324 * extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,
325 * size_t offset, size_t length, unsigned arena_ind);
326 */
327typedef bool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,
328 size_t, unsigned);
329
330/*
331 * bool
332 * extent_purge(extent_hooks_t *extent_hooks, void *addr, size_t size,
333 * size_t offset, size_t length, unsigned arena_ind);
334 */
335typedef bool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
336 unsigned);
337
338/*
339 * bool
340 * extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,
341 * size_t size_a, size_t size_b, bool committed, unsigned arena_ind);
342 */
343typedef bool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
344 bool, unsigned);
345
346/*
347 * bool
348 * extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
349 * void *addr_b, size_t size_b, bool committed, unsigned arena_ind);
350 */
351typedef bool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,
352 bool, unsigned);
353
354struct extent_hooks_s {
355 extent_alloc_t *alloc;
356 extent_dalloc_t *dalloc;
357 extent_destroy_t *destroy;
358 extent_commit_t *commit;
359 extent_decommit_t *decommit;
360 extent_purge_t *purge_lazy;
361 extent_purge_t *purge_forced;
362 extent_split_t *split;
363 extent_merge_t *merge;
364};
365
366/*
367 * By default application code must explicitly refer to mangled symbol names,
368 * so that it is possible to use jemalloc in conjunction with another allocator
369 * in the same application. Define JEMALLOC_MANGLE in order to cause automatic
370 * name mangling that matches the API prefixing that happened as a result of
371 * --with-mangling and/or --with-jemalloc-prefix configuration settings.
372 */
373#ifdef JEMALLOC_MANGLE
374# ifndef JEMALLOC_NO_DEMANGLE
375# define JEMALLOC_NO_DEMANGLE
376# endif
377# define aligned_alloc je_aligned_alloc
378# define calloc je_calloc
379# define dallocx je_dallocx
380# define free je_free
381# define mallctl je_mallctl
382# define mallctlbymib je_mallctlbymib
383# define mallctlnametomib je_mallctlnametomib
384# define malloc je_malloc
385# define malloc_conf je_malloc_conf
386# define malloc_message je_malloc_message
387# define malloc_stats_print je_malloc_stats_print
388# define malloc_usable_size je_malloc_usable_size
389# define mallocx je_mallocx
390# define smallocx_0 je_smallocx_0
391# define nallocx je_nallocx
392# define posix_memalign je_posix_memalign
393# define rallocx je_rallocx
394# define realloc je_realloc
395# define sallocx je_sallocx
396# define sdallocx je_sdallocx
397# define xallocx je_xallocx
398# define memalign je_memalign
399# define valloc je_valloc
400#endif
401
402/*
403 * The je_* macros can be used as stable alternative names for the
404 * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined. This is primarily
405 * meant for use in jemalloc itself, but it can be used by application code to
406 * provide isolation from the name mangling specified via --with-mangling
407 * and/or --with-jemalloc-prefix.
408 */
409#ifndef JEMALLOC_NO_DEMANGLE
410# undef je_aligned_alloc
411# undef je_calloc
412# undef je_dallocx
413# undef je_free
414# undef je_mallctl
415# undef je_mallctlbymib
416# undef je_mallctlnametomib
417# undef je_malloc
418# undef je_malloc_conf
419# undef je_malloc_message
420# undef je_malloc_stats_print
421# undef je_malloc_usable_size
422# undef je_mallocx
423# undef je_smallocx_0
424# undef je_nallocx
425# undef je_posix_memalign
426# undef je_rallocx
427# undef je_realloc
428# undef je_sallocx
429# undef je_sdallocx
430# undef je_xallocx
431# undef je_memalign
432# undef je_valloc
433#endif
434
435#ifdef __cplusplus
436}
437#endif
438#endif /* JEMALLOC_H_ */
439