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