Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tcmalloc/static_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ ABSL_CONST_INIT NumaTopology<kNumaPartitions, kNumBaseClasses>
Static::numa_topology_;
ABSL_CONST_INIT GwpAsanState Static::gwp_asan_state_;
ABSL_CONST_INIT Static::PerSizeClassCounts Static::per_size_class_counts_;
TCMALLOC_ATTRIBUTE_NO_DESTROY ABSL_CONST_INIT SystemAllocator<
NumaTopology<kNumaPartitions, kNumBaseClasses>, kNormalPartitions>
Static::system_allocator_{numa_topology_, kMinMmapAlloc};
TCMALLOC_ATTRIBUTE_NO_DESTROY ABSL_CONST_INIT
Static::SystemAllocatorStorage Static::system_allocator_{numa_topology_,
kMinMmapAlloc};
// Force kInvalidSpan to be read-protected. Span contains a std::atomic, and
// libc++'s std::atomic implementation contains a mutable field in one of its
// implementation details. This prevents Span from being placed in a read-only
Expand Down
23 changes: 20 additions & 3 deletions tcmalloc/static_vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class Static final {
static SystemAllocator<NumaTopology<kNumaPartitions, kNumBaseClasses>,
kNormalPartitions>&
system_allocator() {
return system_allocator_;
return system_allocator_.allocator;
}

static Arena& arena() { return arena_; }
Expand Down Expand Up @@ -252,8 +252,25 @@ class Static final {
static PageAllocatorStorage page_allocator_;
static PageMap pagemap_;

ABSL_CONST_INIT static SystemAllocator<
NumaTopology<kNumaPartitions, kNumBaseClasses>, kNormalPartitions>
// Avoid destruction of SystemAllocator in a way that works with constinit and
// C++17. Once C++17 support is dropped this can be replaced with
// absl::NoDestructor. absl::NoDestructor uses a placement new, which doesn't
// work with constinit in C++17. Destruction is especially dangerous here
// because it can lead to race conditions and crashes on shutdown.
union SystemAllocatorStorage {
constexpr SystemAllocatorStorage(
const NumaTopology<kNumaPartitions, kNumBaseClasses>& topology,
size_t min_mmap_size)
: allocator(topology, min_mmap_size) {}

~SystemAllocatorStorage() {}

SystemAllocator<NumaTopology<kNumaPartitions, kNumBaseClasses>,
kNormalPartitions>
allocator;
};

TCMALLOC_ATTRIBUTE_NO_DESTROY ABSL_CONST_INIT static SystemAllocatorStorage
system_allocator_;

static ABSL_ATTRIBUTE_SECTION_VARIABLE(.data.rel.ro) const Span kInvalidSpan;
Expand Down
Loading