Skip to content

Commit b324cfb

Browse files
authored
Merge pull request #5215 from Sonicadvance1/50
FEXInterpreter: Removes three static objects
2 parents 28c486d + 7047d08 commit b324cfb

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

Source/Tools/FEXInterpreter/FEXInterpreter.cpp

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,40 +143,44 @@ void Init() {
143143
} // namespace FEX::Logging
144144

145145
namespace FEX::Allocator {
146-
static fextl::unique_ptr<FEX::HLE::MemAllocator> Allocator;
147-
static fextl::vector<FEXCore::Allocator::MemoryRegion> Base48Bit;
148-
static fextl::vector<FEXCore::Allocator::MemoryRegion> Low4GB;
149146

150-
void Init(bool Is64Bit) {
147+
fextl::vector<FEXCore::Allocator::MemoryRegion> InitMemoryRegions(bool Is64Bit) {
151148
if (Is64Bit) {
152149
// Destroy the 48th bit if it exists
153-
Base48Bit = FEXCore::Allocator::Setup48BitAllocatorIfExists();
154-
} else {
155-
// Reserve [0x1_0000_0000, 0x2_0000_0000).
156-
// Safety net if 32-bit address calculation overflows in to 64-bit range.
157-
constexpr uint64_t First64BitAddr = 0x1'0000'0000ULL;
158-
Low4GB = FEXCore::Allocator::StealMemoryRegion(First64BitAddr, First64BitAddr + First64BitAddr);
159-
160-
// Setup our userspace allocator
161-
FEXCore::Allocator::SetupHooks();
162-
Allocator = FEX::HLE::CreatePassthroughAllocator();
163-
164-
// Now that the upper 32-bit address space is blocked for future allocations,
165-
// exhaust all of jemalloc's remaining internal allocations that it reserved before.
166-
// TODO: It's unclear how reliably this exhausts those reserves
167-
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc;
168-
void* data;
169-
do {
170-
data = malloc(0x1);
171-
} while (reinterpret_cast<uintptr_t>(data) >> 32 != 0);
172-
free(data);
150+
return FEXCore::Allocator::Setup48BitAllocatorIfExists();
173151
}
152+
153+
// Reserve [0x1_0000_0000, 0x2_0000_0000).
154+
// Safety net if 32-bit address calculation overflows in to 64-bit range.
155+
constexpr uint64_t First64BitAddr = 0x1'0000'0000ULL;
156+
return FEXCore::Allocator::StealMemoryRegion(First64BitAddr, First64BitAddr + First64BitAddr);
157+
}
158+
159+
fextl::unique_ptr<FEX::HLE::MemAllocator> InitAllocator(bool Is64Bit) {
160+
if (Is64Bit) {
161+
return {};
162+
}
163+
164+
// Setup our userspace allocator
165+
FEXCore::Allocator::SetupHooks();
166+
auto Allocator = FEX::HLE::CreatePassthroughAllocator();
167+
168+
// Now that the upper 32-bit address space is blocked for future allocations,
169+
// exhaust all of jemalloc's remaining internal allocations that it reserved before.
170+
// TODO: It's unclear how reliably this exhausts those reserves
171+
FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc;
172+
void* data;
173+
do {
174+
data = malloc(0x1);
175+
} while (reinterpret_cast<uintptr_t>(data) >> 32 != 0);
176+
free(data);
177+
178+
return Allocator;
174179
}
175180

176-
void Shutdown() {
181+
void Shutdown(fextl::vector<FEXCore::Allocator::MemoryRegion>&& MemoryRegions) {
177182
FEXCore::Allocator::ClearHooks();
178-
FEXCore::Allocator::ReclaimMemoryRegion(Base48Bit);
179-
FEXCore::Allocator::ReclaimMemoryRegion(Low4GB);
183+
FEXCore::Allocator::ReclaimMemoryRegion(MemoryRegions);
180184
}
181185
} // namespace FEX::Allocator
182186

@@ -504,7 +508,8 @@ int main(int argc, char** argv, char** const envp) {
504508
// Setup Thread handlers, so FEXCore can create threads.
505509
auto StackTracker = FEX::LinuxEmulation::Threads::SetupThreadHandlers();
506510

507-
FEX::Allocator::Init(Loader.Is64BitMode());
511+
auto MemoryRegions = FEX::Allocator::InitMemoryRegions(Loader.Is64BitMode());
512+
auto Allocator = FEX::Allocator::InitAllocator(Loader.Is64BitMode());
508513

509514
FEXCore::Profiler::Init(Program.ProgramName, Program.ProgramPath);
510515

@@ -521,9 +526,9 @@ int main(int argc, char** argv, char** const envp) {
521526
auto SignalDelegation = FEX::HLE::CreateSignalDelegator(CTX.get(), Program.ProgramName, SupportsAVX);
522527
auto ThunkHandler = FEX::HLE::CreateThunkHandler();
523528

524-
auto SyscallHandler = Loader.Is64BitMode() ? FEX::HLE::x64::CreateHandler(CTX.get(), SignalDelegation.get(), ThunkHandler.get()) :
525-
FEX::HLE::x32::CreateHandler(CTX.get(), SignalDelegation.get(), ThunkHandler.get(),
526-
std::move(FEX::Allocator::Allocator));
529+
auto SyscallHandler = Loader.Is64BitMode() ?
530+
FEX::HLE::x64::CreateHandler(CTX.get(), SignalDelegation.get(), ThunkHandler.get()) :
531+
FEX::HLE::x32::CreateHandler(CTX.get(), SignalDelegation.get(), ThunkHandler.get(), std::move(Allocator));
527532
SyscallHandler->SetCodeLoader(&Loader);
528533
CTX->SetSignalDelegator(SignalDelegation.get());
529534
CTX->SetSyscallHandler(SyscallHandler.get());
@@ -619,7 +624,7 @@ int main(int argc, char** argv, char** const envp) {
619624
LogMan::Throw::UnInstallHandler();
620625
LogMan::Msg::UnInstallHandler();
621626

622-
FEX::Allocator::Shutdown();
627+
FEX::Allocator::Shutdown(std::move(MemoryRegions));
623628

624629
// Allocator is now original system allocator
625630
FEXCore::Telemetry::Shutdown(Program.ProgramName);

0 commit comments

Comments
 (0)