@@ -121,13 +121,6 @@ static_assert(sizeof(MemoryAligner) < kMinSystemAlloc,
121
121
ABSL_CONST_INIT absl::base_internal::SpinLock spinlock (
122
122
absl::kConstInit , absl::base_internal::SCHEDULE_KERNEL_ONLY);
123
123
124
- // Page size is initialized on demand
125
- ABSL_CONST_INIT size_t preferred_alignment ABSL_GUARDED_BY (spinlock) = 0;
126
-
127
- // The current region factory.
128
- ABSL_CONST_INIT AddressRegionFactory* region_factory ABSL_GUARDED_BY (spinlock) =
129
- nullptr;
130
-
131
124
// Rounds size down to a multiple of alignment.
132
125
size_t RoundDown (const size_t size, const size_t alignment) {
133
126
// Checks that the alignment has only one bit set.
@@ -170,8 +163,14 @@ class MmapRegionFactory final : public AddressRegionFactory {
170
163
TCMALLOC_ATTRIBUTE_NO_DESTROY ABSL_CONST_INIT MmapRegionFactory mmap_factory
171
164
ABSL_GUARDED_BY (spinlock);
172
165
166
+ // The current region factory.
167
+ ABSL_CONST_INIT AddressRegionFactory* region_factory ABSL_GUARDED_BY (spinlock) =
168
+ &mmap_factory;
169
+
173
170
class RegionManager {
174
171
public:
172
+ constexpr RegionManager () = default;
173
+
175
174
std::pair<void *, size_t > Alloc (size_t size, size_t alignment, MemoryTag tag)
176
175
ABSL_EXCLUSIVE_LOCKS_REQUIRED(spinlock);
177
176
@@ -197,19 +196,16 @@ class RegionManager {
197
196
AddressRegion* cold_region_{nullptr };
198
197
AddressRegion* metadata_region_{nullptr };
199
198
};
200
- ABSL_CONST_INIT
201
- std::aligned_storage<sizeof (RegionManager), alignof(RegionManager)>::type
202
- region_manager_space ABSL_GUARDED_BY (spinlock){};
203
- ABSL_CONST_INIT RegionManager* region_manager ABSL_GUARDED_BY (spinlock) =
204
- nullptr;
199
+
200
+ ABSL_CONST_INIT RegionManager region_manager ABSL_GUARDED_BY (spinlock);
205
201
206
202
std::pair<void *, size_t > MmapRegion::Alloc (size_t request_size,
207
203
size_t alignment) {
208
204
// Align on kMinSystemAlloc boundaries to reduce external fragmentation for
209
205
// future allocations.
210
206
size_t size = RoundUp (request_size, kMinSystemAlloc );
211
207
if (size < request_size) return {nullptr , 0 };
212
- alignment = std::max (alignment, preferred_alignment );
208
+ alignment = std::max (alignment, kMinSystemAlloc );
213
209
214
210
// Tries to allocate size bytes from the end of [start_, start_ + free_size_),
215
211
// aligned to alignment.
@@ -314,7 +310,7 @@ std::pair<void*, size_t> RegionManager::Alloc(size_t request_size,
314
310
// future allocations.
315
311
size_t size = RoundUp (request_size, kMinSystemAlloc );
316
312
if (size < request_size) return {nullptr , 0 };
317
- alignment = std::max (alignment, preferred_alignment );
313
+ alignment = std::max (alignment, kMinSystemAlloc );
318
314
void * ptr = MmapAligned (size, alignment, tag);
319
315
if (!ptr) return {nullptr , 0 };
320
316
@@ -378,17 +374,6 @@ std::pair<void*, size_t> RegionManager::Allocate(size_t size, size_t alignment,
378
374
return region->Alloc (size, alignment);
379
375
}
380
376
381
- void InitSystemAllocatorIfNecessary () ABSL_EXCLUSIVE_LOCKS_REQUIRED(spinlock) {
382
- if (region_factory) return ;
383
- // Sets the preferred alignment to be the largest of either the alignment
384
- // returned by mmap() or our minimum allocation size. The minimum allocation
385
- // size is usually a multiple of page size, but this need not be true for
386
- // SMALL_BUT_SLOW where we do not allocate in units of huge pages.
387
- preferred_alignment = std::max (GetPageSize (), kMinSystemAlloc );
388
- region_manager = new (®ion_manager_space) RegionManager ();
389
- region_factory = &mmap_factory;
390
- }
391
-
392
377
// Bind the memory region spanning `size` bytes starting from `base` to NUMA
393
378
// nodes assigned to `partition`. Returns zero upon success, or a standard
394
379
// error code upon failure.
@@ -476,9 +461,7 @@ AddressRange SystemAlloc(size_t bytes, size_t alignment, const MemoryTag tag) {
476
461
477
462
AllocationGuardSpinLockHolder lock_holder (&spinlock);
478
463
479
- InitSystemAllocatorIfNecessary ();
480
-
481
- auto [result, actual_bytes] = region_manager->Alloc (bytes, alignment, tag);
464
+ auto [result, actual_bytes] = region_manager.Alloc (bytes, alignment, tag);
482
465
483
466
if (result != nullptr ) {
484
467
CheckAddressBits<kAddressBits >(reinterpret_cast <uintptr_t >(result) +
@@ -610,14 +593,12 @@ bool SystemRelease(void* start, size_t length) {
610
593
611
594
AddressRegionFactory* GetRegionFactory () {
612
595
AllocationGuardSpinLockHolder lock_holder (&spinlock);
613
- InitSystemAllocatorIfNecessary ();
614
596
return region_factory;
615
597
}
616
598
617
599
void SetRegionFactory (AddressRegionFactory* factory) {
618
600
AllocationGuardSpinLockHolder lock_holder (&spinlock);
619
- InitSystemAllocatorIfNecessary ();
620
- region_manager->DiscardMappedRegions ();
601
+ region_manager.DiscardMappedRegions ();
621
602
region_factory = factory;
622
603
}
623
604
0 commit comments