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