apply StaticStorage across the source code

This commit is contained in:
Aliaksey Kandratsenka 2024-03-27 14:53:34 -04:00
parent ee123a1a0d
commit fa3753dc86
11 changed files with 49 additions and 89 deletions

View File

@ -37,8 +37,9 @@
#include "base/low_level_alloc.h"
#include "base/dynamic_annotations.h"
#include "base/spinlock.h"
#include "base/logging.h"
#include "base/spinlock.h"
#include "base/static_storage.h"
#include "malloc_hook-inl.h"
#include <gperftools/malloc_hook.h>
@ -524,18 +525,14 @@ LowLevelAlloc::Arena *LowLevelAlloc::DefaultArena() {
return &default_arena;
}
static DefaultPagesAllocator *default_pages_allocator;
static union {
char chars[sizeof(DefaultPagesAllocator)];
void *ptr;
} debug_pages_allocator_space;
static tcmalloc::StaticStorage<DefaultPagesAllocator> default_pages_allocator;
LowLevelAlloc::PagesAllocator *LowLevelAlloc::GetDefaultPagesAllocator(void) {
if (default_pages_allocator) {
return default_pages_allocator;
}
default_pages_allocator = new (debug_pages_allocator_space.chars) DefaultPagesAllocator();
return default_pages_allocator;
static tcmalloc::TrivialOnce once;
once.RunOnce(+[] () {
default_pages_allocator.Construct();
});
return default_pages_allocator.get();
}
void *DefaultPagesAllocator::MapPages(int32_t flags, size_t size) {

View File

@ -71,6 +71,7 @@
#include "base/googleinit.h"
#include "base/logging.h"
#include "base/spinlock.h"
#include "base/static_storage.h"
#include "base/threading.h"
#include "malloc_hook-inl.h"
#include "maybe_emergency_malloc.h"
@ -697,9 +698,9 @@ class MallocBlock {
// We don't want to allocate or deallocate memory here, so we use
// placement-new. It's ok that we don't destroy this, since we're
// just going to error-exit below anyway. Union is for alignment.
union { void* alignment; char buf[sizeof(SymbolTable)]; } tablebuf;
SymbolTable* symbolization_table = new (tablebuf.buf) SymbolTable;
// just going to error-exit below anyway.
tcmalloc::StaticStorage<SymbolTable> tablebuf;
SymbolTable* symbolization_table = tablebuf.Construct();
for (int i = 0; i < queue_entry.num_deleter_pcs; i++) {
// Symbolizes the previous address of pc because pc may be in the
// next function. This may happen when the function ends with
@ -1071,10 +1072,8 @@ class DebugMallocImplementation : public TCMallocImplementation {
virtual bool GetNumericProperty(const char* name, size_t* value) {
if (TestingPortal** portal = TestingPortal::CheckGetPortal(name, value); portal) {
static DebugTestingPortal* ptr = ([] () {
static struct {
alignas(DebugTestingPortal) char memory[sizeof(DebugTestingPortal)];
} storage;
return new (storage.memory) DebugTestingPortal;
static tcmalloc::StaticStorage<DebugTestingPortal> storage;
return storage.Construct();
})();
*portal = ptr;
*value = 1;
@ -1177,11 +1176,10 @@ class DebugMallocImplementation : public TCMallocImplementation {
};
static tcmalloc::StaticStorage<DebugMallocImplementation> debug_malloc_impl_storage;
void SetupMallocExtension() {
static struct {
alignas(DebugMallocImplementation) char memory[sizeof(DebugMallocImplementation)];
} storage;
MallocExtension::Register(new (storage.memory) DebugMallocImplementation);
MallocExtension::Register(debug_malloc_impl_storage.Construct());
}
REGISTER_MODULE_DESTRUCTOR(debugallocation, {

View File

@ -56,6 +56,7 @@
#include <gperftools/malloc_extension.h>
#include "base/basictypes.h"
#include "base/googleinit.h"
#include "base/static_storage.h"
#include "base/sysinfo.h"
#include "internal_logging.h"
#include "safe_strerror.h"
@ -114,10 +115,7 @@ private:
SysAllocator* fallback_; // Default system allocator to fall back to.
};
static union {
char buf[sizeof(HugetlbSysAllocator)];
void *ptr;
} hugetlb_space;
static tcmalloc::StaticStorage<HugetlbSysAllocator> hugetlb_space;
// No locking needed here since we assume that tcmalloc calls
// us with an internal lock held (see tcmalloc/system-alloc.cc).
@ -270,8 +268,7 @@ bool HugetlbSysAllocator::Initialize() {
REGISTER_MODULE_INITIALIZER(memfs_malloc, {
if (FLAGS_memfs_malloc_path.length()) {
SysAllocator* alloc = MallocExtension::instance()->GetSystemAllocator();
HugetlbSysAllocator* hp =
new (hugetlb_space.buf) HugetlbSysAllocator(alloc);
HugetlbSysAllocator* hp = hugetlb_space.Construct(alloc);
if (hp->Initialize()) {
MallocExtension::instance()->SetSystemAllocator(hp);
}

View File

@ -163,20 +163,6 @@ static inline bool current_thread_is(std::thread::id should_be) {
// ========================================================================= //
// Constructor-less place-holder to store a RegionSet in.
union MemoryRegionMap::RegionSetRep {
char rep[sizeof(RegionSet)];
void* align_it; // do not need a better alignment for 'rep' than this
RegionSet* region_set() { return reinterpret_cast<RegionSet*>(rep); }
};
// The bytes where MemoryRegionMap::regions_ will point to.
// We use RegionSetRep with noop c-tor so that global construction
// does not interfere.
static MemoryRegionMap::RegionSetRep regions_rep;
// ========================================================================= //
// Has InsertRegionLocked been called recursively
// (or rather should we *not* use regions_ to record a hooked mmap).
static bool recursive_insert = false;
@ -527,7 +513,7 @@ void MemoryRegionMap::RestoreSavedBucketsLocked() {
inline void MemoryRegionMap::InitRegionSetLocked() {
RAW_VLOG(12, "Initializing region set");
regions_ = regions_rep.region_set();
regions_ = regions_rep_.get();
recursive_insert = true;
new (regions_) RegionSet();
HandleSavedRegionsLocked(&DoInsertRegionLocked);

View File

@ -257,6 +257,11 @@ class MemoryRegionMap {
typedef std::set<Region, RegionCmp,
STL_Allocator<Region, MyAllocator> > RegionSet;
// The bytes where MemoryRegionMap::regions_ will point to. We use
// StaticStorage with noop c-tor so that global construction does
// not interfere.
static inline tcmalloc::StaticStorage<RegionSet> regions_rep_;
public: // more in-depth interface ==========================================
// STL iterator with values of Region
@ -275,11 +280,6 @@ class MemoryRegionMap {
// Return the accumulated sizes of mapped and unmapped regions.
static int64_t MapSize() { return map_size_; }
static int64_t UnmapSize() { return unmap_size_; }
// Effectively private type from our .cc =================================
// public to let us declare global objects:
union RegionSetRep;
private:
// representation ===========================================================

View File

@ -53,6 +53,7 @@ PageHeapAllocator<Span> Static::span_allocator_;
PageHeapAllocator<StackTrace> Static::stacktrace_allocator_;
Span Static::sampled_objects_;
std::atomic<StackTrace*> Static::growth_stacks_;
StaticStorage<PageHeap> Static::pageheap_;
void Static::InitStaticVars() {
sizemap_.Init();
@ -65,7 +66,7 @@ void Static::InitStaticVars() {
central_cache_[i].Init(i);
}
new (&pageheap_.memory) PageHeap(sizemap_.min_span_size_in_pages());
new (pageheap()) PageHeap(sizemap_.min_span_size_in_pages());
#if defined(ENABLE_AGGRESSIVE_DECOMMIT_BY_DEFAULT)
const bool kDefaultAggressiveDecommit = true;

View File

@ -43,6 +43,7 @@
#include "base/basictypes.h"
#include "base/spinlock.h"
#include "base/static_storage.h"
#include "central_freelist.h"
#include "common.h"
#include "page_heap.h"
@ -74,7 +75,7 @@ class Static {
// must be protected by pageheap_lock.
// Page-level allocator.
static PageHeap* pageheap() { return reinterpret_cast<PageHeap *>(&pageheap_.memory); }
static PageHeap* pageheap() { return pageheap_.get(); }
static PageHeapAllocator<Span>* span_allocator() { return &span_allocator_; }
@ -100,10 +101,7 @@ class Static {
static bool IsInited() { return inited_; }
private:
// some unit tests depend on this and link to static vars
// imperfectly. Thus we keep those unhidden for now. Thankfully
// they're not performance-critical.
/* ATTRIBUTE_HIDDEN */ static bool inited_;
ATTRIBUTE_HIDDEN static bool inited_;
// These static variables require explicit initialization. We cannot
// count on their constructors to do any initialization because other
@ -122,9 +120,7 @@ class Static {
// is stored in trace->stack[kMaxStackDepth-1].
ATTRIBUTE_HIDDEN static std::atomic<StackTrace*> growth_stacks_;
/* ATTRIBUTE_HIDDEN */ static inline struct {
alignas(alignof(PageHeap)) std::byte memory[sizeof(PageHeap)];
} pageheap_;
ATTRIBUTE_HIDDEN static StaticStorage<PageHeap> pageheap_;
};
} // namespace tcmalloc

View File

@ -47,6 +47,7 @@
#include "base/basictypes.h"
#include "base/commandlineflags.h"
#include "base/spinlock.h" // for SpinLockHolder, SpinLock, etc
#include "base/static_storage.h"
#include "common.h"
#include "internal_logging.h"
@ -126,10 +127,7 @@ public:
}
void* Alloc(size_t size, size_t *actual_size, size_t alignment);
};
static union {
char buf[sizeof(SbrkSysAllocator)];
void *ptr;
} sbrk_space;
static tcmalloc::StaticStorage<SbrkSysAllocator> sbrk_space;
class MmapSysAllocator : public SysAllocator {
public:
@ -139,10 +137,7 @@ public:
private:
uintptr_t hint_ = 0;
};
static union {
char buf[sizeof(MmapSysAllocator)];
void *ptr;
} mmap_space;
static tcmalloc::StaticStorage<MmapSysAllocator> mmap_space;
class DefaultSysAllocator : public SysAllocator {
public:
@ -169,10 +164,7 @@ class DefaultSysAllocator : public SysAllocator {
SysAllocator* allocs_[kMaxAllocators];
const char* names_[kMaxAllocators];
};
static union {
char buf[sizeof(DefaultSysAllocator)];
void *ptr;
} default_space;
static tcmalloc::StaticStorage<DefaultSysAllocator> default_space;
static const char sbrk_name[] = "SbrkSysAllocator";
static const char mmap_name[] = "MmapSysAllocator";
@ -367,8 +359,8 @@ SysAllocator *tc_get_sysalloc_override(SysAllocator *def)
static bool system_alloc_inited = false;
void InitSystemAllocators(void) {
MmapSysAllocator *mmap = new (mmap_space.buf) MmapSysAllocator();
SbrkSysAllocator *sbrk = new (sbrk_space.buf) SbrkSysAllocator();
MmapSysAllocator *mmap = mmap_space.Construct();
SbrkSysAllocator *sbrk = sbrk_space.Construct();
// In 64-bit debug mode, place the mmap allocator first since it
// allocates pointers that do not fit in 32 bits and therefore gives
@ -377,7 +369,7 @@ void InitSystemAllocators(void) {
// likely to look like pointers and therefore the conservative gc in
// the heap-checker is less likely to misinterpret a number as a
// pointer).
DefaultSysAllocator *sdef = new (default_space.buf) DefaultSysAllocator();
DefaultSysAllocator *sdef = default_space.Construct();
bool want_mmap = kDebugMode && (sizeof(void*) > 4);
#if __sun__
// TODO: solaris has nice but annoying feature that makes it use

View File

@ -622,10 +622,8 @@ public:
static TestingPortalImpl* Get() {
static TestingPortalImpl* ptr = ([] () {
static struct {
alignas(TestingPortalImpl) char memory[sizeof(TestingPortalImpl)];
} storage;
return new (storage.memory) TestingPortalImpl;
static StaticStorage<TestingPortalImpl> storage;
return storage.Construct();
}());
return ptr;
}
@ -1212,11 +1210,10 @@ static TCMallocGuard module_enter_exit_hook;
#ifndef TCMALLOC_USING_DEBUGALLOCATION
static tcmalloc::StaticStorage<TCMallocImplementation> malloc_impl_storage;
void SetupMallocExtension() {
static struct {
alignas(TCMallocImplementation) char memory[sizeof(TCMallocImplementation)];
} storage;
MallocExtension::Register(new (storage.memory) TCMallocImplementation);
MallocExtension::Register(malloc_impl_storage.Construct());
}
#endif // TCMALLOC_USING_DEBUGALLOCATION

View File

@ -86,6 +86,7 @@
#include "base/function_ref.h"
#include "base/cleanup.h"
#include "base/static_storage.h"
#include "tests/testutil.h"
@ -205,13 +206,9 @@ struct OOMAbleSysAlloc : public SysAllocator {
}
};
static union {
char buf[sizeof(OOMAbleSysAlloc)];
void *ptr;
} test_sys_alloc_space;
static OOMAbleSysAlloc* get_test_sys_alloc() {
return reinterpret_cast<OOMAbleSysAlloc*>(&test_sys_alloc_space);
static tcmalloc::StaticStorage<OOMAbleSysAlloc> storage;
return storage.get();
}
void setup_oomable_sys_alloc() {

View File

@ -50,9 +50,8 @@ SpinLock init_cache_lock;
bool init_cache_ready;
ThreadCache* GetInitCache() {
alignas(ThreadCache) static std::byte init_cache_storage[sizeof(ThreadCache)];
return reinterpret_cast<ThreadCache*>(init_cache_storage);
static StaticStorage<ThreadCache> storage;
return storage.get();
}
} // namespace