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