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