Skip to content

Commit f852823

Browse files
ckennellycopybara-github
authored andcommitted
Initialize system allocator at compile time.
PiperOrigin-RevId: 712948375 Change-Id: I3944ef9e4910103f28712c0f92b27e706817f85d
1 parent b0da6cd commit f852823

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

tcmalloc/system-alloc.cc

+12-31
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,6 @@ static_assert(sizeof(MemoryAligner) < kMinSystemAlloc,
121121
ABSL_CONST_INIT absl::base_internal::SpinLock spinlock(
122122
absl::kConstInit, absl::base_internal::SCHEDULE_KERNEL_ONLY);
123123

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-
131124
// Rounds size down to a multiple of alignment.
132125
size_t RoundDown(const size_t size, const size_t alignment) {
133126
// Checks that the alignment has only one bit set.
@@ -170,8 +163,14 @@ class MmapRegionFactory final : public AddressRegionFactory {
170163
TCMALLOC_ATTRIBUTE_NO_DESTROY ABSL_CONST_INIT MmapRegionFactory mmap_factory
171164
ABSL_GUARDED_BY(spinlock);
172165

166+
// The current region factory.
167+
ABSL_CONST_INIT AddressRegionFactory* region_factory ABSL_GUARDED_BY(spinlock) =
168+
&mmap_factory;
169+
173170
class RegionManager {
174171
public:
172+
constexpr RegionManager() = default;
173+
175174
std::pair<void*, size_t> Alloc(size_t size, size_t alignment, MemoryTag tag)
176175
ABSL_EXCLUSIVE_LOCKS_REQUIRED(spinlock);
177176

@@ -197,19 +196,16 @@ class RegionManager {
197196
AddressRegion* cold_region_{nullptr};
198197
AddressRegion* metadata_region_{nullptr};
199198
};
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);
205201

206202
std::pair<void*, size_t> MmapRegion::Alloc(size_t request_size,
207203
size_t alignment) {
208204
// Align on kMinSystemAlloc boundaries to reduce external fragmentation for
209205
// future allocations.
210206
size_t size = RoundUp(request_size, kMinSystemAlloc);
211207
if (size < request_size) return {nullptr, 0};
212-
alignment = std::max(alignment, preferred_alignment);
208+
alignment = std::max(alignment, kMinSystemAlloc);
213209

214210
// Tries to allocate size bytes from the end of [start_, start_ + free_size_),
215211
// aligned to alignment.
@@ -314,7 +310,7 @@ std::pair<void*, size_t> RegionManager::Alloc(size_t request_size,
314310
// future allocations.
315311
size_t size = RoundUp(request_size, kMinSystemAlloc);
316312
if (size < request_size) return {nullptr, 0};
317-
alignment = std::max(alignment, preferred_alignment);
313+
alignment = std::max(alignment, kMinSystemAlloc);
318314
void* ptr = MmapAligned(size, alignment, tag);
319315
if (!ptr) return {nullptr, 0};
320316

@@ -378,17 +374,6 @@ std::pair<void*, size_t> RegionManager::Allocate(size_t size, size_t alignment,
378374
return region->Alloc(size, alignment);
379375
}
380376

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 (&region_manager_space) RegionManager();
389-
region_factory = &mmap_factory;
390-
}
391-
392377
// Bind the memory region spanning `size` bytes starting from `base` to NUMA
393378
// nodes assigned to `partition`. Returns zero upon success, or a standard
394379
// error code upon failure.
@@ -476,9 +461,7 @@ AddressRange SystemAlloc(size_t bytes, size_t alignment, const MemoryTag tag) {
476461

477462
AllocationGuardSpinLockHolder lock_holder(&spinlock);
478463

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);
482465

483466
if (result != nullptr) {
484467
CheckAddressBits<kAddressBits>(reinterpret_cast<uintptr_t>(result) +
@@ -610,14 +593,12 @@ bool SystemRelease(void* start, size_t length) {
610593

611594
AddressRegionFactory* GetRegionFactory() {
612595
AllocationGuardSpinLockHolder lock_holder(&spinlock);
613-
InitSystemAllocatorIfNecessary();
614596
return region_factory;
615597
}
616598

617599
void SetRegionFactory(AddressRegionFactory* factory) {
618600
AllocationGuardSpinLockHolder lock_holder(&spinlock);
619-
InitSystemAllocatorIfNecessary();
620-
region_manager->DiscardMappedRegions();
601+
region_manager.DiscardMappedRegions();
621602
region_factory = factory;
622603
}
623604

0 commit comments

Comments
 (0)