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 /* sizeof(void *) == 2^LG_SIZEOF_PTR. */
43 #define	LG_SIZEOF_PTR 3
44 
45 /*
46  * Name mangling for public symbols is controlled by --with-mangling and
47  * --with-jemalloc-prefix.  With default settings the je_ prefix is stripped by
48  * these macro definitions.
49  */
50 #ifndef JEMALLOC_NO_RENAME
51 #  define je_malloc_conf malloc_conf
52 #  define je_malloc_message malloc_message
53 #  define je_malloc malloc
54 #  define je_calloc calloc
55 #  define je_posix_memalign posix_memalign
56 #  define je_aligned_alloc aligned_alloc
57 #  define je_realloc realloc
58 #  define je_free free
59 #  define je_mallocx mallocx
60 #  define je_rallocx rallocx
61 #  define je_xallocx xallocx
62 #  define je_sallocx sallocx
63 #  define je_dallocx dallocx
64 #  define je_sdallocx sdallocx
65 #  define je_nallocx nallocx
66 #  define je_mallctl mallctl
67 #  define je_mallctlnametomib mallctlnametomib
68 #  define je_mallctlbymib mallctlbymib
69 #  define je_malloc_stats_print malloc_stats_print
70 #  define je_malloc_usable_size malloc_usable_size
71 #  define je_valloc valloc
72 #endif
73 
74 #include "jemalloc_FreeBSD.h"
75 
76 #include <stdlib.h>
77 #include <stdbool.h>
78 #include <stdint.h>
79 #include <limits.h>
80 #include <strings.h>
81 
82 #define	JEMALLOC_VERSION "4.0.2-0-g486d249fb4715fd3de679b6c2a04f7e657883111"
83 #define	JEMALLOC_VERSION_MAJOR 4
84 #define	JEMALLOC_VERSION_MINOR 0
85 #define	JEMALLOC_VERSION_BUGFIX 2
86 #define	JEMALLOC_VERSION_NREV 0
87 #define	JEMALLOC_VERSION_GID "486d249fb4715fd3de679b6c2a04f7e657883111"
88 
89 #  define MALLOCX_LG_ALIGN(la)	(la)
90 #  if LG_SIZEOF_PTR == 2
91 #    define MALLOCX_ALIGN(a)	(ffs(a)-1)
92 #  else
93 #    define MALLOCX_ALIGN(a)						\
94 	 ((a < (size_t)INT_MAX) ? ffs(a)-1 : ffs(a>>32)+31)
95 #  endif
96 #  define MALLOCX_ZERO	((int)0x40)
97 /*
98  * Bias tcache index bits so that 0 encodes "automatic tcache management", and 1
99  * encodes MALLOCX_TCACHE_NONE.
100  */
101 #  define MALLOCX_TCACHE(tc)	((int)(((tc)+2) << 8))
102 #  define MALLOCX_TCACHE_NONE	MALLOCX_TCACHE(-1)
103 /*
104  * Bias arena index bits so that 0 encodes "use an automatically chosen arena".
105  */
106 #  define MALLOCX_ARENA(a)	((int)(((a)+1) << 20))
107 
108 #if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)
109 #  define JEMALLOC_CXX_THROW throw()
110 #else
111 #  define JEMALLOC_CXX_THROW
112 #endif
113 
114 #ifdef JEMALLOC_HAVE_ATTR
115 #  define JEMALLOC_ATTR(s) __attribute__((s))
116 #  define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))
117 #  ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE
118 #    define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
119 #    define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))
120 #  else
121 #    define JEMALLOC_ALLOC_SIZE(s)
122 #    define JEMALLOC_ALLOC_SIZE2(s1, s2)
123 #  endif
124 #  ifndef JEMALLOC_EXPORT
125 #    define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
126 #  endif
127 #  ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
128 #    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))
129 #  elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)
130 #    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))
131 #  else
132 #    define JEMALLOC_FORMAT_PRINTF(s, i)
133 #  endif
134 #  define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
135 #  define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
136 #  define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
137 #  define JEMALLOC_RESTRICT_RETURN
138 #  define JEMALLOC_ALLOCATOR
139 #elif _MSC_VER
140 #  define JEMALLOC_ATTR(s)
141 #  define JEMALLOC_ALIGNED(s) __declspec(align(s))
142 #  define JEMALLOC_ALLOC_SIZE(s)
143 #  define JEMALLOC_ALLOC_SIZE2(s1, s2)
144 #  ifndef JEMALLOC_EXPORT
145 #    ifdef DLLEXPORT
146 #      define JEMALLOC_EXPORT __declspec(dllexport)
147 #    else
148 #      define JEMALLOC_EXPORT __declspec(dllimport)
149 #    endif
150 #  endif
151 #  define JEMALLOC_FORMAT_PRINTF(s, i)
152 #  define JEMALLOC_NOINLINE __declspec(noinline)
153 #  ifdef __cplusplus
154 #    define JEMALLOC_NOTHROW __declspec(nothrow)
155 #  else
156 #    define JEMALLOC_NOTHROW
157 #  endif
158 #  define JEMALLOC_SECTION(s) __declspec(allocate(s))
159 #  define JEMALLOC_RESTRICT_RETURN __declspec(restrict)
160 #  if _MSC_VER >= 1900 && !defined(__EDG__)
161 #    define JEMALLOC_ALLOCATOR __declspec(allocator)
162 #  else
163 #    define JEMALLOC_ALLOCATOR
164 #  endif
165 #else
166 #  define JEMALLOC_ATTR(s)
167 #  define JEMALLOC_ALIGNED(s)
168 #  define JEMALLOC_ALLOC_SIZE(s)
169 #  define JEMALLOC_ALLOC_SIZE2(s1, s2)
170 #  define JEMALLOC_EXPORT
171 #  define JEMALLOC_FORMAT_PRINTF(s, i)
172 #  define JEMALLOC_NOINLINE
173 #  define JEMALLOC_NOTHROW
174 #  define JEMALLOC_SECTION(s)
175 #  define JEMALLOC_RESTRICT_RETURN
176 #  define JEMALLOC_ALLOCATOR
177 #endif
178 
179 /*
180  * The je_ prefix on the following public symbol declarations is an artifact
181  * of namespace management, and should be omitted in application code unless
182  * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle.h).
183  */
184 extern JEMALLOC_EXPORT const char	*je_malloc_conf;
185 extern JEMALLOC_EXPORT void		(*je_malloc_message)(void *cbopaque,
186     const char *s);
187 
188 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
189     void JEMALLOC_NOTHROW	*je_malloc(size_t size)
190     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
191 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
192     void JEMALLOC_NOTHROW	*je_calloc(size_t num, size_t size)
193     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);
194 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_posix_memalign(void **memptr,
195     size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));
196 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
197     void JEMALLOC_NOTHROW	*je_aligned_alloc(size_t alignment,
198     size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)
199     JEMALLOC_ALLOC_SIZE(2);
200 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
201     void JEMALLOC_NOTHROW	*je_realloc(void *ptr, size_t size)
202     JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);
203 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_free(void *ptr)
204     JEMALLOC_CXX_THROW;
205 
206 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
207     void JEMALLOC_NOTHROW	*je_mallocx(size_t size, int flags)
208     JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
209 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
210     void JEMALLOC_NOTHROW	*je_rallocx(void *ptr, size_t size,
211     int flags) JEMALLOC_ALLOC_SIZE(2);
212 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_xallocx(void *ptr, size_t size,
213     size_t extra, int flags);
214 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_sallocx(const void *ptr,
215     int flags) JEMALLOC_ATTR(pure);
216 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_dallocx(void *ptr, int flags);
217 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_sdallocx(void *ptr, size_t size,
218     int flags);
219 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_nallocx(size_t size, int flags)
220     JEMALLOC_ATTR(pure);
221 
222 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctl(const char *name,
223     void *oldp, size_t *oldlenp, void *newp, size_t newlen);
224 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlnametomib(const char *name,
225     size_t *mibp, size_t *miblenp);
226 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlbymib(const size_t *mib,
227     size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
228 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_malloc_stats_print(
229     void (*write_cb)(void *, const char *), void *je_cbopaque,
230     const char *opts);
231 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_malloc_usable_size(
232     JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
233 
234 #ifdef JEMALLOC_OVERRIDE_MEMALIGN
235 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
236     void JEMALLOC_NOTHROW	*je_memalign(size_t alignment, size_t size)
237     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);
238 #endif
239 
240 #ifdef JEMALLOC_OVERRIDE_VALLOC
241 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
242     void JEMALLOC_NOTHROW	*je_valloc(size_t size) JEMALLOC_CXX_THROW
243     JEMALLOC_ATTR(malloc);
244 #endif
245 
246 /*
247  * void *
248  * chunk_alloc(void *new_addr, size_t size, size_t alignment, bool *zero,
249  *     bool *commit, unsigned arena_ind);
250  */
251 typedef void *(chunk_alloc_t)(void *, size_t, size_t, bool *, bool *, unsigned);
252 
253 /*
254  * bool
255  * chunk_dalloc(void *chunk, size_t size, bool committed, unsigned arena_ind);
256  */
257 typedef bool (chunk_dalloc_t)(void *, size_t, bool, unsigned);
258 
259 /*
260  * bool
261  * chunk_commit(void *chunk, size_t size, size_t offset, size_t length,
262  *     unsigned arena_ind);
263  */
264 typedef bool (chunk_commit_t)(void *, size_t, size_t, size_t, unsigned);
265 
266 /*
267  * bool
268  * chunk_decommit(void *chunk, size_t size, size_t offset, size_t length,
269  *     unsigned arena_ind);
270  */
271 typedef bool (chunk_decommit_t)(void *, size_t, size_t, size_t, unsigned);
272 
273 /*
274  * bool
275  * chunk_purge(void *chunk, size_t size, size_t offset, size_t length,
276  *     unsigned arena_ind);
277  */
278 typedef bool (chunk_purge_t)(void *, size_t, size_t, size_t, unsigned);
279 
280 /*
281  * bool
282  * chunk_split(void *chunk, size_t size, size_t size_a, size_t size_b,
283  *     bool committed, unsigned arena_ind);
284  */
285 typedef bool (chunk_split_t)(void *, size_t, size_t, size_t, bool, unsigned);
286 
287 /*
288  * bool
289  * chunk_merge(void *chunk_a, size_t size_a, void *chunk_b, size_t size_b,
290  *     bool committed, unsigned arena_ind);
291  */
292 typedef bool (chunk_merge_t)(void *, size_t, void *, size_t, bool, unsigned);
293 
294 typedef struct {
295 	chunk_alloc_t		*alloc;
296 	chunk_dalloc_t		*dalloc;
297 	chunk_commit_t		*commit;
298 	chunk_decommit_t	*decommit;
299 	chunk_purge_t		*purge;
300 	chunk_split_t		*split;
301 	chunk_merge_t		*merge;
302 } chunk_hooks_t;
303 
304 /*
305  * By default application code must explicitly refer to mangled symbol names,
306  * so that it is possible to use jemalloc in conjunction with another allocator
307  * in the same application.  Define JEMALLOC_MANGLE in order to cause automatic
308  * name mangling that matches the API prefixing that happened as a result of
309  * --with-mangling and/or --with-jemalloc-prefix configuration settings.
310  */
311 #ifdef JEMALLOC_MANGLE
312 #  ifndef JEMALLOC_NO_DEMANGLE
313 #    define JEMALLOC_NO_DEMANGLE
314 #  endif
315 #  define malloc_conf je_malloc_conf
316 #  define malloc_message je_malloc_message
317 #  define malloc je_malloc
318 #  define calloc je_calloc
319 #  define posix_memalign je_posix_memalign
320 #  define aligned_alloc je_aligned_alloc
321 #  define realloc je_realloc
322 #  define free je_free
323 #  define mallocx je_mallocx
324 #  define rallocx je_rallocx
325 #  define xallocx je_xallocx
326 #  define sallocx je_sallocx
327 #  define dallocx je_dallocx
328 #  define sdallocx je_sdallocx
329 #  define nallocx je_nallocx
330 #  define mallctl je_mallctl
331 #  define mallctlnametomib je_mallctlnametomib
332 #  define mallctlbymib je_mallctlbymib
333 #  define malloc_stats_print je_malloc_stats_print
334 #  define malloc_usable_size je_malloc_usable_size
335 #  define valloc je_valloc
336 #endif
337 
338 /*
339  * The je_* macros can be used as stable alternative names for the
340  * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined.  This is primarily
341  * meant for use in jemalloc itself, but it can be used by application code to
342  * provide isolation from the name mangling specified via --with-mangling
343  * and/or --with-jemalloc-prefix.
344  */
345 #ifndef JEMALLOC_NO_DEMANGLE
346 #  undef je_malloc_conf
347 #  undef je_malloc_message
348 #  undef je_malloc
349 #  undef je_calloc
350 #  undef je_posix_memalign
351 #  undef je_aligned_alloc
352 #  undef je_realloc
353 #  undef je_free
354 #  undef je_mallocx
355 #  undef je_rallocx
356 #  undef je_xallocx
357 #  undef je_sallocx
358 #  undef je_dallocx
359 #  undef je_sdallocx
360 #  undef je_nallocx
361 #  undef je_mallctl
362 #  undef je_mallctlnametomib
363 #  undef je_mallctlbymib
364 #  undef je_malloc_stats_print
365 #  undef je_malloc_usable_size
366 #  undef je_valloc
367 #endif
368 
369 #ifdef __cplusplus
370 }
371 #endif
372 #endif /* JEMALLOC_H_ */
373