Tier0: make sure the memalloc singleton initializer is thread safe

Only enter code if the atomic exchange was performed.
This commit is contained in:
Kawe Mazidjatari 2025-01-09 16:56:48 +01:00
parent 716a3efa0c
commit 6827d1ed02

View File

@ -13,20 +13,19 @@
//-----------------------------------------------------------------------------
// Purpose: initialize the global memory allocator singleton pointer
//-----------------------------------------------------------------------------
static bool s_bAllocatorInitialized = false;
static std::atomic_bool s_bAllocatorInitialized = false;
static void InitAllocator()
{
if (!s_bAllocatorInitialized)
{
s_bAllocatorInitialized = true;
const QWORD imageBase = CModule::GetProcessEnvironmentBlock()->ImageBaseAddress;
if (s_bAllocatorInitialized.exchange(true))
return;
CreateGlobalMemAlloc = CModule::GetExportedSymbol(imageBase,
"CreateGlobalMemAlloc").RCast<CStdMemAlloc* (*)(void)>();
const QWORD imageBase = CModule::GetProcessEnvironmentBlock()->ImageBaseAddress;
g_pMemAllocSingleton = CModule::GetExportedSymbol(imageBase,
"g_pMemAllocSingleton").DerefSelf().RCast<CStdMemAlloc*>();
}
CreateGlobalMemAlloc = CModule::GetExportedSymbol(imageBase,
"CreateGlobalMemAlloc").RCast<CStdMemAlloc * (*)(void)>();
g_pMemAllocSingleton = CModule::GetExportedSymbol(imageBase,
"g_pMemAllocSingleton").DerefSelf().RCast<CStdMemAlloc*>();
}
//-----------------------------------------------------------------------------