5 #include <sys/sysctl.h>
11 # define SIZE_T_MAX SIZE_MAX
22 # define offsetof(type, member) ((size_t)&(((type *)NULL)->member))
33 #define JEMALLOC_MANGLE
34 #include "../jemalloc@install_suffix@.h"
36 #if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))
37 #include <libkern/OSAtomic.h>
41 #include <mach/mach_error.h>
42 #include <mach/mach_init.h>
43 #include <mach/vm_map.h>
44 #include <malloc/malloc.h>
47 #ifdef JEMALLOC_LAZY_LOCK
52 #include "jemalloc/internal/rb.h"
53 #include "jemalloc/internal/qr.h"
54 #include "jemalloc/internal/ql.h"
56 extern void (*JEMALLOC_P(malloc_message
))(void *wcbopaque
, const char *s
);
59 * Define a custom assert() in order to reduce the chances of deadlock during
63 # ifdef JEMALLOC_DEBUG
64 # define assert(e) do { \
66 char line_buf[UMAX2S_BUFSIZE]; \
67 malloc_write("<jemalloc>: "); \
68 malloc_write(__FILE__); \
70 malloc_write(u2s(__LINE__, 10, line_buf)); \
71 malloc_write(": Failed assertion: "); \
74 malloc_write("\"\n"); \
84 # define dassert(e) assert(e)
90 * jemalloc can conceptually be broken into components (arena, tcache, etc.),
91 * but there are circular dependencies that cannot be broken without
92 * substantial performance degradation. In order to reduce the effect on
93 * visual code flow, read the header files in multiple passes, with one of the
94 * following cpp variables defined during each pass:
96 * JEMALLOC_H_TYPES : Preprocessor-defined constants and psuedo-opaque data
98 * JEMALLOC_H_STRUCTS : Data structures.
99 * JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.
100 * JEMALLOC_H_INLINES : Inline functions.
102 /******************************************************************************/
103 #define JEMALLOC_H_TYPES
105 #define ALLOCM_LG_ALIGN_MASK ((int)0x3f)
107 #define ZU(z) ((size_t)z)
110 # define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
113 #ifdef JEMALLOC_DEBUG
114 /* Disable inlining to make debugging easier. */
115 # define JEMALLOC_INLINE
118 # define JEMALLOC_ENABLE_INLINE
119 # define JEMALLOC_INLINE static inline
122 /* Size of stack-allocated buffer passed to buferror(). */
123 #define BUFERROR_BUF 64
125 /* Minimum alignment of allocations is 2^LG_QUANTUM bytes. */
127 # define LG_QUANTUM 4
130 # define LG_QUANTUM 4
133 # define LG_QUANTUM 4
136 # define LG_QUANTUM 4
138 #if (defined(__amd64__) || defined(__x86_64__))
139 # define LG_QUANTUM 4
142 # define LG_QUANTUM 3
145 # define LG_QUANTUM 3
148 # define LG_QUANTUM 4
151 # define LG_QUANTUM 4
154 #define QUANTUM ((size_t)(1U << LG_QUANTUM))
155 #define QUANTUM_MASK (QUANTUM - 1)
157 /* Return the smallest quantum multiple that is >= a. */
158 #define QUANTUM_CEILING(a) \
159 (((a) + QUANTUM_MASK) & ~QUANTUM_MASK)
161 #define LONG ((size_t)(1U << LG_SIZEOF_LONG))
162 #define LONG_MASK (LONG - 1)
164 /* Return the smallest long multiple that is >= a. */
165 #define LONG_CEILING(a) \
166 (((a) + LONG_MASK) & ~LONG_MASK)
168 #define SIZEOF_PTR (1U << LG_SIZEOF_PTR)
169 #define PTR_MASK (SIZEOF_PTR - 1)
171 /* Return the smallest (void *) multiple that is >= a. */
172 #define PTR_CEILING(a) \
173 (((a) + PTR_MASK) & ~PTR_MASK)
176 * Maximum size of L1 cache line. This is used to avoid cache line aliasing.
177 * In addition, this controls the spacing of cacheline-spaced size classes.
179 #define LG_CACHELINE 6
180 #define CACHELINE ((size_t)(1U << LG_CACHELINE))
181 #define CACHELINE_MASK (CACHELINE - 1)
183 /* Return the smallest cacheline multiple that is >= s. */
184 #define CACHELINE_CEILING(s) \
185 (((s) + CACHELINE_MASK) & ~CACHELINE_MASK)
188 * Page size. STATIC_PAGE_SHIFT is determined by the configure script. If
189 * DYNAMIC_PAGE_SHIFT is enabled, only use the STATIC_PAGE_* macros where
190 * compile-time values are required for the purposes of defining data
193 #define STATIC_PAGE_SIZE ((size_t)(1U << STATIC_PAGE_SHIFT))
194 #define STATIC_PAGE_MASK ((size_t)(STATIC_PAGE_SIZE - 1))
206 #ifdef DYNAMIC_PAGE_SHIFT
207 # define PAGE_SHIFT lg_pagesize
208 # define PAGE_SIZE pagesize
209 # define PAGE_MASK pagesize_mask
211 # define PAGE_SHIFT STATIC_PAGE_SHIFT
212 # define PAGE_SIZE STATIC_PAGE_SIZE
213 # define PAGE_MASK STATIC_PAGE_MASK
216 /* Return the smallest pagesize multiple that is >= s. */
217 #define PAGE_CEILING(s) \
218 (((s) + PAGE_MASK) & ~PAGE_MASK)
220 #include "jemalloc/internal/atomic.h"
221 #include "jemalloc/internal/prn.h"
222 #include "jemalloc/internal/ckh.h"
223 #include "jemalloc/internal/stats.h"
224 #include "jemalloc/internal/ctl.h"
225 #include "jemalloc/internal/mutex.h"
226 #include "jemalloc/internal/mb.h"
227 #include "jemalloc/internal/extent.h"
228 #include "jemalloc/internal/arena.h"
229 #include "jemalloc/internal/bitmap.h"
230 #include "jemalloc/internal/base.h"
231 #include "jemalloc/internal/chunk.h"
232 #include "jemalloc/internal/huge.h"
233 #include "jemalloc/internal/rtree.h"
234 #include "jemalloc/internal/tcache.h"
235 #include "jemalloc/internal/hash.h"
237 #include "jemalloc/internal/zone.h"
239 #include "jemalloc/internal/prof.h"
241 #undef JEMALLOC_H_TYPES
242 /******************************************************************************/
243 #define JEMALLOC_H_STRUCTS
245 #include "jemalloc/internal/atomic.h"
246 #include "jemalloc/internal/prn.h"
247 #include "jemalloc/internal/ckh.h"
248 #include "jemalloc/internal/stats.h"
249 #include "jemalloc/internal/ctl.h"
250 #include "jemalloc/internal/mutex.h"
251 #include "jemalloc/internal/mb.h"
252 #include "jemalloc/internal/bitmap.h"
253 #include "jemalloc/internal/extent.h"
254 #include "jemalloc/internal/arena.h"
255 #include "jemalloc/internal/base.h"
256 #include "jemalloc/internal/chunk.h"
257 #include "jemalloc/internal/huge.h"
258 #include "jemalloc/internal/rtree.h"
259 #include "jemalloc/internal/tcache.h"
260 #include "jemalloc/internal/hash.h"
262 #include "jemalloc/internal/zone.h"
264 #include "jemalloc/internal/prof.h"
266 #ifdef JEMALLOC_STATS
269 uint64_t deallocated
;
270 } thread_allocated_t
;
273 #undef JEMALLOC_H_STRUCTS
274 /******************************************************************************/
275 #define JEMALLOC_H_EXTERNS
277 extern bool opt_abort
;
279 extern bool opt_junk
;
282 extern bool opt_sysv
;
284 #ifdef JEMALLOC_XMALLOC
285 extern bool opt_xmalloc
;
288 extern bool opt_zero
;
290 extern size_t opt_narenas
;
292 #ifdef DYNAMIC_PAGE_SHIFT
293 extern size_t pagesize
;
294 extern size_t pagesize_mask
;
295 extern size_t lg_pagesize
;
298 /* Number of CPUs. */
299 extern unsigned ncpus
;
301 extern malloc_mutex_t arenas_lock
; /* Protects arenas initialization. */
302 extern pthread_key_t arenas_tsd
;
305 * Map of pthread_self() --> arenas[???], used for selecting an arena to use
308 extern __thread arena_t
*arenas_tls
JEMALLOC_ATTR(tls_model("initial-exec"));
309 # define ARENA_GET() arenas_tls
310 # define ARENA_SET(v) do { \
312 pthread_setspecific(arenas_tsd, (void *)(v)); \
315 # define ARENA_GET() ((arena_t *)pthread_getspecific(arenas_tsd))
316 # define ARENA_SET(v) do { \
317 pthread_setspecific(arenas_tsd, (void *)(v)); \
322 * Arenas that are used to service external requests. Not all elements of the
323 * arenas array are necessarily used; arenas are created lazily as needed.
325 extern arena_t
**arenas
;
326 extern unsigned narenas
;
328 #ifdef JEMALLOC_STATS
330 extern __thread thread_allocated_t thread_allocated_tls
;
331 # define ALLOCATED_GET() (thread_allocated_tls.allocated)
332 # define ALLOCATEDP_GET() (&thread_allocated_tls.allocated)
333 # define DEALLOCATED_GET() (thread_allocated_tls.deallocated)
334 # define DEALLOCATEDP_GET() (&thread_allocated_tls.deallocated)
335 # define ALLOCATED_ADD(a, d) do { \
336 thread_allocated_tls.allocated += a; \
337 thread_allocated_tls.deallocated += d; \
340 extern pthread_key_t thread_allocated_tsd
;
341 thread_allocated_t
*thread_allocated_get_hard(void);
343 # define ALLOCATED_GET() (thread_allocated_get()->allocated)
344 # define ALLOCATEDP_GET() (&thread_allocated_get()->allocated)
345 # define DEALLOCATED_GET() (thread_allocated_get()->deallocated)
346 # define DEALLOCATEDP_GET() (&thread_allocated_get()->deallocated)
347 # define ALLOCATED_ADD(a, d) do { \
348 thread_allocated_t *thread_allocated = thread_allocated_get(); \
349 thread_allocated->allocated += (a); \
350 thread_allocated->deallocated += (d); \
355 arena_t
*arenas_extend(unsigned ind
);
356 arena_t
*choose_arena_hard(void);
357 int buferror(int errnum
, char *buf
, size_t buflen
);
358 void jemalloc_prefork(void);
359 void jemalloc_postfork(void);
361 #include "jemalloc/internal/atomic.h"
362 #include "jemalloc/internal/prn.h"
363 #include "jemalloc/internal/ckh.h"
364 #include "jemalloc/internal/stats.h"
365 #include "jemalloc/internal/ctl.h"
366 #include "jemalloc/internal/mutex.h"
367 #include "jemalloc/internal/mb.h"
368 #include "jemalloc/internal/bitmap.h"
369 #include "jemalloc/internal/extent.h"
370 #include "jemalloc/internal/arena.h"
371 #include "jemalloc/internal/base.h"
372 #include "jemalloc/internal/chunk.h"
373 #include "jemalloc/internal/huge.h"
374 #include "jemalloc/internal/rtree.h"
375 #include "jemalloc/internal/tcache.h"
376 #include "jemalloc/internal/hash.h"
378 #include "jemalloc/internal/zone.h"
380 #include "jemalloc/internal/prof.h"
382 #undef JEMALLOC_H_EXTERNS
383 /******************************************************************************/
384 #define JEMALLOC_H_INLINES
386 #include "jemalloc/internal/atomic.h"
387 #include "jemalloc/internal/prn.h"
388 #include "jemalloc/internal/ckh.h"
389 #include "jemalloc/internal/stats.h"
390 #include "jemalloc/internal/ctl.h"
391 #include "jemalloc/internal/mutex.h"
392 #include "jemalloc/internal/mb.h"
393 #include "jemalloc/internal/extent.h"
394 #include "jemalloc/internal/base.h"
395 #include "jemalloc/internal/chunk.h"
396 #include "jemalloc/internal/huge.h"
398 #ifndef JEMALLOC_ENABLE_INLINE
399 size_t pow2_ceil(size_t x
);
400 size_t s2u(size_t size
);
401 size_t sa2u(size_t size
, size_t alignment
, size_t *run_size_p
);
402 void malloc_write(const char *s
);
403 arena_t
*choose_arena(void);
404 # if (defined(JEMALLOC_STATS) && defined(NO_TLS))
405 thread_allocated_t
*thread_allocated_get(void);
409 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
410 /* Compute the smallest power of 2 that is >= x. */
411 JEMALLOC_INLINE
size_t
421 #if (LG_SIZEOF_PTR == 3)
429 * Compute usable size that would result from allocating an object with the
432 JEMALLOC_INLINE
size_t
436 if (size
<= small_maxclass
)
437 return (arena_bin_info
[SMALL_SIZE2BIN(size
)].reg_size
);
438 if (size
<= arena_maxclass
)
439 return (PAGE_CEILING(size
));
440 return (CHUNK_CEILING(size
));
444 * Compute usable size that would result from allocating an object with the
445 * specified size and alignment.
447 JEMALLOC_INLINE
size_t
448 sa2u(size_t size
, size_t alignment
, size_t *run_size_p
)
453 * Round size up to the nearest multiple of alignment.
455 * This done, we can take advantage of the fact that for each small
456 * size class, every object is aligned at the smallest power of two
457 * that is non-zero in the base two representation of the size. For
460 * Size | Base 2 | Minimum alignment
461 * -----+----------+------------------
463 * 144 | 10100000 | 32
464 * 192 | 11000000 | 64
466 * Depending on runtime settings, it is possible that arena_malloc()
467 * will further round up to a power of two, but that never causes
468 * correctness issues.
470 usize
= (size
+ (alignment
- 1)) & (-alignment
);
472 * (usize < size) protects against the combination of maximal
473 * alignment and size greater than maximal alignment.
476 /* size_t overflow. */
480 if (usize
<= arena_maxclass
&& alignment
<= PAGE_SIZE
) {
481 if (usize
<= small_maxclass
)
482 return (arena_bin_info
[SMALL_SIZE2BIN(usize
)].reg_size
);
483 return (PAGE_CEILING(usize
));
488 * We can't achieve subpage alignment, so round up alignment
489 * permanently; it makes later calculations simpler.
491 alignment
= PAGE_CEILING(alignment
);
492 usize
= PAGE_CEILING(size
);
494 * (usize < size) protects against very large sizes within
495 * PAGE_SIZE of SIZE_T_MAX.
497 * (usize + alignment < usize) protects against the
498 * combination of maximal alignment and usize large enough
499 * to cause overflow. This is similar to the first overflow
500 * check above, but it needs to be repeated due to the new
501 * usize value, which may now be *equal* to maximal
502 * alignment, whereas before we only detected overflow if the
503 * original size was *greater* than maximal alignment.
505 if (usize
< size
|| usize
+ alignment
< usize
) {
506 /* size_t overflow. */
511 * Calculate the size of the over-size run that arena_palloc()
512 * would need to allocate in order to guarantee the alignment.
514 if (usize
>= alignment
)
515 run_size
= usize
+ alignment
- PAGE_SIZE
;
518 * It is possible that (alignment << 1) will cause
519 * overflow, but it doesn't matter because we also
520 * subtract PAGE_SIZE, which in the case of overflow
521 * leaves us with a very large run_size. That causes
522 * the first conditional below to fail, which means
523 * that the bogus run_size value never gets used for
524 * anything important.
526 run_size
= (alignment
<< 1) - PAGE_SIZE
;
528 if (run_size_p
!= NULL
)
529 *run_size_p
= run_size
;
531 if (run_size
<= arena_maxclass
)
532 return (PAGE_CEILING(usize
));
533 return (CHUNK_CEILING(usize
));
538 * Wrapper around malloc_message() that avoids the need for
539 * JEMALLOC_P(malloc_message)(...) throughout the code.
542 malloc_write(const char *s
)
545 JEMALLOC_P(malloc_message
)(NULL
, s
);
549 * Choose an arena based on a per-thread value (fast-path code, calls slow-path
550 * code if necessary).
552 JEMALLOC_INLINE arena_t
*
559 ret
= choose_arena_hard();
566 #if (defined(JEMALLOC_STATS) && defined(NO_TLS))
567 JEMALLOC_INLINE thread_allocated_t
*
568 thread_allocated_get(void)
570 thread_allocated_t
*thread_allocated
= (thread_allocated_t
*)
571 pthread_getspecific(thread_allocated_tsd
);
573 if (thread_allocated
== NULL
)
574 return (thread_allocated_get_hard());
575 return (thread_allocated
);
580 #include "jemalloc/internal/bitmap.h"
581 #include "jemalloc/internal/rtree.h"
582 #include "jemalloc/internal/tcache.h"
583 #include "jemalloc/internal/arena.h"
584 #include "jemalloc/internal/hash.h"
586 #include "jemalloc/internal/zone.h"
589 #ifndef JEMALLOC_ENABLE_INLINE
590 void *imalloc(size_t size
);
591 void *icalloc(size_t size
);
592 void *ipalloc(size_t usize
, size_t alignment
, bool zero
);
593 size_t isalloc(const void *ptr
);
594 # ifdef JEMALLOC_IVSALLOC
595 size_t ivsalloc(const void *ptr
);
597 void idalloc(void *ptr
);
598 void *iralloc(void *ptr
, size_t size
, size_t extra
, size_t alignment
,
599 bool zero
, bool no_move
);
602 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_))
603 JEMALLOC_INLINE
void *
609 if (size
<= arena_maxclass
)
610 return (arena_malloc(size
, false));
612 return (huge_malloc(size
, false));
615 JEMALLOC_INLINE
void *
619 if (size
<= arena_maxclass
)
620 return (arena_malloc(size
, true));
622 return (huge_malloc(size
, true));
625 JEMALLOC_INLINE
void *
626 ipalloc(size_t usize
, size_t alignment
, bool zero
)
631 assert(usize
== sa2u(usize
, alignment
, NULL
));
633 if (usize
<= arena_maxclass
&& alignment
<= PAGE_SIZE
)
634 ret
= arena_malloc(usize
, zero
);
639 * Ideally we would only ever call sa2u() once per aligned
640 * allocation request, and the caller of this function has
641 * already done so once. However, it's rather burdensome to
642 * require every caller to pass in run_size, especially given
643 * that it's only relevant to large allocations. Therefore,
644 * just call it again here in order to get run_size.
646 sa2u(usize
, alignment
, &run_size
);
647 if (run_size
<= arena_maxclass
) {
648 ret
= arena_palloc(choose_arena(), usize
, run_size
,
650 } else if (alignment
<= chunksize
)
651 ret
= huge_malloc(usize
, zero
);
653 ret
= huge_palloc(usize
, alignment
, zero
);
656 assert(((uintptr_t)ret
& (alignment
- 1)) == 0);
660 JEMALLOC_INLINE
size_t
661 isalloc(const void *ptr
)
664 arena_chunk_t
*chunk
;
668 chunk
= (arena_chunk_t
*)CHUNK_ADDR2BASE(ptr
);
671 dassert(chunk
->arena
->magic
== ARENA_MAGIC
);
674 ret
= arena_salloc_demote(ptr
);
676 ret
= arena_salloc(ptr
);
679 ret
= huge_salloc(ptr
);
684 #ifdef JEMALLOC_IVSALLOC
685 JEMALLOC_INLINE
size_t
686 ivsalloc(const void *ptr
)
689 /* Return 0 if ptr is not within a chunk managed by jemalloc. */
690 if (rtree_get(chunks_rtree
, (uintptr_t)CHUNK_ADDR2BASE(ptr
)) == NULL
)
693 return (isalloc(ptr
));
700 arena_chunk_t
*chunk
;
704 chunk
= (arena_chunk_t
*)CHUNK_ADDR2BASE(ptr
);
706 arena_dalloc(chunk
->arena
, chunk
, ptr
);
708 huge_dalloc(ptr
, true);
711 JEMALLOC_INLINE
void *
712 iralloc(void *ptr
, size_t size
, size_t extra
, size_t alignment
, bool zero
,
721 oldsize
= isalloc(ptr
);
723 if (alignment
!= 0 && ((uintptr_t)ptr
& ((uintptr_t)alignment
-1))
725 size_t usize
, copysize
;
728 * Existing object alignment is inadquate; allocate new space
733 usize
= sa2u(size
+ extra
, alignment
, NULL
);
736 ret
= ipalloc(usize
, alignment
, zero
);
740 /* Try again, without extra this time. */
741 usize
= sa2u(size
, alignment
, NULL
);
744 ret
= ipalloc(usize
, alignment
, zero
);
749 * Copy at most size bytes (not size+extra), since the caller
750 * has no expectation that the extra bytes will be reliably
753 copysize
= (size
< oldsize
) ? size
: oldsize
;
754 memcpy(ret
, ptr
, copysize
);
760 if (size
<= arena_maxclass
) {
761 return (arena_ralloc_no_move(ptr
, oldsize
, size
,
764 return (huge_ralloc_no_move(ptr
, oldsize
, size
,
768 if (size
+ extra
<= arena_maxclass
) {
769 return (arena_ralloc(ptr
, oldsize
, size
, extra
,
772 return (huge_ralloc(ptr
, oldsize
, size
, extra
,
779 #include "jemalloc/internal/prof.h"
781 #undef JEMALLOC_H_INLINES
782 /******************************************************************************/