blob: bbd3ca5964b8813fd47465a8c8ad7d25f72d5e89 [file] [log] [blame]
Christopher Ferris5daf4e42014-04-22 18:52:58 -07001#ifndef JEMALLOC_H_
2#define JEMALLOC_H_
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/* Defined if __attribute__((...)) syntax is supported. */
8#define JEMALLOC_HAVE_ATTR
9
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070010/* Defined if alloc_size attribute is supported. */
Christopher Ferris6c1bfbf2015-09-15 13:24:03 -070011/* #undef JEMALLOC_HAVE_ATTR_ALLOC_SIZE */
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070012
13/* Defined if format(gnu_printf, ...) attribute is supported. */
Colin Cross70274782015-12-29 16:56:11 -080014#if !defined(__clang__)
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070015#define JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
Colin Cross70274782015-12-29 16:56:11 -080016#endif
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070017
18/* Defined if format(printf, ...) attribute is supported. */
19#define JEMALLOC_HAVE_ATTR_FORMAT_PRINTF
20
Christopher Ferris5daf4e42014-04-22 18:52:58 -070021/*
22 * Define overrides for non-standard allocator-related functions if they are
23 * present on the system.
24 */
25#define JEMALLOC_OVERRIDE_MEMALIGN
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070026#ifndef __LP64__
Christopher Ferrisbf9b0182016-12-08 11:56:18 -080027#define JEMALLOC_OVERRIDE_VALLOC
Christopher Ferris96d58c82015-04-22 06:59:40 +000028#endif
Christopher Ferris5daf4e42014-04-22 18:52:58 -070029
30/*
31 * At least Linux omits the "const" in:
32 *
33 * size_t malloc_usable_size(const void *ptr);
34 *
35 * Match the operating system's prototype.
36 */
37#define JEMALLOC_USABLE_SIZE_CONST const
38
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070039/*
40 * If defined, specify throw() for the public function prototypes when compiling
41 * with C++. The only justification for this is to match the prototypes that
42 * glibc defines.
43 */
Christopher Ferrisbf9b0182016-12-08 11:56:18 -080044/* #undef JEMALLOC_USE_CXX_THROW */
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070045
Christopher Ferris473a1852016-03-02 16:24:07 -080046#ifdef _MSC_VER
47# ifdef _WIN64
48# define LG_SIZEOF_PTR_WIN 3
49# else
50# define LG_SIZEOF_PTR_WIN 2
51# endif
52#endif
53
Christopher Ferris5daf4e42014-04-22 18:52:58 -070054/* sizeof(void *) == 2^LG_SIZEOF_PTR. */
Christopher Ferris9ebe2ac2014-07-01 14:34:56 -070055#ifdef __LP64__
56#define LG_SIZEOF_PTR 3
57#else
Christopher Ferris5daf4e42014-04-22 18:52:58 -070058#define LG_SIZEOF_PTR 2
Christopher Ferris9ebe2ac2014-07-01 14:34:56 -070059#endif
Christopher Ferris5daf4e42014-04-22 18:52:58 -070060
61/*
62 * Name mangling for public symbols is controlled by --with-mangling and
63 * --with-jemalloc-prefix. With default settings the je_ prefix is stripped by
64 * these macro definitions.
65 */
66#ifndef JEMALLOC_NO_RENAME
67# define je_malloc_conf je_malloc_conf
68# define je_malloc_message je_malloc_message
69# define je_malloc je_malloc
70# define je_calloc je_calloc
71# define je_posix_memalign je_posix_memalign
72# define je_aligned_alloc je_aligned_alloc
73# define je_realloc je_realloc
74# define je_free je_free
75# define je_mallocx je_mallocx
76# define je_rallocx je_rallocx
77# define je_xallocx je_xallocx
78# define je_sallocx je_sallocx
79# define je_dallocx je_dallocx
Christopher Ferris96d58c82015-04-22 06:59:40 +000080# define je_sdallocx je_sdallocx
Christopher Ferris5daf4e42014-04-22 18:52:58 -070081# define je_nallocx je_nallocx
82# define je_mallctl je_mallctl
83# define je_mallctlnametomib je_mallctlnametomib
84# define je_mallctlbymib je_mallctlbymib
85# define je_malloc_stats_print je_malloc_stats_print
86# define je_malloc_usable_size je_malloc_usable_size
87# define je_memalign je_memalign
88# define je_valloc je_valloc
89#endif
90
Christopher Ferris96d58c82015-04-22 06:59:40 +000091#include <stdlib.h>
92#include <stdbool.h>
93#include <stdint.h>
Christopher Ferris5daf4e42014-04-22 18:52:58 -070094#include <limits.h>
95#include <strings.h>
96
Christopher Ferrisbf9b0182016-12-08 11:56:18 -080097#define JEMALLOC_VERSION "4.4.0-0-gf1f76357313e7dcad7262f17a48ff0a2e005fcdc"
Christopher Ferris6f50cbc2015-09-09 12:17:01 -070098#define JEMALLOC_VERSION_MAJOR 4
Christopher Ferrisbf9b0182016-12-08 11:56:18 -080099#define JEMALLOC_VERSION_MINOR 4
100#define JEMALLOC_VERSION_BUGFIX 0
Christopher Ferris35452472016-06-14 14:28:47 -0700101#define JEMALLOC_VERSION_NREV 0
Christopher Ferrisbf9b0182016-12-08 11:56:18 -0800102#define JEMALLOC_VERSION_GID "f1f76357313e7dcad7262f17a48ff0a2e005fcdc"
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700103
Christopher Ferris473a1852016-03-02 16:24:07 -0800104# define MALLOCX_LG_ALIGN(la) ((int)(la))
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700105# if LG_SIZEOF_PTR == 2
Christopher Ferris35452472016-06-14 14:28:47 -0700106# define MALLOCX_ALIGN(a) ((int)(ffs((int)(a))-1))
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700107# else
108# define MALLOCX_ALIGN(a) \
Christopher Ferris35452472016-06-14 14:28:47 -0700109 ((int)(((size_t)(a) < (size_t)INT_MAX) ? ffs((int)(a))-1 : \
110 ffs((int)(((size_t)(a))>>32))+31))
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700111# endif
112# define MALLOCX_ZERO ((int)0x40)
Christopher Ferris96d58c82015-04-22 06:59:40 +0000113/*
114 * Bias tcache index bits so that 0 encodes "automatic tcache management", and 1
115 * encodes MALLOCX_TCACHE_NONE.
116 */
117# define MALLOCX_TCACHE(tc) ((int)(((tc)+2) << 8))
118# define MALLOCX_TCACHE_NONE MALLOCX_TCACHE(-1)
119/*
120 * Bias arena index bits so that 0 encodes "use an automatically chosen arena".
121 */
Christopher Ferris35452472016-06-14 14:28:47 -0700122# define MALLOCX_ARENA(a) ((((int)(a))+1) << 20)
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700123
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700124#if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)
125# define JEMALLOC_CXX_THROW throw()
126#else
127# define JEMALLOC_CXX_THROW
128#endif
129
Christopher Ferrisfb1f0942016-11-08 14:47:48 -0800130#if _MSC_VER
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700131# define JEMALLOC_ATTR(s)
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700132# define JEMALLOC_ALIGNED(s) __declspec(align(s))
133# define JEMALLOC_ALLOC_SIZE(s)
134# define JEMALLOC_ALLOC_SIZE2(s1, s2)
Christopher Ferris96d58c82015-04-22 06:59:40 +0000135# ifndef JEMALLOC_EXPORT
136# ifdef DLLEXPORT
137# define JEMALLOC_EXPORT __declspec(dllexport)
138# else
139# define JEMALLOC_EXPORT __declspec(dllimport)
140# endif
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700141# endif
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700142# define JEMALLOC_FORMAT_PRINTF(s, i)
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700143# define JEMALLOC_NOINLINE __declspec(noinline)
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700144# ifdef __cplusplus
145# define JEMALLOC_NOTHROW __declspec(nothrow)
146# else
147# define JEMALLOC_NOTHROW
148# endif
149# define JEMALLOC_SECTION(s) __declspec(allocate(s))
150# define JEMALLOC_RESTRICT_RETURN __declspec(restrict)
151# if _MSC_VER >= 1900 && !defined(__EDG__)
152# define JEMALLOC_ALLOCATOR __declspec(allocator)
153# else
154# define JEMALLOC_ALLOCATOR
155# endif
Christopher Ferrisad6417c2016-02-01 10:59:37 -0800156#elif defined(JEMALLOC_HAVE_ATTR)
157# define JEMALLOC_ATTR(s) __attribute__((s))
158# define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))
159# ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE
160# define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
161# define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))
162# else
163# define JEMALLOC_ALLOC_SIZE(s)
164# define JEMALLOC_ALLOC_SIZE2(s1, s2)
165# endif
166# ifndef JEMALLOC_EXPORT
167# define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
168# endif
169# ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
170# define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))
171# elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)
172# define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))
173# else
174# define JEMALLOC_FORMAT_PRINTF(s, i)
175# endif
176# define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
177# define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
178# define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
179# define JEMALLOC_RESTRICT_RETURN
180# define JEMALLOC_ALLOCATOR
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700181#else
182# define JEMALLOC_ATTR(s)
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700183# define JEMALLOC_ALIGNED(s)
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700184# define JEMALLOC_ALLOC_SIZE(s)
185# define JEMALLOC_ALLOC_SIZE2(s1, s2)
186# define JEMALLOC_EXPORT
187# define JEMALLOC_FORMAT_PRINTF(s, i)
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700188# define JEMALLOC_NOINLINE
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700189# define JEMALLOC_NOTHROW
190# define JEMALLOC_SECTION(s)
191# define JEMALLOC_RESTRICT_RETURN
192# define JEMALLOC_ALLOCATOR
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700193#endif
194
195/*
196 * The je_ prefix on the following public symbol declarations is an artifact
197 * of namespace management, and should be omitted in application code unless
198 * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle.h).
199 */
200extern JEMALLOC_EXPORT const char *je_malloc_conf;
201extern JEMALLOC_EXPORT void (*je_malloc_message)(void *cbopaque,
202 const char *s);
203
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700204JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
205 void JEMALLOC_NOTHROW *je_malloc(size_t size)
206 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
207JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
208 void JEMALLOC_NOTHROW *je_calloc(size_t num, size_t size)
209 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);
210JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_posix_memalign(void **memptr,
211 size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));
212JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
213 void JEMALLOC_NOTHROW *je_aligned_alloc(size_t alignment,
214 size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)
215 JEMALLOC_ALLOC_SIZE(2);
216JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
217 void JEMALLOC_NOTHROW *je_realloc(void *ptr, size_t size)
218 JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);
219JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_free(void *ptr)
220 JEMALLOC_CXX_THROW;
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700221
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700222JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
223 void JEMALLOC_NOTHROW *je_mallocx(size_t size, int flags)
224 JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
225JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
226 void JEMALLOC_NOTHROW *je_rallocx(void *ptr, size_t size,
227 int flags) JEMALLOC_ALLOC_SIZE(2);
228JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_xallocx(void *ptr, size_t size,
229 size_t extra, int flags);
230JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_sallocx(const void *ptr,
231 int flags) JEMALLOC_ATTR(pure);
232JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_dallocx(void *ptr, int flags);
233JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_sdallocx(void *ptr, size_t size,
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700234 int flags);
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700235JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_nallocx(size_t size, int flags)
Christopher Ferris96d58c82015-04-22 06:59:40 +0000236 JEMALLOC_ATTR(pure);
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700237
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700238JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctl(const char *name,
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700239 void *oldp, size_t *oldlenp, void *newp, size_t newlen);
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700240JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctlnametomib(const char *name,
241 size_t *mibp, size_t *miblenp);
242JEMALLOC_EXPORT int JEMALLOC_NOTHROW je_mallctlbymib(const size_t *mib,
243 size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
244JEMALLOC_EXPORT void JEMALLOC_NOTHROW je_malloc_stats_print(
245 void (*write_cb)(void *, const char *), void *je_cbopaque,
246 const char *opts);
247JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_malloc_usable_size(
248 JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700249
250#ifdef JEMALLOC_OVERRIDE_MEMALIGN
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700251JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
252 void JEMALLOC_NOTHROW *je_memalign(size_t alignment, size_t size)
253 JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700254#endif
255
256#ifdef JEMALLOC_OVERRIDE_VALLOC
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700257JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
258 void JEMALLOC_NOTHROW *je_valloc(size_t size) JEMALLOC_CXX_THROW
259 JEMALLOC_ATTR(malloc);
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700260#endif
261
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700262/*
263 * void *
264 * chunk_alloc(void *new_addr, size_t size, size_t alignment, bool *zero,
265 * bool *commit, unsigned arena_ind);
266 */
267typedef void *(chunk_alloc_t)(void *, size_t, size_t, bool *, bool *, unsigned);
268
269/*
270 * bool
271 * chunk_dalloc(void *chunk, size_t size, bool committed, unsigned arena_ind);
272 */
273typedef bool (chunk_dalloc_t)(void *, size_t, bool, unsigned);
274
275/*
276 * bool
277 * chunk_commit(void *chunk, size_t size, size_t offset, size_t length,
278 * unsigned arena_ind);
279 */
280typedef bool (chunk_commit_t)(void *, size_t, size_t, size_t, unsigned);
281
282/*
283 * bool
284 * chunk_decommit(void *chunk, size_t size, size_t offset, size_t length,
285 * unsigned arena_ind);
286 */
287typedef bool (chunk_decommit_t)(void *, size_t, size_t, size_t, unsigned);
288
289/*
290 * bool
291 * chunk_purge(void *chunk, size_t size, size_t offset, size_t length,
292 * unsigned arena_ind);
293 */
294typedef bool (chunk_purge_t)(void *, size_t, size_t, size_t, unsigned);
295
296/*
297 * bool
298 * chunk_split(void *chunk, size_t size, size_t size_a, size_t size_b,
299 * bool committed, unsigned arena_ind);
300 */
301typedef bool (chunk_split_t)(void *, size_t, size_t, size_t, bool, unsigned);
302
303/*
304 * bool
305 * chunk_merge(void *chunk_a, size_t size_a, void *chunk_b, size_t size_b,
306 * bool committed, unsigned arena_ind);
307 */
308typedef bool (chunk_merge_t)(void *, size_t, void *, size_t, bool, unsigned);
309
310typedef struct {
311 chunk_alloc_t *alloc;
312 chunk_dalloc_t *dalloc;
313 chunk_commit_t *commit;
314 chunk_decommit_t *decommit;
315 chunk_purge_t *purge;
316 chunk_split_t *split;
317 chunk_merge_t *merge;
318} chunk_hooks_t;
Christopher Ferris9ebe2ac2014-07-01 14:34:56 -0700319
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700320/*
321 * By default application code must explicitly refer to mangled symbol names,
322 * so that it is possible to use jemalloc in conjunction with another allocator
323 * in the same application. Define JEMALLOC_MANGLE in order to cause automatic
324 * name mangling that matches the API prefixing that happened as a result of
325 * --with-mangling and/or --with-jemalloc-prefix configuration settings.
326 */
327#ifdef JEMALLOC_MANGLE
328# ifndef JEMALLOC_NO_DEMANGLE
329# define JEMALLOC_NO_DEMANGLE
330# endif
331# define malloc_conf je_malloc_conf
332# define malloc_message je_malloc_message
333# define malloc je_malloc
334# define calloc je_calloc
335# define posix_memalign je_posix_memalign
336# define aligned_alloc je_aligned_alloc
337# define realloc je_realloc
338# define free je_free
339# define mallocx je_mallocx
340# define rallocx je_rallocx
341# define xallocx je_xallocx
342# define sallocx je_sallocx
343# define dallocx je_dallocx
Christopher Ferris96d58c82015-04-22 06:59:40 +0000344# define sdallocx je_sdallocx
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700345# define nallocx je_nallocx
346# define mallctl je_mallctl
347# define mallctlnametomib je_mallctlnametomib
348# define mallctlbymib je_mallctlbymib
349# define malloc_stats_print je_malloc_stats_print
350# define malloc_usable_size je_malloc_usable_size
351# define memalign je_memalign
352# define valloc je_valloc
353#endif
354
355/*
356 * The je_* macros can be used as stable alternative names for the
357 * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined. This is primarily
358 * meant for use in jemalloc itself, but it can be used by application code to
359 * provide isolation from the name mangling specified via --with-mangling
360 * and/or --with-jemalloc-prefix.
361 */
362#ifndef JEMALLOC_NO_DEMANGLE
363# undef je_malloc_conf
364# undef je_malloc_message
365# undef je_malloc
366# undef je_calloc
367# undef je_posix_memalign
368# undef je_aligned_alloc
369# undef je_realloc
370# undef je_free
371# undef je_mallocx
372# undef je_rallocx
373# undef je_xallocx
374# undef je_sallocx
375# undef je_dallocx
Christopher Ferris96d58c82015-04-22 06:59:40 +0000376# undef je_sdallocx
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700377# undef je_nallocx
378# undef je_mallctl
379# undef je_mallctlnametomib
380# undef je_mallctlbymib
381# undef je_malloc_stats_print
382# undef je_malloc_usable_size
383# undef je_memalign
384# undef je_valloc
385#endif
386
387#ifdef __cplusplus
Christopher Ferris6f50cbc2015-09-09 12:17:01 -0700388}
Christopher Ferris5daf4e42014-04-22 18:52:58 -0700389#endif
390#endif /* JEMALLOC_H_ */