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 #define 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 #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 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 #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 #if defined(__LP64__)
     52 #define LG_SIZEOF_PTR 3
     53 #else
     54 #define LG_SIZEOF_PTR 2
     55 #endif
     56 
     57 /*
     58  * Name mangling for public symbols is controlled by --with-mangling and
     59  * --with-jemalloc-prefix.  With default settings the je_ prefix is stripped by
     60  * these macro definitions.
     61  */
     62 #include "jemalloc_rename.h"
     63 
     64 #include <stdlib.h>
     65 #include <stdbool.h>
     66 #include <stdint.h>
     67 #include <limits.h>
     68 #include <strings.h>
     69 
     70 #define JEMALLOC_VERSION "5.1.0-0-g61efbda7098de6fe64c362d309824864308c36d4"
     71 #define JEMALLOC_VERSION_MAJOR 5
     72 #define JEMALLOC_VERSION_MINOR 1
     73 #define JEMALLOC_VERSION_BUGFIX 0
     74 #define JEMALLOC_VERSION_NREV 0
     75 #define JEMALLOC_VERSION_GID "61efbda7098de6fe64c362d309824864308c36d4"
     76 
     77 #define MALLOCX_LG_ALIGN(la)	((int)(la))
     78 #if LG_SIZEOF_PTR == 2
     79 #  define MALLOCX_ALIGN(a)	((int)(ffs((int)(a))-1))
     80 #else
     81 #  define MALLOCX_ALIGN(a)						\
     82      ((int)(((size_t)(a) < (size_t)INT_MAX) ? ffs((int)(a))-1 :	\
     83      ffs((int)(((size_t)(a))>>32))+31))
     84 #endif
     85 #define MALLOCX_ZERO	((int)0x40)
     86 /*
     87  * Bias tcache index bits so that 0 encodes "automatic tcache management", and 1
     88  * encodes MALLOCX_TCACHE_NONE.
     89  */
     90 #define MALLOCX_TCACHE(tc)	((int)(((tc)+2) << 8))
     91 #define MALLOCX_TCACHE_NONE	MALLOCX_TCACHE(-1)
     92 /*
     93  * Bias arena index bits so that 0 encodes "use an automatically chosen arena".
     94  */
     95 #define MALLOCX_ARENA(a)	((((int)(a))+1) << 20)
     96 
     97 /*
     98  * Use as arena index in "arena.<i>.{purge,decay,dss}" and
     99  * "stats.arenas.<i>.*" mallctl interfaces to select all arenas.  This
    100  * definition is intentionally specified in raw decimal format to support
    101  * cpp-based string concatenation, e.g.
    102  *
    103  *   #define STRINGIFY_HELPER(x) #x
    104  *   #define STRINGIFY(x) STRINGIFY_HELPER(x)
    105  *
    106  *   mallctl("arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".purge", NULL, NULL, NULL,
    107  *       0);
    108  */
    109 #define MALLCTL_ARENAS_ALL	4096
    110 /*
    111  * Use as arena index in "stats.arenas.<i>.*" mallctl interfaces to select
    112  * destroyed arenas.
    113  */
    114 #define MALLCTL_ARENAS_DESTROYED	4097
    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 defined(_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 #    define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))
    153 #    define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))
    154 #  else
    155 #    define JEMALLOC_ALLOC_SIZE(s)
    156 #    define JEMALLOC_ALLOC_SIZE2(s1, s2)
    157 #  endif
    158 #  ifndef JEMALLOC_EXPORT
    159 #    define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility("default"))
    160 #  endif
    161 #  ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF
    162 #    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))
    163 #  elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)
    164 #    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))
    165 #  else
    166 #    define JEMALLOC_FORMAT_PRINTF(s, i)
    167 #  endif
    168 #  define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)
    169 #  define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)
    170 #  define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))
    171 #  define JEMALLOC_RESTRICT_RETURN
    172 #  define JEMALLOC_ALLOCATOR
    173 #else
    174 #  define JEMALLOC_ATTR(s)
    175 #  define JEMALLOC_ALIGNED(s)
    176 #  define JEMALLOC_ALLOC_SIZE(s)
    177 #  define JEMALLOC_ALLOC_SIZE2(s1, s2)
    178 #  define JEMALLOC_EXPORT
    179 #  define JEMALLOC_FORMAT_PRINTF(s, i)
    180 #  define JEMALLOC_NOINLINE
    181 #  define JEMALLOC_NOTHROW
    182 #  define JEMALLOC_SECTION(s)
    183 #  define JEMALLOC_RESTRICT_RETURN
    184 #  define JEMALLOC_ALLOCATOR
    185 #endif
    186 
    187 /*
    188  * The je_ prefix on the following public symbol declarations is an artifact
    189  * of namespace management, and should be omitted in application code unless
    190  * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle.h).
    191  */
    192 extern JEMALLOC_EXPORT const char	*je_malloc_conf;
    193 extern JEMALLOC_EXPORT void		(*je_malloc_message)(void *cbopaque,
    194     const char *s);
    195 
    196 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    197     void JEMALLOC_NOTHROW	*je_malloc(size_t size)
    198     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
    199 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    200     void JEMALLOC_NOTHROW	*je_calloc(size_t num, size_t size)
    201     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);
    202 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_posix_memalign(void **memptr,
    203     size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));
    204 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    205     void JEMALLOC_NOTHROW	*je_aligned_alloc(size_t alignment,
    206     size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)
    207     JEMALLOC_ALLOC_SIZE(2);
    208 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    209     void JEMALLOC_NOTHROW	*je_realloc(void *ptr, size_t size)
    210     JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);
    211 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_free(void *ptr)
    212     JEMALLOC_CXX_THROW;
    213 
    214 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    215     void JEMALLOC_NOTHROW	*je_mallocx(size_t size, int flags)
    216     JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);
    217 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    218     void JEMALLOC_NOTHROW	*je_rallocx(void *ptr, size_t size,
    219     int flags) JEMALLOC_ALLOC_SIZE(2);
    220 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_xallocx(void *ptr, size_t size,
    221     size_t extra, int flags);
    222 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_sallocx(const void *ptr,
    223     int flags) JEMALLOC_ATTR(pure);
    224 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_dallocx(void *ptr, int flags);
    225 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_sdallocx(void *ptr, size_t size,
    226     int flags);
    227 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_nallocx(size_t size, int flags)
    228     JEMALLOC_ATTR(pure);
    229 
    230 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctl(const char *name,
    231     void *oldp, size_t *oldlenp, void *newp, size_t newlen);
    232 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlnametomib(const char *name,
    233     size_t *mibp, size_t *miblenp);
    234 JEMALLOC_EXPORT int JEMALLOC_NOTHROW	je_mallctlbymib(const size_t *mib,
    235     size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);
    236 JEMALLOC_EXPORT void JEMALLOC_NOTHROW	je_malloc_stats_print(
    237     void (*write_cb)(void *, const char *), void *je_cbopaque,
    238     const char *opts);
    239 JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW	je_malloc_usable_size(
    240     JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
    241 
    242 #ifdef JEMALLOC_OVERRIDE_MEMALIGN
    243 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    244     void JEMALLOC_NOTHROW	*je_memalign(size_t alignment, size_t size)
    245     JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);
    246 #endif
    247 
    248 #ifdef JEMALLOC_OVERRIDE_VALLOC
    249 JEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN
    250     void JEMALLOC_NOTHROW	*je_valloc(size_t size) JEMALLOC_CXX_THROW
    251     JEMALLOC_ATTR(malloc);
    252 #endif
    253 
    254 typedef struct extent_hooks_s extent_hooks_t;
    255 
    256 /*
    257  * void *
    258  * extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
    259  *     size_t alignment, bool *zero, bool *commit, unsigned arena_ind);
    260  */
    261 typedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t, bool *,
    262     bool *, unsigned);
    263 
    264 /*
    265  * bool
    266  * extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,
    267  *     bool committed, unsigned arena_ind);
    268  */
    269 typedef bool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, bool,
    270     unsigned);
    271 
    272 /*
    273  * void
    274  * extent_destroy(extent_hooks_t *extent_hooks, void *addr, size_t size,
    275  *     bool committed, unsigned arena_ind);
    276  */
    277 typedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, bool,
    278     unsigned);
    279 
    280 /*
    281  * bool
    282  * extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,
    283  *     size_t offset, size_t length, unsigned arena_ind);
    284  */
    285 typedef bool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
    286     unsigned);
    287 
    288 /*
    289  * bool
    290  * extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,
    291  *     size_t offset, size_t length, unsigned arena_ind);
    292  */
    293 typedef bool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,
    294     size_t, unsigned);
    295 
    296 /*
    297  * bool
    298  * extent_purge(extent_hooks_t *extent_hooks, void *addr, size_t size,
    299  *     size_t offset, size_t length, unsigned arena_ind);
    300  */
    301 typedef bool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
    302     unsigned);
    303 
    304 /*
    305  * bool
    306  * extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,
    307  *     size_t size_a, size_t size_b, bool committed, unsigned arena_ind);
    308  */
    309 typedef bool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
    310     bool, unsigned);
    311 
    312 /*
    313  * bool
    314  * extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
    315  *     void *addr_b, size_t size_b, bool committed, unsigned arena_ind);
    316  */
    317 typedef bool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,
    318     bool, unsigned);
    319 
    320 struct extent_hooks_s {
    321 	extent_alloc_t		*alloc;
    322 	extent_dalloc_t		*dalloc;
    323 	extent_destroy_t	*destroy;
    324 	extent_commit_t		*commit;
    325 	extent_decommit_t	*decommit;
    326 	extent_purge_t		*purge_lazy;
    327 	extent_purge_t		*purge_forced;
    328 	extent_split_t		*split;
    329 	extent_merge_t		*merge;
    330 };
    331 
    332 /*
    333  * By default application code must explicitly refer to mangled symbol names,
    334  * so that it is possible to use jemalloc in conjunction with another allocator
    335  * in the same application.  Define JEMALLOC_MANGLE in order to cause automatic
    336  * name mangling that matches the API prefixing that happened as a result of
    337  * --with-mangling and/or --with-jemalloc-prefix configuration settings.
    338  */
    339 #ifdef JEMALLOC_MANGLE
    340 #  ifndef JEMALLOC_NO_DEMANGLE
    341 #    define JEMALLOC_NO_DEMANGLE
    342 #  endif
    343 #  define aligned_alloc je_aligned_alloc
    344 #  define calloc je_calloc
    345 #  define dallocx je_dallocx
    346 #  define free je_free
    347 #  define mallctl je_mallctl
    348 #  define mallctlbymib je_mallctlbymib
    349 #  define mallctlnametomib je_mallctlnametomib
    350 #  define malloc je_malloc
    351 #  define malloc_conf je_malloc_conf
    352 #  define malloc_message je_malloc_message
    353 #  define malloc_stats_print je_malloc_stats_print
    354 #  define malloc_usable_size je_malloc_usable_size
    355 #  define mallocx je_mallocx
    356 #  define nallocx je_nallocx
    357 #  define posix_memalign je_posix_memalign
    358 #  define rallocx je_rallocx
    359 #  define realloc je_realloc
    360 #  define sallocx je_sallocx
    361 #  define sdallocx je_sdallocx
    362 #  define xallocx je_xallocx
    363 #  define memalign je_memalign
    364 #  define valloc je_valloc
    365 #endif
    366 
    367 /*
    368  * The je_* macros can be used as stable alternative names for the
    369  * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined.  This is primarily
    370  * meant for use in jemalloc itself, but it can be used by application code to
    371  * provide isolation from the name mangling specified via --with-mangling
    372  * and/or --with-jemalloc-prefix.
    373  */
    374 #ifndef JEMALLOC_NO_DEMANGLE
    375 #  undef je_aligned_alloc
    376 #  undef je_calloc
    377 #  undef je_dallocx
    378 #  undef je_free
    379 #  undef je_mallctl
    380 #  undef je_mallctlbymib
    381 #  undef je_mallctlnametomib
    382 #  undef je_malloc
    383 #  undef je_malloc_conf
    384 #  undef je_malloc_message
    385 #  undef je_malloc_stats_print
    386 #  undef je_malloc_usable_size
    387 #  undef je_mallocx
    388 #  undef je_nallocx
    389 #  undef je_posix_memalign
    390 #  undef je_rallocx
    391 #  undef je_realloc
    392 #  undef je_sallocx
    393 #  undef je_sdallocx
    394 #  undef je_xallocx
    395 #  undef je_memalign
    396 #  undef je_valloc
    397 #endif
    398 
    399 #ifdef __cplusplus
    400 }
    401 #endif
    402 #endif /* JEMALLOC_H_ */
    403