jemalloc_internal.h.in revision a4915851577c948cf9051d60152944ca2d1b2d59
1#ifndef JEMALLOC_INTERNAL_H 2#define JEMALLOC_INTERNAL_H 3#include <math.h> 4#ifdef _WIN32 5# include <windows.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 12# undef ERANGE 13# define ERANGE ERROR_INVALID_DATA 14#else 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 20# endif 21# include <sys/uio.h> 22# include <pthread.h> 23# include <errno.h> 24#endif 25#include <sys/types.h> 26 27#include <limits.h> 28#ifndef SIZE_T_MAX 29# define SIZE_T_MAX SIZE_MAX 30#endif 31#include <stdarg.h> 32#include <stdbool.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <stdint.h> 36#include <stddef.h> 37#ifndef offsetof 38# define offsetof(type, member) ((size_t)&(((type *)NULL)->member)) 39#endif 40#include <inttypes.h> 41#include <string.h> 42#include <strings.h> 43#include <ctype.h> 44#ifdef _MSC_VER 45# include <io.h> 46typedef intptr_t ssize_t; 47# define PATH_MAX 1024 48# define STDERR_FILENO 2 49# define __func__ __FUNCTION__ 50/* Disable warnings about deprecated system functions */ 51# pragma warning(disable: 4996) 52#else 53# include <unistd.h> 54#endif 55#include <fcntl.h> 56 57#define JEMALLOC_NO_DEMANGLE 58#include "../jemalloc@install_suffix@.h" 59 60#ifdef JEMALLOC_UTRACE 61#include <sys/ktrace.h> 62#endif 63 64#ifdef JEMALLOC_VALGRIND 65#include <valgrind/valgrind.h> 66#include <valgrind/memcheck.h> 67#endif 68 69#include "jemalloc/internal/private_namespace.h" 70 71#ifdef JEMALLOC_CC_SILENCE 72#define UNUSED JEMALLOC_ATTR(unused) 73#else 74#define UNUSED 75#endif 76 77static const bool config_debug = 78#ifdef JEMALLOC_DEBUG 79 true 80#else 81 false 82#endif 83 ; 84static const bool config_dss = 85#ifdef JEMALLOC_DSS 86 true 87#else 88 false 89#endif 90 ; 91static const bool config_fill = 92#ifdef JEMALLOC_FILL 93 true 94#else 95 false 96#endif 97 ; 98static const bool config_lazy_lock = 99#ifdef JEMALLOC_LAZY_LOCK 100 true 101#else 102 false 103#endif 104 ; 105static const bool config_prof = 106#ifdef JEMALLOC_PROF 107 true 108#else 109 false 110#endif 111 ; 112static const bool config_prof_libgcc = 113#ifdef JEMALLOC_PROF_LIBGCC 114 true 115#else 116 false 117#endif 118 ; 119static const bool config_prof_libunwind = 120#ifdef JEMALLOC_PROF_LIBUNWIND 121 true 122#else 123 false 124#endif 125 ; 126static const bool config_mremap = 127#ifdef JEMALLOC_MREMAP 128 true 129#else 130 false 131#endif 132 ; 133static const bool config_munmap = 134#ifdef JEMALLOC_MUNMAP 135 true 136#else 137 false 138#endif 139 ; 140static const bool config_stats = 141#ifdef JEMALLOC_STATS 142 true 143#else 144 false 145#endif 146 ; 147static const bool config_tcache = 148#ifdef JEMALLOC_TCACHE 149 true 150#else 151 false 152#endif 153 ; 154static const bool config_tls = 155#ifdef JEMALLOC_TLS 156 true 157#else 158 false 159#endif 160 ; 161static const bool config_utrace = 162#ifdef JEMALLOC_UTRACE 163 true 164#else 165 false 166#endif 167 ; 168static const bool config_valgrind = 169#ifdef JEMALLOC_VALGRIND 170 true 171#else 172 false 173#endif 174 ; 175static const bool config_xmalloc = 176#ifdef JEMALLOC_XMALLOC 177 true 178#else 179 false 180#endif 181 ; 182static const bool config_ivsalloc = 183#ifdef JEMALLOC_IVSALLOC 184 true 185#else 186 false 187#endif 188 ; 189 190#ifdef JEMALLOC_ATOMIC9 191#include <machine/atomic.h> 192#endif 193 194#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN)) 195#include <libkern/OSAtomic.h> 196#endif 197 198#ifdef JEMALLOC_ZONE 199#include <mach/mach_error.h> 200#include <mach/mach_init.h> 201#include <mach/vm_map.h> 202#include <malloc/malloc.h> 203#endif 204 205#define RB_COMPACT 206#include "jemalloc/internal/rb.h" 207#include "jemalloc/internal/qr.h" 208#include "jemalloc/internal/ql.h" 209 210/* 211 * jemalloc can conceptually be broken into components (arena, tcache, etc.), 212 * but there are circular dependencies that cannot be broken without 213 * substantial performance degradation. In order to reduce the effect on 214 * visual code flow, read the header files in multiple passes, with one of the 215 * following cpp variables defined during each pass: 216 * 217 * JEMALLOC_H_TYPES : Preprocessor-defined constants and psuedo-opaque data 218 * types. 219 * JEMALLOC_H_STRUCTS : Data structures. 220 * JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes. 221 * JEMALLOC_H_INLINES : Inline functions. 222 */ 223/******************************************************************************/ 224#define JEMALLOC_H_TYPES 225 226#define ALLOCM_LG_ALIGN_MASK ((int)0x3f) 227 228#define ZU(z) ((size_t)z) 229#define QU(q) ((uint64_t)q) 230 231#ifndef __DECONST 232# define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var)) 233#endif 234 235#ifdef JEMALLOC_DEBUG 236 /* Disable inlining to make debugging easier. */ 237# define JEMALLOC_ALWAYS_INLINE 238# define JEMALLOC_INLINE 239# define inline 240#else 241# define JEMALLOC_ENABLE_INLINE 242# ifdef JEMALLOC_HAVE_ATTR 243# define JEMALLOC_ALWAYS_INLINE \ 244 static inline JEMALLOC_ATTR(unused) JEMALLOC_ATTR(always_inline) 245# else 246# define JEMALLOC_ALWAYS_INLINE static inline 247# endif 248# define JEMALLOC_INLINE static inline 249# ifdef _MSC_VER 250# define inline _inline 251# endif 252#endif 253 254/* Smallest size class to support. */ 255#define LG_TINY_MIN 3 256#define TINY_MIN (1U << LG_TINY_MIN) 257 258/* 259 * Minimum alignment of allocations is 2^LG_QUANTUM bytes (ignoring tiny size 260 * classes). 261 */ 262#ifndef LG_QUANTUM 263# if (defined(__i386__) || defined(_M_IX86)) 264# define LG_QUANTUM 4 265# endif 266# ifdef __ia64__ 267# define LG_QUANTUM 4 268# endif 269# ifdef __alpha__ 270# define LG_QUANTUM 4 271# endif 272# ifdef __sparc64__ 273# define LG_QUANTUM 4 274# endif 275# if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64)) 276# define LG_QUANTUM 4 277# endif 278# ifdef __arm__ 279# define LG_QUANTUM 3 280# endif 281# ifdef __hppa__ 282# define LG_QUANTUM 4 283# endif 284# ifdef __mips__ 285# define LG_QUANTUM 3 286# endif 287# ifdef __powerpc__ 288# define LG_QUANTUM 4 289# endif 290# ifdef __s390__ 291# define LG_QUANTUM 4 292# endif 293# ifdef __SH4__ 294# define LG_QUANTUM 4 295# endif 296# ifdef __tile__ 297# define LG_QUANTUM 4 298# endif 299# ifndef LG_QUANTUM 300# error "No LG_QUANTUM definition for architecture; specify via CPPFLAGS" 301# endif 302#endif 303 304#define QUANTUM ((size_t)(1U << LG_QUANTUM)) 305#define QUANTUM_MASK (QUANTUM - 1) 306 307/* Return the smallest quantum multiple that is >= a. */ 308#define QUANTUM_CEILING(a) \ 309 (((a) + QUANTUM_MASK) & ~QUANTUM_MASK) 310 311#define LONG ((size_t)(1U << LG_SIZEOF_LONG)) 312#define LONG_MASK (LONG - 1) 313 314/* Return the smallest long multiple that is >= a. */ 315#define LONG_CEILING(a) \ 316 (((a) + LONG_MASK) & ~LONG_MASK) 317 318#define SIZEOF_PTR (1U << LG_SIZEOF_PTR) 319#define PTR_MASK (SIZEOF_PTR - 1) 320 321/* Return the smallest (void *) multiple that is >= a. */ 322#define PTR_CEILING(a) \ 323 (((a) + PTR_MASK) & ~PTR_MASK) 324 325/* 326 * Maximum size of L1 cache line. This is used to avoid cache line aliasing. 327 * In addition, this controls the spacing of cacheline-spaced size classes. 328 * 329 * CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can 330 * only handle raw constants. 331 */ 332#define LG_CACHELINE 6 333#define CACHELINE 64 334#define CACHELINE_MASK (CACHELINE - 1) 335 336/* Return the smallest cacheline multiple that is >= s. */ 337#define CACHELINE_CEILING(s) \ 338 (((s) + CACHELINE_MASK) & ~CACHELINE_MASK) 339 340/* Page size. STATIC_PAGE_SHIFT is determined by the configure script. */ 341#ifdef PAGE_MASK 342# undef PAGE_MASK 343#endif 344#define LG_PAGE STATIC_PAGE_SHIFT 345#define PAGE ((size_t)(1U << STATIC_PAGE_SHIFT)) 346#define PAGE_MASK ((size_t)(PAGE - 1)) 347 348/* Return the smallest pagesize multiple that is >= s. */ 349#define PAGE_CEILING(s) \ 350 (((s) + PAGE_MASK) & ~PAGE_MASK) 351 352/* Return the nearest aligned address at or below a. */ 353#define ALIGNMENT_ADDR2BASE(a, alignment) \ 354 ((void *)((uintptr_t)(a) & (-(alignment)))) 355 356/* Return the offset between a and the nearest aligned address at or below a. */ 357#define ALIGNMENT_ADDR2OFFSET(a, alignment) \ 358 ((size_t)((uintptr_t)(a) & (alignment - 1))) 359 360/* Return the smallest alignment multiple that is >= s. */ 361#define ALIGNMENT_CEILING(s, alignment) \ 362 (((s) + (alignment - 1)) & (-(alignment))) 363 364/* Declare a variable length array */ 365#if __STDC_VERSION__ < 199901L 366# ifdef _MSC_VER 367# include <malloc.h> 368# define alloca _alloca 369# else 370# ifdef JEMALLOC_HAS_ALLOCA_H 371# include <alloca.h> 372# else 373# include <stdlib.h> 374# endif 375# endif 376# define VARIABLE_ARRAY(type, name, count) \ 377 type *name = alloca(sizeof(type) * count) 378#else 379# define VARIABLE_ARRAY(type, name, count) type name[count] 380#endif 381 382#ifdef JEMALLOC_VALGRIND 383/* 384 * The JEMALLOC_VALGRIND_*() macros must be macros rather than functions 385 * so that when Valgrind reports errors, there are no extra stack frames 386 * in the backtraces. 387 * 388 * The size that is reported to valgrind must be consistent through a chain of 389 * malloc..realloc..realloc calls. Request size isn't recorded anywhere in 390 * jemalloc, so it is critical that all callers of these macros provide usize 391 * rather than request size. As a result, buffer overflow detection is 392 * technically weakened for the standard API, though it is generally accepted 393 * practice to consider any extra bytes reported by malloc_usable_size() as 394 * usable space. 395 */ 396#define JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero) do { \ 397 if (config_valgrind && opt_valgrind && cond) \ 398 VALGRIND_MALLOCLIKE_BLOCK(ptr, usize, p2rz(ptr), zero); \ 399} while (0) 400#define JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize, \ 401 old_rzsize, zero) do { \ 402 if (config_valgrind && opt_valgrind) { \ 403 size_t rzsize = p2rz(ptr); \ 404 \ 405 if (ptr == old_ptr) { \ 406 VALGRIND_RESIZEINPLACE_BLOCK(ptr, old_usize, \ 407 usize, rzsize); \ 408 if (zero && old_usize < usize) { \ 409 VALGRIND_MAKE_MEM_DEFINED( \ 410 (void *)((uintptr_t)ptr + \ 411 old_usize), usize - old_usize); \ 412 } \ 413 } else { \ 414 if (old_ptr != NULL) { \ 415 VALGRIND_FREELIKE_BLOCK(old_ptr, \ 416 old_rzsize); \ 417 } \ 418 if (ptr != NULL) { \ 419 size_t copy_size = (old_usize < usize) \ 420 ? old_usize : usize; \ 421 size_t tail_size = usize - copy_size; \ 422 VALGRIND_MALLOCLIKE_BLOCK(ptr, usize, \ 423 rzsize, false); \ 424 if (copy_size > 0) { \ 425 VALGRIND_MAKE_MEM_DEFINED(ptr, \ 426 copy_size); \ 427 } \ 428 if (zero && tail_size > 0) { \ 429 VALGRIND_MAKE_MEM_DEFINED( \ 430 (void *)((uintptr_t)ptr + \ 431 copy_size), tail_size); \ 432 } \ 433 } \ 434 } \ 435 } \ 436} while (0) 437#define JEMALLOC_VALGRIND_FREE(ptr, rzsize) do { \ 438 if (config_valgrind && opt_valgrind) \ 439 VALGRIND_FREELIKE_BLOCK(ptr, rzsize); \ 440} while (0) 441#else 442#define RUNNING_ON_VALGRIND ((unsigned)0) 443#define VALGRIND_MALLOCLIKE_BLOCK(addr, sizeB, rzB, is_zeroed) \ 444 do {} while (0) 445#define VALGRIND_RESIZEINPLACE_BLOCK(addr, oldSizeB, newSizeB, rzB) \ 446 do {} while (0) 447#define VALGRIND_FREELIKE_BLOCK(addr, rzB) do {} while (0) 448#define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr, _qzz_len) do {} while (0) 449#define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr, _qzz_len) do {} while (0) 450#define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr, _qzz_len) do {} while (0) 451#define JEMALLOC_VALGRIND_MALLOC(cond, ptr, usize, zero) do {} while (0) 452#define JEMALLOC_VALGRIND_REALLOC(ptr, usize, old_ptr, old_usize, \ 453 old_rzsize, zero) do {} while (0) 454#define JEMALLOC_VALGRIND_FREE(ptr, rzsize) do {} while (0) 455#endif 456 457#include "jemalloc/internal/util.h" 458#include "jemalloc/internal/atomic.h" 459#include "jemalloc/internal/prng.h" 460#include "jemalloc/internal/ckh.h" 461#include "jemalloc/internal/size_classes.h" 462#include "jemalloc/internal/stats.h" 463#include "jemalloc/internal/ctl.h" 464#include "jemalloc/internal/mutex.h" 465#include "jemalloc/internal/tsd.h" 466#include "jemalloc/internal/mb.h" 467#include "jemalloc/internal/extent.h" 468#include "jemalloc/internal/arena.h" 469#include "jemalloc/internal/bitmap.h" 470#include "jemalloc/internal/base.h" 471#include "jemalloc/internal/chunk.h" 472#include "jemalloc/internal/huge.h" 473#include "jemalloc/internal/rtree.h" 474#include "jemalloc/internal/tcache.h" 475#include "jemalloc/internal/hash.h" 476#include "jemalloc/internal/quarantine.h" 477#include "jemalloc/internal/prof.h" 478 479#undef JEMALLOC_H_TYPES 480/******************************************************************************/ 481#define JEMALLOC_H_STRUCTS 482 483#include "jemalloc/internal/util.h" 484#include "jemalloc/internal/atomic.h" 485#include "jemalloc/internal/prng.h" 486#include "jemalloc/internal/ckh.h" 487#include "jemalloc/internal/size_classes.h" 488#include "jemalloc/internal/stats.h" 489#include "jemalloc/internal/ctl.h" 490#include "jemalloc/internal/mutex.h" 491#include "jemalloc/internal/tsd.h" 492#include "jemalloc/internal/mb.h" 493#include "jemalloc/internal/bitmap.h" 494#include "jemalloc/internal/extent.h" 495#include "jemalloc/internal/arena.h" 496#include "jemalloc/internal/base.h" 497#include "jemalloc/internal/chunk.h" 498#include "jemalloc/internal/huge.h" 499#include "jemalloc/internal/rtree.h" 500#include "jemalloc/internal/tcache.h" 501#include "jemalloc/internal/hash.h" 502#include "jemalloc/internal/quarantine.h" 503#include "jemalloc/internal/prof.h" 504 505typedef struct { 506 uint64_t allocated; 507 uint64_t deallocated; 508} thread_allocated_t; 509/* 510 * The JEMALLOC_CONCAT() wrapper is necessary to pass {0, 0} via a cpp macro 511 * argument. 512 */ 513#define THREAD_ALLOCATED_INITIALIZER JEMALLOC_CONCAT({0, 0}) 514 515#undef JEMALLOC_H_STRUCTS 516/******************************************************************************/ 517#define JEMALLOC_H_EXTERNS 518 519extern bool opt_abort; 520extern bool opt_junk; 521extern size_t opt_quarantine; 522extern bool opt_redzone; 523extern bool opt_utrace; 524extern bool opt_valgrind; 525extern bool opt_xmalloc; 526extern bool opt_zero; 527extern size_t opt_narenas; 528 529/* Number of CPUs. */ 530extern unsigned ncpus; 531 532/* Protects arenas initialization (arenas, arenas_total). */ 533extern malloc_mutex_t arenas_lock; 534/* 535 * Arenas that are used to service external requests. Not all elements of the 536 * arenas array are necessarily used; arenas are created lazily as needed. 537 * 538 * arenas[0..narenas_auto) are used for automatic multiplexing of threads and 539 * arenas. arenas[narenas_auto..narenas_total) are only used if the application 540 * takes some action to create them and allocate from them. 541 */ 542extern arena_t **arenas; 543extern unsigned narenas_total; 544extern unsigned narenas_auto; /* Read-only after initialization. */ 545 546arena_t *arenas_extend(unsigned ind); 547void arenas_cleanup(void *arg); 548arena_t *choose_arena_hard(void); 549void jemalloc_prefork(void); 550void jemalloc_postfork_parent(void); 551void jemalloc_postfork_child(void); 552 553#include "jemalloc/internal/util.h" 554#include "jemalloc/internal/atomic.h" 555#include "jemalloc/internal/prng.h" 556#include "jemalloc/internal/ckh.h" 557#include "jemalloc/internal/size_classes.h" 558#include "jemalloc/internal/stats.h" 559#include "jemalloc/internal/ctl.h" 560#include "jemalloc/internal/mutex.h" 561#include "jemalloc/internal/tsd.h" 562#include "jemalloc/internal/mb.h" 563#include "jemalloc/internal/bitmap.h" 564#include "jemalloc/internal/extent.h" 565#include "jemalloc/internal/arena.h" 566#include "jemalloc/internal/base.h" 567#include "jemalloc/internal/chunk.h" 568#include "jemalloc/internal/huge.h" 569#include "jemalloc/internal/rtree.h" 570#include "jemalloc/internal/tcache.h" 571#include "jemalloc/internal/hash.h" 572#include "jemalloc/internal/quarantine.h" 573#include "jemalloc/internal/prof.h" 574 575#undef JEMALLOC_H_EXTERNS 576/******************************************************************************/ 577#define JEMALLOC_H_INLINES 578 579#include "jemalloc/internal/util.h" 580#include "jemalloc/internal/atomic.h" 581#include "jemalloc/internal/prng.h" 582#include "jemalloc/internal/ckh.h" 583#include "jemalloc/internal/size_classes.h" 584#include "jemalloc/internal/stats.h" 585#include "jemalloc/internal/ctl.h" 586#include "jemalloc/internal/mutex.h" 587#include "jemalloc/internal/tsd.h" 588#include "jemalloc/internal/mb.h" 589#include "jemalloc/internal/extent.h" 590#include "jemalloc/internal/base.h" 591#include "jemalloc/internal/chunk.h" 592#include "jemalloc/internal/huge.h" 593 594#ifndef JEMALLOC_ENABLE_INLINE 595malloc_tsd_protos(JEMALLOC_ATTR(unused), arenas, arena_t *) 596 597size_t s2u(size_t size); 598size_t sa2u(size_t size, size_t alignment); 599unsigned narenas_total_get(void); 600arena_t *choose_arena(arena_t *arena); 601#endif 602 603#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_)) 604/* 605 * Map of pthread_self() --> arenas[???], used for selecting an arena to use 606 * for allocations. 607 */ 608malloc_tsd_externs(arenas, arena_t *) 609malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, arenas, arena_t *, NULL, 610 arenas_cleanup) 611 612/* 613 * Compute usable size that would result from allocating an object with the 614 * specified size. 615 */ 616JEMALLOC_ALWAYS_INLINE size_t 617s2u(size_t size) 618{ 619 620 if (size <= SMALL_MAXCLASS) 621 return (arena_bin_info[SMALL_SIZE2BIN(size)].reg_size); 622 if (size <= arena_maxclass) 623 return (PAGE_CEILING(size)); 624 return (CHUNK_CEILING(size)); 625} 626 627/* 628 * Compute usable size that would result from allocating an object with the 629 * specified size and alignment. 630 */ 631JEMALLOC_ALWAYS_INLINE size_t 632sa2u(size_t size, size_t alignment) 633{ 634 size_t usize; 635 636 assert(alignment != 0 && ((alignment - 1) & alignment) == 0); 637 638 /* 639 * Round size up to the nearest multiple of alignment. 640 * 641 * This done, we can take advantage of the fact that for each small 642 * size class, every object is aligned at the smallest power of two 643 * that is non-zero in the base two representation of the size. For 644 * example: 645 * 646 * Size | Base 2 | Minimum alignment 647 * -----+----------+------------------ 648 * 96 | 1100000 | 32 649 * 144 | 10100000 | 32 650 * 192 | 11000000 | 64 651 */ 652 usize = ALIGNMENT_CEILING(size, alignment); 653 /* 654 * (usize < size) protects against the combination of maximal 655 * alignment and size greater than maximal alignment. 656 */ 657 if (usize < size) { 658 /* size_t overflow. */ 659 return (0); 660 } 661 662 if (usize <= arena_maxclass && alignment <= PAGE) { 663 if (usize <= SMALL_MAXCLASS) 664 return (arena_bin_info[SMALL_SIZE2BIN(usize)].reg_size); 665 return (PAGE_CEILING(usize)); 666 } else { 667 size_t run_size; 668 669 /* 670 * We can't achieve subpage alignment, so round up alignment 671 * permanently; it makes later calculations simpler. 672 */ 673 alignment = PAGE_CEILING(alignment); 674 usize = PAGE_CEILING(size); 675 /* 676 * (usize < size) protects against very large sizes within 677 * PAGE of SIZE_T_MAX. 678 * 679 * (usize + alignment < usize) protects against the 680 * combination of maximal alignment and usize large enough 681 * to cause overflow. This is similar to the first overflow 682 * check above, but it needs to be repeated due to the new 683 * usize value, which may now be *equal* to maximal 684 * alignment, whereas before we only detected overflow if the 685 * original size was *greater* than maximal alignment. 686 */ 687 if (usize < size || usize + alignment < usize) { 688 /* size_t overflow. */ 689 return (0); 690 } 691 692 /* 693 * Calculate the size of the over-size run that arena_palloc() 694 * would need to allocate in order to guarantee the alignment. 695 * If the run wouldn't fit within a chunk, round up to a huge 696 * allocation size. 697 */ 698 run_size = usize + alignment - PAGE; 699 if (run_size <= arena_maxclass) 700 return (PAGE_CEILING(usize)); 701 return (CHUNK_CEILING(usize)); 702 } 703} 704 705JEMALLOC_INLINE unsigned 706narenas_total_get(void) 707{ 708 unsigned narenas; 709 710 malloc_mutex_lock(&arenas_lock); 711 narenas = narenas_total; 712 malloc_mutex_unlock(&arenas_lock); 713 714 return (narenas); 715} 716 717/* Choose an arena based on a per-thread value. */ 718JEMALLOC_INLINE arena_t * 719choose_arena(arena_t *arena) 720{ 721 arena_t *ret; 722 723 if (arena != NULL) 724 return (arena); 725 726 if ((ret = *arenas_tsd_get()) == NULL) { 727 ret = choose_arena_hard(); 728 assert(ret != NULL); 729 } 730 731 return (ret); 732} 733#endif 734 735#include "jemalloc/internal/bitmap.h" 736#include "jemalloc/internal/rtree.h" 737/* 738 * Include arena.h twice in order to resolve circular dependencies with 739 * tcache.h. 740 */ 741#define JEMALLOC_ARENA_INLINE_A 742#include "jemalloc/internal/arena.h" 743#undef JEMALLOC_ARENA_INLINE_A 744#include "jemalloc/internal/tcache.h" 745#define JEMALLOC_ARENA_INLINE_B 746#include "jemalloc/internal/arena.h" 747#undef JEMALLOC_ARENA_INLINE_B 748#include "jemalloc/internal/hash.h" 749#include "jemalloc/internal/quarantine.h" 750 751#ifndef JEMALLOC_ENABLE_INLINE 752void *imallocx(size_t size, bool try_tcache, arena_t *arena); 753void *imalloc(size_t size); 754void *icallocx(size_t size, bool try_tcache, arena_t *arena); 755void *icalloc(size_t size); 756void *ipallocx(size_t usize, size_t alignment, bool zero, bool try_tcache, 757 arena_t *arena); 758void *ipalloc(size_t usize, size_t alignment, bool zero); 759size_t isalloc(const void *ptr, bool demote); 760size_t ivsalloc(const void *ptr, bool demote); 761size_t u2rz(size_t usize); 762size_t p2rz(const void *ptr); 763void idallocx(void *ptr, bool try_tcache); 764void idalloc(void *ptr); 765void iqallocx(void *ptr, bool try_tcache); 766void iqalloc(void *ptr); 767void *irallocx(void *ptr, size_t size, size_t extra, size_t alignment, 768 bool zero, bool no_move, bool try_tcache_alloc, bool try_tcache_dalloc, 769 arena_t *arena); 770void *iralloc(void *ptr, size_t size, size_t extra, size_t alignment, 771 bool zero, bool no_move); 772malloc_tsd_protos(JEMALLOC_ATTR(unused), thread_allocated, thread_allocated_t) 773#endif 774 775#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_C_)) 776JEMALLOC_ALWAYS_INLINE void * 777imallocx(size_t size, bool try_tcache, arena_t *arena) 778{ 779 780 assert(size != 0); 781 782 if (size <= arena_maxclass) 783 return (arena_malloc(arena, size, false, try_tcache)); 784 else 785 return (huge_malloc(size, false)); 786} 787 788JEMALLOC_ALWAYS_INLINE void * 789imalloc(size_t size) 790{ 791 792 return (imallocx(size, true, NULL)); 793} 794 795JEMALLOC_ALWAYS_INLINE void * 796icallocx(size_t size, bool try_tcache, arena_t *arena) 797{ 798 799 if (size <= arena_maxclass) 800 return (arena_malloc(arena, size, true, try_tcache)); 801 else 802 return (huge_malloc(size, true)); 803} 804 805JEMALLOC_ALWAYS_INLINE void * 806icalloc(size_t size) 807{ 808 809 return (icallocx(size, true, NULL)); 810} 811 812JEMALLOC_ALWAYS_INLINE void * 813ipallocx(size_t usize, size_t alignment, bool zero, bool try_tcache, 814 arena_t *arena) 815{ 816 void *ret; 817 818 assert(usize != 0); 819 assert(usize == sa2u(usize, alignment)); 820 821 if (usize <= arena_maxclass && alignment <= PAGE) 822 ret = arena_malloc(arena, usize, zero, try_tcache); 823 else { 824 if (usize <= arena_maxclass) { 825 ret = arena_palloc(choose_arena(arena), usize, 826 alignment, zero); 827 } else if (alignment <= chunksize) 828 ret = huge_malloc(usize, zero); 829 else 830 ret = huge_palloc(usize, alignment, zero); 831 } 832 833 assert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret); 834 return (ret); 835} 836 837JEMALLOC_ALWAYS_INLINE void * 838ipalloc(size_t usize, size_t alignment, bool zero) 839{ 840 841 return (ipallocx(usize, alignment, zero, true, NULL)); 842} 843 844/* 845 * Typical usage: 846 * void *ptr = [...] 847 * size_t sz = isalloc(ptr, config_prof); 848 */ 849JEMALLOC_ALWAYS_INLINE size_t 850isalloc(const void *ptr, bool demote) 851{ 852 size_t ret; 853 arena_chunk_t *chunk; 854 855 assert(ptr != NULL); 856 /* Demotion only makes sense if config_prof is true. */ 857 assert(config_prof || demote == false); 858 859 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 860 if (chunk != ptr) 861 ret = arena_salloc(ptr, demote); 862 else 863 ret = huge_salloc(ptr); 864 865 return (ret); 866} 867 868JEMALLOC_ALWAYS_INLINE size_t 869ivsalloc(const void *ptr, bool demote) 870{ 871 872 /* Return 0 if ptr is not within a chunk managed by jemalloc. */ 873 if (rtree_get(chunks_rtree, (uintptr_t)CHUNK_ADDR2BASE(ptr)) == NULL) 874 return (0); 875 876 return (isalloc(ptr, demote)); 877} 878 879JEMALLOC_INLINE size_t 880u2rz(size_t usize) 881{ 882 size_t ret; 883 884 if (usize <= SMALL_MAXCLASS) { 885 size_t binind = SMALL_SIZE2BIN(usize); 886 ret = arena_bin_info[binind].redzone_size; 887 } else 888 ret = 0; 889 890 return (ret); 891} 892 893JEMALLOC_INLINE size_t 894p2rz(const void *ptr) 895{ 896 size_t usize = isalloc(ptr, false); 897 898 return (u2rz(usize)); 899} 900 901JEMALLOC_ALWAYS_INLINE void 902idallocx(void *ptr, bool try_tcache) 903{ 904 arena_chunk_t *chunk; 905 906 assert(ptr != NULL); 907 908 chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); 909 if (chunk != ptr) 910 arena_dalloc(chunk->arena, chunk, ptr, try_tcache); 911 else 912 huge_dalloc(ptr, true); 913} 914 915JEMALLOC_ALWAYS_INLINE void 916idalloc(void *ptr) 917{ 918 919 idallocx(ptr, true); 920} 921 922JEMALLOC_ALWAYS_INLINE void 923iqallocx(void *ptr, bool try_tcache) 924{ 925 926 if (config_fill && opt_quarantine) 927 quarantine(ptr); 928 else 929 idallocx(ptr, try_tcache); 930} 931 932JEMALLOC_ALWAYS_INLINE void 933iqalloc(void *ptr) 934{ 935 936 iqallocx(ptr, true); 937} 938 939JEMALLOC_ALWAYS_INLINE void * 940irallocx(void *ptr, size_t size, size_t extra, size_t alignment, bool zero, 941 bool no_move, bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena) 942{ 943 void *ret; 944 size_t oldsize; 945 946 assert(ptr != NULL); 947 assert(size != 0); 948 949 oldsize = isalloc(ptr, config_prof); 950 951 if (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1)) 952 != 0) { 953 size_t usize, copysize; 954 955 /* 956 * Existing object alignment is inadequate; allocate new space 957 * and copy. 958 */ 959 if (no_move) 960 return (NULL); 961 usize = sa2u(size + extra, alignment); 962 if (usize == 0) 963 return (NULL); 964 ret = ipallocx(usize, alignment, zero, try_tcache_alloc, arena); 965 if (ret == NULL) { 966 if (extra == 0) 967 return (NULL); 968 /* Try again, without extra this time. */ 969 usize = sa2u(size, alignment); 970 if (usize == 0) 971 return (NULL); 972 ret = ipallocx(usize, alignment, zero, try_tcache_alloc, 973 arena); 974 if (ret == NULL) 975 return (NULL); 976 } 977 /* 978 * Copy at most size bytes (not size+extra), since the caller 979 * has no expectation that the extra bytes will be reliably 980 * preserved. 981 */ 982 copysize = (size < oldsize) ? size : oldsize; 983 memcpy(ret, ptr, copysize); 984 iqallocx(ptr, try_tcache_dalloc); 985 return (ret); 986 } 987 988 if (no_move) { 989 if (size <= arena_maxclass) { 990 return (arena_ralloc_no_move(ptr, oldsize, size, 991 extra, zero)); 992 } else { 993 return (huge_ralloc_no_move(ptr, oldsize, size, 994 extra)); 995 } 996 } else { 997 if (size + extra <= arena_maxclass) { 998 return (arena_ralloc(arena, ptr, oldsize, size, extra, 999 alignment, zero, try_tcache_alloc, 1000 try_tcache_dalloc)); 1001 } else { 1002 return (huge_ralloc(ptr, oldsize, size, extra, 1003 alignment, zero, try_tcache_dalloc)); 1004 } 1005 } 1006} 1007 1008JEMALLOC_ALWAYS_INLINE void * 1009iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero, 1010 bool no_move) 1011{ 1012 1013 return (irallocx(ptr, size, extra, alignment, zero, no_move, true, true, 1014 NULL)); 1015} 1016 1017malloc_tsd_externs(thread_allocated, thread_allocated_t) 1018malloc_tsd_funcs(JEMALLOC_ALWAYS_INLINE, thread_allocated, thread_allocated_t, 1019 THREAD_ALLOCATED_INITIALIZER, malloc_tsd_no_cleanup) 1020#endif 1021 1022#include "jemalloc/internal/prof.h" 1023 1024#undef JEMALLOC_H_INLINES 1025/******************************************************************************/ 1026#endif /* JEMALLOC_INTERNAL_H */ 1027