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