Skip to content

Commit

Permalink
System: Remove redundant remap
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Aug 4, 2024
1 parent 02fbfae commit c3bf267
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
19 changes: 11 additions & 8 deletions src/core/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static u8** s_fastmem_lut = nullptr;

static bool s_kernel_initialize_hook_run = false;

static bool AllocateMemoryMap(Error* error);
static bool AllocateMemoryMap(bool export_shared_memory, Error* error);
static void ReleaseMemoryMap();
static void SetRAMSize(bool enable_8mb_ram);

Expand Down Expand Up @@ -197,11 +197,14 @@ static constexpr size_t TOTAL_SIZE = LUT_OFFSET + LUT_SIZE;
#define FIXUP_WORD_WRITE_VALUE(size, offset, value) \
((size == MemoryAccessSize::Word) ? (value) : ((value) << (((offset) & 3u) * 8)))

bool Bus::AllocateMemoryMap(Error* error)
bool Bus::AllocateMemoryMap(bool export_shared_memory, Error* error)
{
// This executes super early in process startup, therefore export_shared_memory will always be false.
if (g_settings.export_shared_memory)
INFO_LOG("Allocating{} shared memory map.", export_shared_memory ? " EXPORTED" : "");
if (export_shared_memory)
{
s_shmem_name = MemMap::GetFileMappingName("duckstation");
INFO_LOG("Shared memory object name is \"{}\".", s_shmem_name);
}
s_shmem_handle = MemMap::CreateSharedMemory(s_shmem_name.c_str(), MemoryMap::TOTAL_SIZE, error);
if (!s_shmem_handle)
{
Expand Down Expand Up @@ -304,9 +307,9 @@ void Bus::ReleaseMemoryMap()
}
}

bool Bus::AllocateMemory(Error* error)
bool Bus::AllocateMemory(bool export_shared_memory, Error* error)
{
if (!AllocateMemoryMap(error))
if (!AllocateMemoryMap(export_shared_memory, error))
return false;

#ifdef ENABLE_MMAP_FASTMEM
Expand Down Expand Up @@ -336,7 +339,7 @@ void Bus::ReleaseMemory()
ReleaseMemoryMap();
}

bool Bus::ReallocateMemoryMap(Error* error)
bool Bus::ReallocateMemoryMap(bool export_shared_memory, Error* error)
{
// Need to back up RAM+BIOS.
DynamicHeapArray<u8> ram_backup;
Expand All @@ -354,7 +357,7 @@ bool Bus::ReallocateMemoryMap(Error* error)
}

ReleaseMemoryMap();
if (!AllocateMemoryMap(error)) [[unlikely]]
if (!AllocateMemoryMap(export_shared_memory, error)) [[unlikely]]
return false;

if (System::IsValid())
Expand Down
4 changes: 2 additions & 2 deletions src/core/bus.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ enum : u32
static constexpr size_t FASTMEM_ARENA_SIZE = UINT64_C(0x100000000);
#endif

bool AllocateMemory(Error* error);
bool AllocateMemory(bool export_shared_memory, Error* error);
void ReleaseMemory();

/// Frees and re-allocates the memory map for the process.
/// This should be called when shared memory exports are enabled.
bool ReallocateMemoryMap(Error* error);
bool ReallocateMemoryMap(bool export_shared_memory, Error* error);

/// Cleans up/deletes the shared memory object for this process.
/// Should be called when the process crashes, to avoid leaking.
Expand Down
1 change: 1 addition & 0 deletions src/core/game_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ void GameDatabase::Entry::ApplySettings(Settings& settings, bool display_osd_mes
Host::OSD_WARNING_DURATION);
}

#undef APPEND_MESSAGE_FMT
#undef APPEND_MESSAGE

#define BIT_FOR(ctype) (static_cast<u16>(1) << static_cast<u32>(ctype))
Expand Down
14 changes: 5 additions & 9 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,11 @@ bool System::Internal::ProcessStartup(Error* error)
if (!CPU::CodeCache::ProcessStartup(error))
return false;

// g_settings is not valid at this point, query global config directly.
const bool export_shared_memory = Host::GetBoolSettingValue("Hacks", "ExportSharedMemory", false);

// Fastmem alloc *must* come after JIT alloc, otherwise it tends to eat the 4GB region after the executable on MacOS.
if (!Bus::AllocateMemory(error))
if (!Bus::AllocateMemory(export_shared_memory, error))
{
CPU::CodeCache::ProcessShutdown();
return false;
Expand Down Expand Up @@ -439,13 +442,6 @@ bool System::Internal::CPUThreadInitialize(Error* error)
// This will call back to Host::LoadSettings() -> ReloadSources().
LoadSettings(false);

// Yuckity yuck. We have to test for memory export explicitly, because the allocation happens before config load.
if (g_settings.export_shared_memory && !Bus::ReallocateMemoryMap(error))
{
Error::AddPrefix(error, "Failed to reallocate memory map:\n");
return false;
}

#ifdef ENABLE_RAINTEGRATION
if (Host::GetBaseBoolSettingValue("Cheevos", "UseRAIntegration", false))
Achievements::SwitchToRAIntegration();
Expand Down Expand Up @@ -4367,7 +4363,7 @@ void System::CheckForSettingsChanges(const Settings& old_settings)
if (g_settings.export_shared_memory != old_settings.export_shared_memory) [[unlikely]]
{
Error error;
if (!Bus::ReallocateMemoryMap(&error)) [[unlikely]]
if (!Bus::ReallocateMemoryMap(g_settings.export_shared_memory, &error)) [[unlikely]]
{
ERROR_LOG(error.GetDescription());
Panic("Failed to reallocate memory map. The log may contain more information.");
Expand Down

0 comments on commit c3bf267

Please sign in to comment.