Common/MemoryArena: Add destroy/valid methods

This commit is contained in:
Connor McLaughlin 2020-12-26 16:47:56 +10:00
parent e71f6aa80a
commit d74d27163c
2 changed files with 32 additions and 7 deletions

View File

@ -63,13 +63,7 @@ MemoryArena::MemoryArena() = default;
MemoryArena::~MemoryArena() MemoryArena::~MemoryArena()
{ {
#if defined(WIN32) Destroy();
if (m_file_handle)
CloseHandle(m_file_handle);
#elif defined(__linux__)
if (m_shmem_fd > 0)
close(m_shmem_fd);
#endif
} }
void* MemoryArena::FindBaseAddressForMapping(size_t size) void* MemoryArena::FindBaseAddressForMapping(size_t size)
@ -100,8 +94,20 @@ void* MemoryArena::FindBaseAddressForMapping(size_t size)
return base_address; return base_address;
} }
bool MemoryArena::IsValid() const
{
#if defined(WIN32)
return m_file_handle != nullptr;
#else
return m_shmem_fd >= 0;
#endif
}
bool MemoryArena::Create(size_t size, bool writable, bool executable) bool MemoryArena::Create(size_t size, bool writable, bool executable)
{ {
if (IsValid())
Destroy();
#if defined(WIN32) #if defined(WIN32)
const std::string file_mapping_name = StringUtil::StdStringFromFormat("duckstation_%u", GetCurrentProcessId()); const std::string file_mapping_name = StringUtil::StdStringFromFormat("duckstation_%u", GetCurrentProcessId());
@ -173,6 +179,23 @@ bool MemoryArena::Create(size_t size, bool writable, bool executable)
#endif #endif
} }
void MemoryArena::Destroy()
{
#if defined(WIN32)
if (m_file_handle)
{
CloseHandle(m_file_handle);
m_file_handle = nullptr;
}
#elif defined(__linux__)
if (m_shmem_fd > 0)
{
close(m_shmem_fd);
m_shmem_fd = -1;
}
#endif
}
std::optional<MemoryArena::View> MemoryArena::CreateView(size_t offset, size_t size, bool writable, bool executable, std::optional<MemoryArena::View> MemoryArena::CreateView(size_t offset, size_t size, bool writable, bool executable,
void* fixed_address) void* fixed_address)
{ {

View File

@ -32,7 +32,9 @@ public:
static void* FindBaseAddressForMapping(size_t size); static void* FindBaseAddressForMapping(size_t size);
bool IsValid() const;
bool Create(size_t size, bool writable, bool executable); bool Create(size_t size, bool writable, bool executable);
void Destroy();
std::optional<View> CreateView(size_t offset, size_t size, bool writable, bool executable, std::optional<View> CreateView(size_t offset, size_t size, bool writable, bool executable,
void* fixed_address = nullptr); void* fixed_address = nullptr);