9191#include < wtf/StdLibExtras.h>
9292
9393#if OS(DARWIN)
94+ #include < mach/mach_init.h>
9495#include < malloc/malloc.h>
9596#endif
9697
@@ -629,28 +630,45 @@ static ALWAYS_INLINE uint32_t freedObjectEndPoison()
629630// Configuration
630631// -------------------------------------------------------------------
631632
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;
633+ // // Not all possible combinations of the following parameters make
634+ // // sense. In particular, if kMaxSize increases, you may have to
635+ // // increase kNumClasses as well.
636+ // #if OS(DARWIN)
637+ // # define K_PAGE_SHIFT PAGE_SHIFT
638+ // # if (K_PAGE_SHIFT == 12)
639+ // # define K_NUM_CLASSES 68
640+ // # elif (K_PAGE_SHIFT == 14)
641+ // # define K_NUM_CLASSES 77
642+ // # else
643+ // # error "Unsupported PAGE_SHIFT amount"
644+ // # endif
645+ // #else
646+ // # define K_PAGE_SHIFT 12
647+ // # define K_NUM_CLASSES 68
648+ // #endif
649+ // static const size_t kPageShift = K_PAGE_SHIFT;
650+ // static const size_t kPageSize = 1 << kPageShift;
651+ // static const size_t kMaxSize = 32u * 1024;
652+ // static const size_t kAlignShift = 3;
653+ // static const size_t kAlignment = 1 << kAlignShift;
654+ // static const size_t kNumClasses = K_NUM_CLASSES;
655+
656+ // Type that can hold the length of a run of pages
657+ typedef uintptr_t Length;
658+
659+ // Not all possible combinations of the following parameters make
660+ // sense. In particular, if kMaxSize increases, you may have to
661+ // increase kNumClasses as well.
662+ #define K_PAGE_SHIFT_MIN 12
663+ #define K_PAGE_SHIFT_MAX 14
664+ #define K_NUM_CLASSES_MAX 77
665+ static size_t kPageShift = 0 ;
666+ static size_t kNumClasses = 0 ;
667+ static size_t kPageSize = 0 ;
668+ static Length kMaxValidPages = 0 ;
669+ static const size_t kMaxSize = 32u * 1024 ;
670+ static const size_t kAlignShift = 3 ;
671+ static const size_t kAlignment = 1 << kAlignShift ;
654672
655673// Allocates a big block of memory for the pagemap once we reach more than
656674// 128MB
@@ -662,14 +680,14 @@ static const size_t kPageMapBigAllocationThreshold = 128 << 20;
662680// should keep this value big because various incarnations of Linux
663681// have small limits on the number of mmap() regions per
664682// address-space.
665- static const size_t kMinSystemAlloc = 1 << (20 - kPageShift );
683+ static const size_t kMinSystemAlloc = 1 << (20 - K_PAGE_SHIFT_MAX );
666684
667685// Number of objects to move between a per-thread list and a central
668686// list in one shot. We want this to be not too small so we can
669687// amortize the lock overhead for accessing the central list. Making
670688// it too big may temporarily cause unnecessary memory wastage in the
671689// per-thread free list until the scavenger cleans up the list.
672- static int num_objects_to_move[kNumClasses ];
690+ static int num_objects_to_move[K_NUM_CLASSES_MAX ];
673691
674692// Maximum length we allow a per-thread free-list to have before we
675693// move objects from it into the corresponding central free-list. We
@@ -765,10 +783,10 @@ static inline int ClassIndex(size_t s) {
765783}
766784
767785// Mapping from size class to max size storable in that class
768- static size_t class_to_size[kNumClasses ];
786+ static size_t class_to_size[K_NUM_CLASSES_MAX ];
769787
770788// Mapping from size class to number of pages to allocate at a time
771- static size_t class_to_pages[kNumClasses ];
789+ static size_t class_to_pages[K_NUM_CLASSES_MAX ];
772790
773791// Hardened singly linked list. We make this a class to allow compiler to
774792// statically prevent mismatching hardened and non-hardened list
@@ -808,12 +826,13 @@ struct TCEntry {
808826 HardenedSLL head; // Head of chain of objects.
809827 HardenedSLL tail; // Tail of chain of objects.
810828};
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 ;
829+ // A central cache freelist can have anywhere from 0 to kNumTransferEntries
830+ // slots to put link list chains into. To keep memory usage bounded the total
831+ // number of TCEntries across size classes is fixed. Currently each size
832+ // class is initially given one TCEntry which also means that the maximum any
833+ // one class can have is kNumClasses.
834+ #define K_NUM_TRANSFER_ENTRIES_MAX static_cast <int >(K_NUM_CLASSES_MAX)
835+ #define kNumTransferEntries static_cast <int >(kNumClasses )
817836
818837// Note: the following only works for "n"s that fit in 32-bits, but
819838// that is fine since we only use it for small sizes.
@@ -917,6 +936,25 @@ static int NumMoveSize(size_t size) {
917936
918937// Initialize the mapping arrays
919938static void InitSizeClasses () {
939+ #if OS(DARWIN)
940+ kPageShift = vm_page_shift;
941+ switch (kPageShift ) {
942+ case 12 :
943+ kNumClasses = 68 ;
944+ break ;
945+ case 14 :
946+ kNumClasses = 77 ;
947+ break ;
948+ default :
949+ CRASH ();
950+ };
951+ #else
952+ kPageShift = 12 ;
953+ kNumClasses = 68 ;
954+ #endif
955+ kPageSize = 1 << kPageShift ;
956+ kMaxValidPages = (~static_cast <Length>(0 )) >> kPageShift ;
957+
920958 // Do some sanity checking on add_amount[]/shift_amount[]/class_array[]
921959 if (ClassIndex (0 ) < 0 ) {
922960 MESSAGE (" Invalid class index %d for size 0\n " , ClassIndex (0 ));
@@ -1144,11 +1182,6 @@ class PageHeapAllocator {
11441182// Type that can hold a page number
11451183typedef uintptr_t PageID;
11461184
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-
11521185// Convert byte size into pages. This won't overflow, but may return
11531186// an unreasonably large value if bytes is huge enough.
11541187static inline Length pages (size_t bytes) {
@@ -1431,7 +1464,7 @@ class TCMalloc_Central_FreeList {
14311464 // Here we reserve space for TCEntry cache slots. Since one size class can
14321465 // end up getting all the TCEntries quota in the system we just preallocate
14331466 // sufficient number of entries here.
1434- TCEntry tc_slots_[kNumTransferEntries ];
1467+ TCEntry tc_slots_[K_NUM_TRANSFER_ENTRIES_MAX ];
14351468
14361469 // Number of currently used cached entries in tc_slots_. This variable is
14371470 // updated under a lock but can be read without one.
@@ -1677,16 +1710,16 @@ static const size_t kBitsUnusedOn64Bit = 0;
16771710// A three-level map for 64-bit machines
16781711template <> class MapSelector <64 > {
16791712 public:
1680- typedef TCMalloc_PageMap3<64 - kPageShift - kBitsUnusedOn64Bit > Type;
1713+ typedef TCMalloc_PageMap3<64 - K_PAGE_SHIFT_MIN - kBitsUnusedOn64Bit > Type;
16811714 typedef PackedCache<64 , uint64_t > CacheType;
16821715};
16831716#endif
16841717
16851718// A two-level map for 32-bit machines
16861719template <> class MapSelector <32 > {
16871720 public:
1688- typedef TCMalloc_PageMap2<32 - kPageShift > Type;
1689- typedef PackedCache<32 - kPageShift , uint16_t > CacheType;
1721+ typedef TCMalloc_PageMap2<32 - K_PAGE_SHIFT_MIN > Type;
1722+ typedef PackedCache<32 - K_PAGE_SHIFT_MIN , uint16_t > CacheType;
16901723};
16911724
16921725// -------------------------------------------------------------------------
@@ -1932,7 +1965,7 @@ void TCMalloc_PageHeap::init()
19321965 scavenge_counter_ = 0 ;
19331966 // Start scavenging at kMaxPages list
19341967 scavenge_index_ = kMaxPages -1 ;
1935- COMPILE_ASSERT (kNumClasses <= (1 << PageMapCache::kValuebits ), valuebits );
1968+ ASSERT (kNumClasses <= (1 << PageMapCache::kValuebits ));
19361969 DLL_Init (&large_.normal , entropy_);
19371970 DLL_Init (&large_.returned , entropy_);
19381971 for (size_t i = 0 ; i < kMaxPages ; i++) {
@@ -2744,7 +2777,7 @@ class TCMalloc_ThreadCache {
27442777 size_t size_; // Combined size of data
27452778 ThreadIdentifier tid_; // Which thread owns it
27462779 bool in_setspecific_; // Called pthread_setspecific?
2747- FreeList list_[kNumClasses ]; // Array indexed by size-class
2780+ FreeList list_[K_NUM_CLASSES_MAX ]; // Array indexed by size-class
27482781
27492782 // We sample allocations, biased by the size of the allocation
27502783 uint32_t rnd_; // Cheap random number generator
@@ -2812,7 +2845,7 @@ class TCMalloc_ThreadCache {
28122845
28132846// Central cache -- a collection of free-lists, one per size-class.
28142847// We have a separate lock per free-list to reduce contention.
2815- static TCMalloc_Central_FreeListPadded central_cache[kNumClasses ];
2848+ static TCMalloc_Central_FreeListPadded central_cache[K_NUM_CLASSES_MAX ];
28162849
28172850// Page-level allocator
28182851static AllocAlignmentInteger pageheap_memory[(sizeof (TCMalloc_PageHeap) + sizeof (AllocAlignmentInteger) - 1 ) / sizeof (AllocAlignmentInteger)];
0 commit comments