xref: /freebsd/lib/libc/stdlib/malloc/jemalloc/include/jemalloc/jemalloc.h (revision c43cad87172039ccf38172129c79755ea79e6102)
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_arg(...) attribute is supported. */
14 #define JEMALLOC_HAVE_ATTR_FORMAT_ARG
15 
16 /* Defined if format(gnu_printf, ...) attribute is supported. */
17 /* #undef 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 /* #undef JEMALLOC_OVERRIDE_MEMALIGN */
33 #define JEMALLOC_OVERRIDE_VALLOC
34 
35 /*
36  * At least Linux omits the "const" in:
37  *
38  *   size_t malloc_usable_size(const void *ptr);
39  *
40  * Match the operating system's prototype.
41  */
42 #define JEMALLOC_USABLE_SIZE_CONST const
43 
44 /*
45  * If defined, specify throw() for the public function prototypes when compiling
46  * with C++.  The only justification for this is to match the prototypes that
47  * glibc defines.
48  */
49 /* #undef JEMALLOC_USE_CXX_THROW */
50 
51 #ifdef _MSC_VER
52 #  ifdef _WIN64
53 #    define LG_SIZEOF_PTR_WIN 3
54 #  else
55 #    define LG_SIZEOF_PTR_WIN 2
56 #  endif
57 #endif
58 
59 /* sizeof(void *) == 2^LG_SIZEOF_PTR. */
60 #define LG_SIZEOF_PTR 3
61 
62 /*
63  * Name mangling for public symbols is controlled by --with-mangling and
64  * --with-jemalloc-prefix.  With default settings the je_ prefix is stripped by
65  * these macro definitions.
66  */
67 #ifndef JEMALLOC_NO_RENAME
68 #  define je_aligned_alloc aligned_alloc
69 #  define je_calloc calloc
70 #  define je_dallocx dallocx
71 #  define je_free free
72 #  define je_mallctl mallctl
73 #  define je_mallctlbymib mallctlbymib
74 #  define je_mallctlnametomib mallctlnametomib
75 #  define je_malloc malloc
76 #  define je_malloc_conf malloc_conf
77 #  define je_malloc_conf_2_conf_harder malloc_conf_2_conf_harder
78 #  define je_malloc_message malloc_message
79 #  define je_malloc_stats_print malloc_stats_print
80 #  define je_malloc_usable_size malloc_usable_size
81 #  define je_mallocx mallocx
82 #  define je_smallocx_54eaed1d8b56b1aa528be3bdd1877e59c56fa90c smallocx_54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
83 #  define je_nallocx nallocx
84 #  define je_posix_memalign posix_memalign
85 #  define je_rallocx rallocx
86 #  define je_realloc realloc
87 #  define je_sallocx sallocx
88 #  define je_sdallocx sdallocx
89 #  define je_xallocx xallocx
90 #  define je_memalign memalign
91 #  define je_valloc valloc
92 #endif
93 
94 #include "jemalloc_FreeBSD.h"
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-0-g54eaed1d8b56b1aa528be3bdd1877e59c56fa90c"
103 #define JEMALLOC_VERSION_MAJOR 5
104 #define JEMALLOC_VERSION_MINOR 3
105 #define JEMALLOC_VERSION_BUGFIX 0
106 #define JEMALLOC_VERSION_NREV 0
107 #define JEMALLOC_VERSION_GID "54eaed1d8b56b1aa528be3bdd1877e59c56fa90c"
108 #define JEMALLOC_VERSION_GID_IDENT 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
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(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  */
251 extern JEMALLOC_EXPORT const char	*je_malloc_conf;
252 extern JEMALLOC_EXPORT void		(*je_malloc_message)(void *cbopaque,
253     const char *s);
254 
255 JEMALLOC_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);
258 JEMALLOC_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);
261 JEMALLOC_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));
264 JEMALLOC_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);
268 JEMALLOC_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);
271 JEMALLOC_EXPORT void JEMALLOC_SYS_NOTHROW	je_free(void *ptr)
272     JEMALLOC_CXX_THROW;
273 
274 JEMALLOC_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);
277 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
278     void JEMALLOC_NOTHROW	*je_rallocx(void *ptr, size_t size,
279     int flags) JEMALLOC_ALLOC_SIZE(2);
280 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_xallocx(void *ptr, size_t size,
281     size_t extra, int flags);
282 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_sallocx(const void *ptr,
283     int flags) JEMALLOC_ATTR(pure);
284 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_dallocx(void *ptr, int flags);
285 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_sdallocx(void *ptr, size_t size,
286     int flags);
287 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_nallocx(size_t size, int flags)
288     JEMALLOC_ATTR(pure);
289 
290 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctl(const char *name,
291     void *oldp, size_t *oldlenp, void *newp, size_t newlen);
292 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlnametomib(const char *name,
293     size_t *mibp, size_t *miblenp);
294 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlbymib(const size_t *mib,
295     size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
296 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_malloc_stats_print(
297     void (*write_cb)(void *, const char *), void *je_cbopaque,
298     const char *opts);
299 JEMALLOC_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
302 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_malloc_size(
303     const void *ptr);
304 #endif
305 
306 #ifdef JEMALLOC_OVERRIDE_MEMALIGN
307 JEMALLOC_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
313 JEMALLOC_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 typedef struct extent_hooks_s extent_hooks_t;
319 
320 /*
321  * void *
322  * extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
323  *     size_t alignment, bool *zero, bool *commit, unsigned arena_ind);
324  */
325 typedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t, bool *,
326     bool *, unsigned);
327 
328 /*
329  * bool
330  * extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,
331  *     bool committed, unsigned arena_ind);
332  */
333 typedef bool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, bool,
334     unsigned);
335 
336 /*
337  * void
338  * extent_destroy(extent_hooks_t *extent_hooks, void *addr, size_t size,
339  *     bool committed, unsigned arena_ind);
340  */
341 typedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, bool,
342     unsigned);
343 
344 /*
345  * bool
346  * extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,
347  *     size_t offset, size_t length, unsigned arena_ind);
348  */
349 typedef bool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
350     unsigned);
351 
352 /*
353  * bool
354  * extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,
355  *     size_t offset, size_t length, unsigned arena_ind);
356  */
357 typedef bool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,
358     size_t, unsigned);
359 
360 /*
361  * bool
362  * extent_purge(extent_hooks_t *extent_hooks, void *addr, size_t size,
363  *     size_t offset, size_t length, unsigned arena_ind);
364  */
365 typedef bool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
366     unsigned);
367 
368 /*
369  * bool
370  * extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,
371  *     size_t size_a, size_t size_b, bool committed, unsigned arena_ind);
372  */
373 typedef bool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
374     bool, unsigned);
375 
376 /*
377  * bool
378  * extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
379  *     void *addr_b, size_t size_b, bool committed, unsigned arena_ind);
380  */
381 typedef bool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,
382     bool, unsigned);
383 
384 struct extent_hooks_s {
385 	extent_alloc_t		*alloc;
386 	extent_dalloc_t		*dalloc;
387 	extent_destroy_t	*destroy;
388 	extent_commit_t		*commit;
389 	extent_decommit_t	*decommit;
390 	extent_purge_t		*purge_lazy;
391 	extent_purge_t		*purge_forced;
392 	extent_split_t		*split;
393 	extent_merge_t		*merge;
394 };
395 
396 /*
397  * By default application code must explicitly refer to mangled symbol names,
398  * so that it is possible to use jemalloc in conjunction with another allocator
399  * in the same application.  Define JEMALLOC_MANGLE in order to cause automatic
400  * name mangling that matches the API prefixing that happened as a result of
401  * --with-mangling and/or --with-jemalloc-prefix configuration settings.
402  */
403 #ifdef JEMALLOC_MANGLE
404 #  ifndef JEMALLOC_NO_DEMANGLE
405 #    define JEMALLOC_NO_DEMANGLE
406 #  endif
407 #  define aligned_alloc je_aligned_alloc
408 #  define calloc je_calloc
409 #  define dallocx je_dallocx
410 #  define free je_free
411 #  define mallctl je_mallctl
412 #  define mallctlbymib je_mallctlbymib
413 #  define mallctlnametomib je_mallctlnametomib
414 #  define malloc je_malloc
415 #  define malloc_conf je_malloc_conf
416 #  define malloc_conf_2_conf_harder je_malloc_conf_2_conf_harder
417 #  define malloc_message je_malloc_message
418 #  define malloc_stats_print je_malloc_stats_print
419 #  define malloc_usable_size je_malloc_usable_size
420 #  define mallocx je_mallocx
421 #  define smallocx_54eaed1d8b56b1aa528be3bdd1877e59c56fa90c je_smallocx_54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
422 #  define nallocx je_nallocx
423 #  define posix_memalign je_posix_memalign
424 #  define rallocx je_rallocx
425 #  define realloc je_realloc
426 #  define sallocx je_sallocx
427 #  define sdallocx je_sdallocx
428 #  define xallocx je_xallocx
429 #  define valloc je_valloc
430 #endif
431 
432 /*
433  * The je_* macros can be used as stable alternative names for the
434  * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined.  This is primarily
435  * meant for use in jemalloc itself, but it can be used by application code to
436  * provide isolation from the name mangling specified via --with-mangling
437  * and/or --with-jemalloc-prefix.
438  */
439 #ifndef JEMALLOC_NO_DEMANGLE
440 #  undef je_aligned_alloc
441 #  undef je_calloc
442 #  undef je_dallocx
443 #  undef je_free
444 #  undef je_mallctl
445 #  undef je_mallctlbymib
446 #  undef je_mallctlnametomib
447 #  undef je_malloc
448 #  undef je_malloc_conf
449 #  undef je_malloc_conf_2_conf_harder
450 #  undef je_malloc_message
451 #  undef je_malloc_stats_print
452 #  undef je_malloc_usable_size
453 #  undef je_mallocx
454 #  undef je_smallocx_54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
455 #  undef je_nallocx
456 #  undef je_posix_memalign
457 #  undef je_rallocx
458 #  undef je_realloc
459 #  undef je_sallocx
460 #  undef je_sdallocx
461 #  undef je_xallocx
462 #  undef je_valloc
463 #endif
464 
465 #ifdef __cplusplus
466 }
467 #endif
468 #endif /* JEMALLOC_H_ */
469