1 #ifndef JEMALLOC_INTERNAL_H
2 #define JEMALLOC_INTERNAL_H
6 # define ENOENT ERROR_PATH_NOT_FOUND
7 # define EINVAL ERROR_BAD_ARGUMENTS
8 # define EAGAIN ERROR_OUTOFMEMORY
9 # define EPERM ERROR_WRITE_FAULT
10 # define EFAULT ERROR_INVALID_ADDRESS
11 # define ENOMEM ERROR_NOT_ENOUGH_MEMORY
13 # define ERANGE ERROR_INVALID_DATA
15 # include <sys/param.h>
16 # include <sys/mman.h>
17 # include <sys/syscall.h>
18 # if !defined(SYS_write) && defined(__NR_write)
19 # define SYS_write __NR_write
25 #include <sys/types.h>
29 # define SIZE_T_MAX SIZE_MAX
38 # define offsetof(type, member) ((size_t)&(((type *)NULL)->member))
47 # define PATH_MAX 1024
48 # define STDERR_FILENO 2
49 # define __func__ __FUNCTION__
51 # pragma warning(disable: 4996)
57 #include "jemalloc_defs.h"
59 #ifdef JEMALLOC_UTRACE
60 #include <sys/ktrace.h>
63 #ifdef JEMALLOC_VALGRIND
64 #include <valgrind/valgrind.h>
65 #include <valgrind/memcheck.h>
68 #define JEMALLOC_NO_DEMANGLE
69 #include "../jemalloc.h"
72 #ifdef JEMALLOC_CC_SILENCE
73 #define UNUSED JEMALLOC_ATTR(unused)
100 #ifdef JEMALLOC_LAZY_LOCK
114 #ifdef JEMALLOC_PROF_LIBGCC
121 #ifdef JEMALLOC_PROF_LIBUNWIND
128 #ifdef JEMALLOC_MREMAP
135 #ifdef JEMALLOC_MUNMAP
142 #ifdef JEMALLOC_STATS
149 #ifdef JEMALLOC_TCACHE
163 #ifdef JEMALLOC_UTRACE
170 #ifdef JEMALLOC_VALGRIND
177 #ifdef JEMALLOC_XMALLOC
184 #ifdef JEMALLOC_IVSALLOC
191 #ifdef JEMALLOC_ATOMIC9
192 #include <machine/atomic.h>
195 #if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
196 #include <libkern/OSAtomic.h>
200 #include <mach/mach_error.h>
201 #include <mach/mach_init.h>
202 #include <mach/vm_map.h>
203 #include <malloc/malloc.h>
225 #define JEMALLOC_H_TYPES
227 #ifndef JEMALLOC_HAS_RESTRICT
231 #define MALLOCX_LG_ALIGN_MASK ((int)0x3f)
232 #define ALLOCM_LG_ALIGN_MASK ((int)0x3f)
234 #define ZU(z) ((size_t)z)
235 #define QU(q) ((uint64_t)q)
238 # define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
241 #if defined(JEMALLOC_DEBUG)
243 # define JEMALLOC_ALWAYS_INLINE
244 # define JEMALLOC_ALWAYS_INLINE_C static
245 # define JEMALLOC_INLINE
246 # define JEMALLOC_INLINE_C static
249 # define JEMALLOC_ENABLE_INLINE
250 # ifdef JEMALLOC_HAVE_ATTR
251 # define JEMALLOC_ALWAYS_INLINE \
252 static inline JEMALLOC_ATTR(unused) JEMALLOC_ATTR(always_inline)
253 # define JEMALLOC_ALWAYS_INLINE_C \
254 static inline JEMALLOC_ATTR(always_inline)
256 # define JEMALLOC_ALWAYS_INLINE static inline
257 # define JEMALLOC_ALWAYS_INLINE_C static inline
259 # define JEMALLOC_INLINE static inline
260 # define JEMALLOC_INLINE_C static inline
262 # define inline _inline
267 #define LG_TINY_MIN 3
268 #define TINY_MIN (1U << LG_TINY_MIN)
275 # if (defined(__i386__) || defined(_M_IX86))
276 # define LG_QUANTUM 4
279 # define LG_QUANTUM 4
282 # define LG_QUANTUM 4
285 # define LG_QUANTUM 4
287 # if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64))
288 # define LG_QUANTUM 4
291 # define LG_QUANTUM 3
294 # define LG_QUANTUM 4
297 # define LG_QUANTUM 4
300 # define LG_QUANTUM 3
303 # define LG_QUANTUM 4
306 # define LG_QUANTUM 4
309 # define LG_QUANTUM 4
312 # define LG_QUANTUM 4
315 # error "No LG_QUANTUM definition for architecture; specify via CPPFLAGS"
319 #define QUANTUM ((size_t)(1U << LG_QUANTUM))
320 #define QUANTUM_MASK (QUANTUM - 1)
323 #define QUANTUM_CEILING(a) \
324 (((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
326 #define LONG ((size_t)(1U << LG_SIZEOF_LONG))
327 #define LONG_MASK (LONG - 1)
330 #define LONG_CEILING(a) \
331 (((a) + LONG_MASK) & ~LONG_MASK)
333 #define SIZEOF_PTR (1U << LG_SIZEOF_PTR)
334 #define PTR_MASK (SIZEOF_PTR - 1)
337 #define PTR_CEILING(a) \
338 (((a) + PTR_MASK) & ~PTR_MASK)
347 #define LG_CACHELINE 6
349 #define CACHELINE_MASK (CACHELINE - 1)
352 #define CACHELINE_CEILING(s) \
353 (((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
359 #define LG_PAGE STATIC_PAGE_SHIFT
360 #define PAGE ((size_t)(1U << STATIC_PAGE_SHIFT))
361 #define PAGE_MASK ((size_t)(PAGE - 1))
364 #define PAGE_CEILING(s) \
365 (((s) + PAGE_MASK) & ~PAGE_MASK)
368 #define ALIGNMENT_ADDR2BASE(a, alignment) \
369 ((void *)((uintptr_t)(a) & (-(alignment))))
372 #define ALIGNMENT_ADDR2OFFSET(a, alignment) \
373 ((size_t)((uintptr_t)(a) & (alignment - 1)))
376 #define ALIGNMENT_CEILING(s, alignment) \
377 (((s) + (alignment - 1)) & (-(alignment)))
380 #if __STDC_VERSION__ < 199901L
383 # define alloca _alloca
385 # ifdef JEMALLOC_HAS_ALLOCA_H
391 # define VARIABLE_ARRAY(type, name, count) \
392 type *name = alloca(sizeof(type) * count)
394 # define VARIABLE_ARRAY(type, name, count) type name[count]
397 #ifdef JEMALLOC_VALGRIND
411 #define JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero) do { \
412 if (config_valgrind && opt_valgrind && cond) \
413 VALGRIND_MALLOCLIKE_BLOCK(ptr, usize, p2rz(ptr), zero); \
415 #define JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize, \
416 old_rzsize, zero) do { \
417 if (config_valgrind && opt_valgrind) { \
418 size_t rzsize = p2rz(ptr); \
420 if (ptr == old_ptr) { \
421 VALGRIND_RESIZEINPLACE_BLOCK(ptr, old_usize, \
423 if (zero && old_usize < usize) { \
424 VALGRIND_MAKE_MEM_DEFINED( \
425 (void *)((uintptr_t)ptr + \
426 old_usize), usize - old_usize); \
429 if (old_ptr != NULL) { \
430 VALGRIND_FREELIKE_BLOCK(old_ptr, \
434 size_t copy_size = (old_usize < usize) \
435 ? old_usize : usize; \
436 size_t tail_size = usize - copy_size; \
437 VALGRIND_MALLOCLIKE_BLOCK(ptr, usize, \
439 if (copy_size > 0) { \
440 VALGRIND_MAKE_MEM_DEFINED(ptr, \
443 if (zero && tail_size > 0) { \
444 VALGRIND_MAKE_MEM_DEFINED( \
445 (void *)((uintptr_t)ptr + \
446 copy_size), tail_size); \
452 #define JEMALLOC_VALGRIND_FREE(ptr, rzsize) do { \
453 if (config_valgrind && opt_valgrind) \
454 VALGRIND_FREELIKE_BLOCK(ptr, rzsize); \
457 #define RUNNING_ON_VALGRIND ((unsigned)0)
458 #define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \
460 #define VALGRIND_RESIZEINPLACE_BLOCK(addr, oldSizeB, newSizeB, rzB) \
462 #define VALGRIND_FREELIKE_BLOCK(addr, rzB) do {} while (0)
463 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len) do {} while (0)
464 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len) do {} while (0)
465 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len) do {} while (0)
466 #define JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero) do {} while (0)
467 #define JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize, \
468 old_rzsize, zero) do {} while (0)
469 #define JEMALLOC_VALGRIND_FREE(ptr, rzsize) do {} while (0)
494 #undef JEMALLOC_H_TYPES
496 #define JEMALLOC_H_STRUCTS
528 #define THREAD_ALLOCATED_INITIALIZER JEMALLOC_ARG_CONCAT({0, 0})
530 #undef JEMALLOC_H_STRUCTS
532 #define JEMALLOC_H_EXTERNS
545 extern unsigned ncpus;
590 #undef JEMALLOC_H_EXTERNS
592 #define JEMALLOC_H_INLINES
609 #ifndef JEMALLOC_ENABLE_INLINE
610 malloc_tsd_protos(JEMALLOC_ATTR(unused),
arenas, arena_t *)
612 size_t
s2u(
size_t size);
613 size_t sa2u(
size_t size,
size_t alignment);
618 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
631 JEMALLOC_ALWAYS_INLINE
size_t
635 if (size <= SMALL_MAXCLASS)
639 return (CHUNK_CEILING(size));
647 sa2u(
size_t size,
size_t alignment)
651 assert(alignment != 0 && ((alignment - 1) & alignment) == 0);
678 if (usize <= SMALL_MAXCLASS)
702 if (usize < size || usize + alignment < usize) {
713 run_size = usize + alignment -
PAGE;
716 return (CHUNK_CEILING(usize));
756 #define JEMALLOC_ARENA_INLINE_A
758 #undef JEMALLOC_ARENA_INLINE_A
760 #define JEMALLOC_ARENA_INLINE_B
762 #undef JEMALLOC_ARENA_INLINE_B
766 #ifndef JEMALLOC_ENABLE_INLINE
767 void *
imalloct(
size_t size,
bool try_tcache, arena_t *arena);
769 void *
icalloct(
size_t size,
bool try_tcache, arena_t *arena);
771 void *
ipalloct(
size_t usize,
size_t alignment,
bool zero,
bool try_tcache,
773 void *
ipalloc(
size_t usize,
size_t alignment,
bool zero);
774 size_t isalloc(
const void *ptr,
bool demote);
775 size_t ivsalloc(
const void *ptr,
bool demote);
776 size_t u2rz(
size_t usize);
777 size_t p2rz(
const void *ptr);
778 void idalloct(
void *ptr,
bool try_tcache);
780 void iqalloct(
void *ptr,
bool try_tcache);
782 void *
iralloct_realign(
void *ptr,
size_t oldsize,
size_t size,
size_t extra,
783 size_t alignment,
bool zero,
bool try_tcache_alloc,
bool try_tcache_dalloc,
785 void *
iralloct(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
786 bool zero,
bool try_tcache_alloc,
bool try_tcache_dalloc, arena_t *arena);
787 void *
iralloc(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
789 bool ixalloc(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
794 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
796 imalloct(
size_t size,
bool try_tcache, arena_t *arena)
815 icalloct(
size_t size,
bool try_tcache, arena_t *arena)
838 assert(usize ==
sa2u(usize, alignment));
872 arena_chunk_t *
chunk;
878 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
903 if (usize <= SMALL_MAXCLASS) {
904 size_t binind = SMALL_SIZE2BIN(usize);
915 size_t usize =
isalloc(ptr,
false);
917 return (
u2rz(usize));
923 arena_chunk_t *
chunk;
927 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
960 size_t alignment,
bool zero,
bool try_tcache_alloc,
bool try_tcache_dalloc,
964 size_t usize, copysize;
966 usize =
sa2u(size + extra, alignment);
969 p =
ipalloct(usize, alignment, zero, try_tcache_alloc, arena);
974 usize =
sa2u(size, alignment);
977 p =
ipalloct(usize, alignment, zero, try_tcache_alloc, arena);
985 copysize = (size < oldsize) ? size : oldsize;
986 memcpy(p, ptr, copysize);
992 iralloct(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
bool zero,
993 bool try_tcache_alloc,
bool try_tcache_dalloc, arena_t *arena)
1009 zero, try_tcache_alloc, try_tcache_dalloc, arena));
1014 alignment, zero, try_tcache_alloc,
1015 try_tcache_dalloc));
1023 iralloc(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
bool zero)
1026 return (
iralloct(ptr, size, extra, alignment, zero,
true,
true,
NULL));
1030 ixalloc(
void *ptr,
size_t size,
size_t extra,
size_t alignment,
bool zero)
1034 assert(ptr !=
NULL);
1057 #undef JEMALLOC_H_INLINES
#define malloc_mutex_lock
Definition: private_namespace.h:237
#define malloc_tsd_no_cleanup
Definition: private_namespace.h:249
JEMALLOC_ALWAYS_INLINE void * iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra, size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena)
Definition: jemalloc_internal.h:959
JEMALLOC_ALWAYS_INLINE void iqalloct(void *ptr, bool try_tcache)
Definition: jemalloc_internal.h:942
JEMALLOC_ALWAYS_INLINE void * imalloct(size_t size, bool try_tcache, arena_t *arena)
Definition: jemalloc_internal.h:796
#define huge_ralloc_no_move
Definition: private_namespace.h:212
void jemalloc_postfork_parent(void)
#define PAGE_CEILING(s)
Definition: jemalloc_internal.h:364
#define JEMALLOC_INLINE
Definition: jemalloc_internal.h:259
JEMALLOC_ALWAYS_INLINE size_t ivsalloc(const void *ptr, bool demote)
Definition: jemalloc_internal.h:888
#define huge_dss_prec_get
Definition: private_namespace.h:200
static const bool config_stats
Definition: jemalloc_internal.h:141
JEMALLOC_ALWAYS_INLINE void * imalloc(size_t size)
Definition: jemalloc_internal.h:808
#define arena_ralloc_no_move
Definition: private_namespace.h:57
JEMALLOC_INLINE size_t p2rz(const void *ptr)
Definition: jemalloc_internal.h:913
static const bool config_dss
Definition: jemalloc_internal.h:85
JEMALLOC_INLINE unsigned narenas_total_get(void)
Definition: jemalloc_internal.h:721
#define ALIGNMENT_ADDR2BASE(a, alignment)
Definition: jemalloc_internal.h:368
#define arena_dalloc
Definition: private_namespace.h:8
#define huge_dalloc
Definition: private_namespace.h:198
thread_allocated_t
Definition: jemalloc_internal.h:1051
#define arenas_tsd_get
Definition: private_namespace.h:73
#define chunksize
Definition: private_namespace.h:126
JEMALLOC_ALWAYS_INLINE size_t isalloc(const void *ptr, bool demote)
Definition: jemalloc_internal.h:869
malloc_mutex_t arenas_lock
JEMALLOC_ALWAYS_INLINE void * ipalloct(size_t usize, size_t alignment, bool zero, bool try_tcache, arena_t *arena)
Definition: jemalloc_internal.h:832
JEMALLOC_ALWAYS_INLINE void idalloct(void *ptr, bool try_tcache)
Definition: jemalloc_internal.h:921
arena_t NULL
Definition: jemalloc_internal.h:624
#define rtree_get
Definition: private_namespace.h:339
#define arena_maxclass
Definition: private_namespace.h:40
JEMALLOC_INLINE arena_t * choose_arena(arena_t *arena)
Definition: jemalloc_internal.h:734
JEMALLOC_ALWAYS_INLINE void * ipalloc(size_t usize, size_t alignment, bool zero)
Definition: jemalloc_internal.h:857
JEMALLOC_ALWAYS_INLINE size_t sa2u(size_t size, size_t alignment)
Definition: jemalloc_internal.h:647
#define arena_bin_info
Definition: private_namespace.h:6
static const bool config_utrace
Definition: jemalloc_internal.h:162
JEMALLOC_ALWAYS_INLINE void * icalloc(size_t size)
Definition: jemalloc_internal.h:825
JEMALLOC_ALWAYS_INLINE void iqalloc(void *ptr)
Definition: jemalloc_internal.h:952
arena_t arenas_cleanup JEMALLOC_ALWAYS_INLINE size_t s2u(size_t size)
Definition: jemalloc_internal.h:632
#define arena_malloc
Definition: private_namespace.h:18
static const bool config_fill
Definition: jemalloc_internal.h:92
_W64 unsigned int uintptr_t
Definition: stdint.h:119
JEMALLOC_ALWAYS_INLINE void * iralloct(void *ptr, size_t size, size_t extra, size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena)
Definition: jemalloc_internal.h:992
#define huge_palloc
Definition: private_namespace.h:205
static const bool config_tcache
Definition: jemalloc_internal.h:148
#define huge_ralloc
Definition: private_namespace.h:211
static const bool config_munmap
Definition: jemalloc_internal.h:134
unsigned __int64 uint64_t
Definition: stdint.h:90
void arenas_cleanup(void *arg)
uint64_t deallocated
Definition: jemalloc_internal.h:522
static const bool config_debug
Definition: jemalloc_internal.h:78
#define huge_malloc
Definition: private_namespace.h:201
thread_allocated
Definition: jemalloc_internal.h:1051
arena_t * choose_arena_hard(void)
static const bool config_ivsalloc
Definition: jemalloc_internal.h:183
#define chunks_rtree
Definition: private_namespace.h:125
void jemalloc_postfork_child(void)
JEMALLOC_ALWAYS_INLINE void idalloc(void *ptr)
Definition: jemalloc_internal.h:935
#define malloc_mutex_unlock
Definition: private_namespace.h:241
#define arena_palloc
Definition: private_namespace.h:42
#define JEMALLOC_ALWAYS_INLINE
Definition: jemalloc_internal.h:256
#define arena_salloc
Definition: private_namespace.h:60
arena_t * arenas_extend(unsigned ind)
Definition: jemalloc_internal.h:520
#define arena_ralloc
Definition: private_namespace.h:55
JEMALLOC_ALWAYS_INLINE void * icalloct(size_t size, bool try_tcache, arena_t *arena)
Definition: jemalloc_internal.h:815
internal::NamedArg< char > arg(StringRef name, const T &arg)
Definition: format.h:3248
#define THREAD_ALLOCATED_INITIALIZER
Definition: jemalloc_internal.h:528
static const bool config_mremap
Definition: jemalloc_internal.h:127
static const bool config_xmalloc
Definition: jemalloc_internal.h:176
JEMALLOC_ALWAYS_INLINE void * iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero)
Definition: jemalloc_internal.h:1023
static const bool config_valgrind
Definition: jemalloc_internal.h:169
static const bool config_prof
Definition: jemalloc_internal.h:106
malloc_tsd_externs(arenas, arena_t *) malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE
static unorm16 zero()
Definition: unorm16.h:82
static const bool config_tls
Definition: jemalloc_internal.h:155
_W64 signed int intptr_t
Definition: stdint.h:118
JEMALLOC_INLINE size_t u2rz(size_t usize)
Definition: jemalloc_internal.h:899
static const bool config_lazy_lock
Definition: jemalloc_internal.h:99
#define quarantine
Definition: private_namespace.h:323
#define huge_salloc
Definition: private_namespace.h:213
static const bool config_prof_libgcc
Definition: jemalloc_internal.h:113
void jemalloc_prefork(void)
#define PAGE
Definition: jemalloc_internal.h:360
uint64_t allocated
Definition: jemalloc_internal.h:521
#define ALIGNMENT_CEILING(s, alignment)
Definition: jemalloc_internal.h:376
arena_t ** arenas
Definition: jemalloc_internal.h:624
static const bool config_prof_libunwind
Definition: jemalloc_internal.h:120
JEMALLOC_ALWAYS_INLINE bool ixalloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero)
Definition: jemalloc_internal.h:1030