9191#include < wtf/StdLibExtras.h>
9292
9393#if OS(DARWIN)
94+ #include < mach/mach_init.h>
9495#include < malloc/malloc.h>
9596#endif
9697
@@ -628,29 +629,23 @@ static ALWAYS_INLINE uint32_t freedObjectEndPoison()
628629// -------------------------------------------------------------------
629630// Configuration
630631// -------------------------------------------------------------------
631-
632- // Not all possible combinations of the following parameters make
633- // sense. In particular, if kMaxSize increases, you may have to
634- // increase kNumClasses as well.
635- #if OS(DARWIN)
636- # define K_PAGE_SHIFT PAGE_SHIFT
637- # if (K_PAGE_SHIFT == 12)
638- # define K_NUM_CLASSES 68
639- # elif (K_PAGE_SHIFT == 14)
640- # define K_NUM_CLASSES 77
641- # else
642- # error "Unsupported PAGE_SHIFT amount"
643- # endif
644- #else
645- # define K_PAGE_SHIFT 12
646- # define K_NUM_CLASSES 68
647- #endif
648- static const size_t kPageShift = K_PAGE_SHIFT;
649- static const size_t kPageSize = 1 << kPageShift ;
650- static const size_t kMaxSize = 32u * 1024 ;
651- static const size_t kAlignShift = 3 ;
652- static const size_t kAlignment = 1 << kAlignShift ;
653- static const size_t kNumClasses = K_NUM_CLASSES;
632+
633+ // Type that can hold the length of a run of pages
634+ typedef uintptr_t Length;
635+
636+ // Not all possible combinations of the following parameters make
637+ // sense. In particular, if kMaxSize increases, you may have to
638+ // increase kNumClasses as well.
639+ #define K_PAGE_SHIFT_MIN 12
640+ #define K_PAGE_SHIFT_MAX 14
641+ #define K_NUM_CLASSES_MAX 77
642+ static size_t kPageShift = 0 ;
643+ static size_t kNumClasses = 0 ;
644+ static size_t kPageSize = 0 ;
645+ static Length kMaxValidPages = 0 ;
646+ static const size_t kMaxSize = 32u * 1024 ;
647+ static const size_t kAlignShift = 3 ;
648+ static const size_t kAlignment = 1 << kAlignShift ;
654649
655650// Allocates a big block of memory for the pagemap once we reach more than
656651// 128MB
@@ -662,14 +657,14 @@ static const size_t kPageMapBigAllocationThreshold = 128 << 20;
662657// should keep this value big because various incarnations of Linux
663658// have small limits on the number of mmap() regions per
664659// address-space.
665- static const size_t kMinSystemAlloc = 1 << (20 - kPageShift );
660+ static const size_t kMinSystemAlloc = 1 << (20 - K_PAGE_SHIFT_MAX );
666661
667662// Number of objects to move between a per-thread list and a central
668663// list in one shot. We want this to be not too small so we can
669664// amortize the lock overhead for accessing the central list. Making
670665// it too big may temporarily cause unnecessary memory wastage in the
671666// per-thread free list until the scavenger cleans up the list.
672- static int num_objects_to_move[kNumClasses ];
667+ static int num_objects_to_move[K_NUM_CLASSES_MAX ];
673668
674669// Maximum length we allow a per-thread free-list to have before we
675670// move objects from it into the corresponding central free-list. We
@@ -765,10 +760,10 @@ static inline int ClassIndex(size_t s) {
765760}
766761
767762// Mapping from size class to max size storable in that class
768- static size_t class_to_size[kNumClasses ];
763+ static size_t class_to_size[K_NUM_CLASSES_MAX ];
769764
770765// Mapping from size class to number of pages to allocate at a time
771- static size_t class_to_pages[kNumClasses ];
766+ static size_t class_to_pages[K_NUM_CLASSES_MAX ];
772767
773768// Hardened singly linked list. We make this a class to allow compiler to
774769// statically prevent mismatching hardened and non-hardened list
@@ -808,12 +803,13 @@ struct TCEntry {
808803 HardenedSLL head; // Head of chain of objects.
809804 HardenedSLL tail; // Tail of chain of objects.
810805};
811- // A central cache freelist can have anywhere from 0 to kNumTransferEntries
812- // slots to put link list chains into. To keep memory usage bounded the total
813- // number of TCEntries across size classes is fixed. Currently each size
814- // class is initially given one TCEntry which also means that the maximum any
815- // one class can have is kNumClasses.
816- static const int kNumTransferEntries = kNumClasses ;
806+ // A central cache freelist can have anywhere from 0 to kNumTransferEntries
807+ // slots to put link list chains into. To keep memory usage bounded the total
808+ // number of TCEntries across size classes is fixed. Currently each size
809+ // class is initially given one TCEntry which also means that the maximum any
810+ // one class can have is kNumClasses.
811+ #define K_NUM_TRANSFER_ENTRIES_MAX static_cast <int >(K_NUM_CLASSES_MAX)
812+ #define kNumTransferEntries static_cast <int >(kNumClasses )
817813
818814// Note: the following only works for "n"s that fit in 32-bits, but
819815// that is fine since we only use it for small sizes.
@@ -917,6 +913,25 @@ static int NumMoveSize(size_t size) {
917913
918914// Initialize the mapping arrays
919915static void InitSizeClasses () {
916+ #if OS(DARWIN)
917+ kPageShift = vm_page_shift;
918+ switch (kPageShift ) {
919+ case 12 :
920+ kNumClasses = 68 ;
921+ break ;
922+ case 14 :
923+ kNumClasses = 77 ;
924+ break ;
925+ default :
926+ CRASH ();
927+ };
928+ #else
929+ kPageShift = 12 ;
930+ kNumClasses = 68 ;
931+ #endif
932+ kPageSize = 1 << kPageShift ;
933+ kMaxValidPages = (~static_cast <Length>(0 )) >> kPageShift ;
934+
920935 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[]
921936 if (ClassIndex (0 ) < 0 ) {
922937 MESSAGE (" Invalid class index %d for size 0\n " , ClassIndex (0 ));
@@ -1144,11 +1159,6 @@ class PageHeapAllocator {
11441159// Type that can hold a page number
11451160typedef uintptr_t PageID;
11461161
1147- // Type that can hold the length of a run of pages
1148- typedef uintptr_t Length;
1149-
1150- static const Length kMaxValidPages = (~static_cast <Length>(0 )) >> kPageShift ;
1151-
11521162// Convert byte size into pages. This won't overflow, but may return
11531163// an unreasonably large value if bytes is huge enough.
11541164static inline Length pages (size_t bytes) {
@@ -1431,7 +1441,7 @@ class TCMalloc_Central_FreeList {
14311441 // Here we reserve space for TCEntry cache slots. Since one size class can
14321442 // end up getting all the TCEntries quota in the system we just preallocate
14331443 // sufficient number of entries here.
1434- TCEntry tc_slots_[kNumTransferEntries ];
1444+ TCEntry tc_slots_[K_NUM_TRANSFER_ENTRIES_MAX ];
14351445
14361446 // Number of currently used cached entries in tc_slots_. This variable is
14371447 // updated under a lock but can be read without one.
@@ -1677,16 +1687,16 @@ static const size_t kBitsUnusedOn64Bit = 0;
16771687// A three-level map for 64-bit machines
16781688template <> class MapSelector <64 > {
16791689 public:
1680- typedef TCMalloc_PageMap3<64 - kPageShift - kBitsUnusedOn64Bit > Type;
1690+ typedef TCMalloc_PageMap3<64 - K_PAGE_SHIFT_MIN - kBitsUnusedOn64Bit > Type;
16811691 typedef PackedCache<64 , uint64_t > CacheType;
16821692};
16831693#endif
16841694
16851695// A two-level map for 32-bit machines
16861696template <> class MapSelector <32 > {
16871697 public:
1688- typedef TCMalloc_PageMap2<32 - kPageShift > Type;
1689- typedef PackedCache<32 - kPageShift , uint16_t > CacheType;
1698+ typedef TCMalloc_PageMap2<32 - K_PAGE_SHIFT_MIN > Type;
1699+ typedef PackedCache<32 - K_PAGE_SHIFT_MIN , uint16_t > CacheType;
16901700};
16911701
16921702// -------------------------------------------------------------------------
@@ -1932,7 +1942,7 @@ void TCMalloc_PageHeap::init()
19321942 scavenge_counter_ = 0 ;
19331943 // Start scavenging at kMaxPages list
19341944 scavenge_index_ = kMaxPages -1 ;
1935- COMPILE_ASSERT (kNumClasses <= (1 << PageMapCache::kValuebits ), valuebits );
1945+ ASSERT (kNumClasses <= (1 << PageMapCache::kValuebits ));
19361946 DLL_Init (&large_.normal , entropy_);
19371947 DLL_Init (&large_.returned , entropy_);
19381948 for (size_t i = 0 ; i < kMaxPages ; i++) {
@@ -2744,7 +2754,7 @@ class TCMalloc_ThreadCache {
27442754 size_t size_; // Combined size of data
27452755 ThreadIdentifier tid_; // Which thread owns it
27462756 bool in_setspecific_; // Called pthread_setspecific?
2747- FreeList list_[kNumClasses ]; // Array indexed by size-class
2757+ FreeList list_[K_NUM_CLASSES_MAX ]; // Array indexed by size-class
27482758
27492759 // We sample allocations, biased by the size of the allocation
27502760 uint32_t rnd_; // Cheap random number generator
@@ -2812,7 +2822,7 @@ class TCMalloc_ThreadCache {
28122822
28132823// Central cache -- a collection of free-lists, one per size-class.
28142824// We have a separate lock per free-list to reduce contention.
2815- static TCMalloc_Central_FreeListPadded central_cache[kNumClasses ];
2825+ static TCMalloc_Central_FreeListPadded central_cache[K_NUM_CLASSES_MAX ];
28162826
28172827// Page-level allocator
28182828static AllocAlignmentInteger pageheap_memory[(sizeof (TCMalloc_PageHeap) + sizeof (AllocAlignmentInteger) - 1 ) / sizeof (AllocAlignmentInteger)];
0 commit comments