Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemArena: Add Darwin implementation #13269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions Source/Core/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ elseif(WIN32)
Logging/ConsoleListenerWin.cpp
MemArenaWin.cpp
)
elseif(APPLE)
target_sources(common PRIVATE
Logging/ConsoleListenerNix.cpp
MemArenaDarwin.cpp
)
else()
target_sources(common PRIVATE
Logging/ConsoleListenerNix.cpp
Expand Down
11 changes: 11 additions & 0 deletions Source/Core/Common/MemArena.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <string_view>
#include <vector>

#ifdef __APPLE__
#include <mach/mach.h>
#endif

#include "Common/CommonTypes.h"
#include "Common/DynamicLibrary.h"

Expand Down Expand Up @@ -120,6 +124,13 @@ class MemArena final
void* m_reserved_region = nullptr;
void* m_memory_handle = nullptr;
WindowsMemoryFunctions m_memory_functions;
#elif defined(__APPLE__)
vm_address_t m_shm_address = 0;
vm_size_t m_shm_size = 0;
mem_entry_name_port_t m_shm_entry = MACH_PORT_NULL;

vm_address_t m_region_address = 0;
vm_size_t m_region_size = 0;
#else
int m_shm_fd = 0;
void* m_reserved_region = nullptr;
Expand Down
225 changes: 225 additions & 0 deletions Source/Core/Common/MemArenaDarwin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/MemArena.h"

#include "Common/Assert.h"
#include "Common/Logging/Log.h"

namespace Common
{
MemArena::MemArena() = default;
MemArena::~MemArena() = default;

void MemArena::GrabSHMSegment(size_t size, std::string_view base_name)
{
kern_return_t retval = vm_allocate(mach_task_self(), &m_shm_address, size, VM_FLAGS_ANYWHERE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "GrabSHMSegment failed: vm_allocate returned {0:#x}", retval);

m_shm_address = 0;

return;
}

memory_object_size_t entry_size = size;
constexpr vm_prot_t prot = VM_PROT_READ | VM_PROT_WRITE;

retval = mach_make_memory_entry_64(mach_task_self(), &entry_size, m_shm_address, prot,
&m_shm_entry, MACH_PORT_NULL);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "GrabSHMSegment failed: mach_make_memory_entry_64 returned {0:#x}",
retval);

m_shm_address = 0;
m_shm_entry = MACH_PORT_NULL;

return;
}

m_shm_size = size;
}

void MemArena::ReleaseSHMSegment()
{
if (m_shm_entry != MACH_PORT_NULL)
{
mach_port_deallocate(mach_task_self(), m_shm_entry);
}

if (m_shm_address != 0)
{
vm_deallocate(mach_task_self(), m_shm_address, m_shm_size);
}

m_shm_address = 0;
m_shm_size = 0;
m_shm_entry = MACH_PORT_NULL;
}

void* MemArena::CreateView(s64 offset, size_t size)
{
if (m_shm_address == 0)
{
ERROR_LOG_FMT(MEMMAP, "CreateView failed: no shared memory segment allocated");
return nullptr;
}

vm_address_t address;
constexpr vm_prot_t prot = VM_PROT_READ | VM_PROT_WRITE;

kern_return_t retval = vm_map(mach_task_self(), &address, size, 0, VM_FLAGS_ANYWHERE, m_shm_entry,
offset, false, prot, prot, VM_INHERIT_DEFAULT);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "CreateView failed: vm_map returned {0:#x}", retval);
return nullptr;
}

return reinterpret_cast<void*>(address);
}

void MemArena::ReleaseView(void* view, size_t size)
{
vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(view), size);
}

u8* MemArena::ReserveMemoryRegion(size_t memory_size)
{
vm_address_t address;

kern_return_t retval = vm_allocate(mach_task_self(), &address, memory_size, VM_FLAGS_ANYWHERE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "ReserveMemoryRegion: vm_allocate returned {0:#x}", retval);
return nullptr;
}

retval = vm_protect(mach_task_self(), address, memory_size, true, VM_PROT_NONE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "ReserveMemoryRegion failed: vm_prot returned {0:#x}", retval);
return nullptr;
}

m_region_address = address;
m_region_size = memory_size;

return reinterpret_cast<u8*>(m_region_address);
}

void MemArena::ReleaseMemoryRegion()
{
if (m_region_address != 0)
{
vm_deallocate(mach_task_self(), m_region_address, m_region_size);
}

m_region_address = 0;
m_region_size = 0;
}

void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
{
if (m_shm_address == 0)
{
ERROR_LOG_FMT(MEMMAP, "MapInMemoryRegion failed: no shared memory segment allocated");
return nullptr;
}

vm_address_t address = reinterpret_cast<vm_address_t>(base);
constexpr vm_prot_t prot = VM_PROT_READ | VM_PROT_WRITE;

kern_return_t retval =
vm_map(mach_task_self(), &address, size, 0, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, m_shm_entry,
offset, false, prot, prot, VM_INHERIT_DEFAULT);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "MapInMemoryRegion failed: vm_map returned {0:#x}", retval);
return nullptr;
}

return reinterpret_cast<void*>(address);
}

void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
{
vm_address_t address = reinterpret_cast<vm_address_t>(view);

kern_return_t retval =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "UnmapFromMemoryRegion failed: vm_allocate returned {0:#x}", retval);
return;
}

retval = vm_protect(mach_task_self(), address, size, true, VM_PROT_NONE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "UnmapFromMemoryRegion failed: vm_prot returned {0:#x}", retval);
}
}

LazyMemoryRegion::LazyMemoryRegion() = default;

LazyMemoryRegion::~LazyMemoryRegion()
{
Release();
}

void* LazyMemoryRegion::Create(size_t size)
{
ASSERT(!m_memory);

if (size == 0)
return nullptr;

vm_address_t memory = 0;

kern_return_t retval = vm_allocate(mach_task_self(), &memory, size, VM_FLAGS_ANYWHERE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "Failed to allocate memory space: {0:#x}", retval);
return nullptr;
}

m_memory = reinterpret_cast<void*>(memory);
m_size = size;

return m_memory;
}

void LazyMemoryRegion::Clear()
{
ASSERT(m_memory);

vm_address_t new_memory = reinterpret_cast<vm_address_t>(m_memory);

kern_return_t retval =
vm_allocate(mach_task_self(), &new_memory, m_size, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE);
if (retval != KERN_SUCCESS)
{
ERROR_LOG_FMT(MEMMAP, "Failed to reallocate memory space: {0:#x}", retval);

m_memory = nullptr;

return;
}

m_memory = reinterpret_cast<void*>(new_memory);
}

void LazyMemoryRegion::Release()
{
if (m_memory)
{
vm_deallocate(mach_task_self(), reinterpret_cast<vm_address_t>(m_memory), m_size);
}

m_memory = nullptr;
m_size = 0;
}
} // namespace Common