MemArena: Split into three separate files for each OS.
This commit is contained in:
parent
a29d7625dd
commit
812cc8b632
|
@ -85,7 +85,6 @@ add_library(common
|
||||||
Matrix.h
|
Matrix.h
|
||||||
MD5.cpp
|
MD5.cpp
|
||||||
MD5.h
|
MD5.h
|
||||||
MemArena.cpp
|
|
||||||
MemArena.h
|
MemArena.h
|
||||||
MemoryUtil.cpp
|
MemoryUtil.cpp
|
||||||
MemoryUtil.h
|
MemoryUtil.h
|
||||||
|
@ -185,16 +184,19 @@ if(ANDROID)
|
||||||
AndroidAnalytics.cpp
|
AndroidAnalytics.cpp
|
||||||
AndroidAnalytics.h
|
AndroidAnalytics.h
|
||||||
Logging/ConsoleListenerDroid.cpp
|
Logging/ConsoleListenerDroid.cpp
|
||||||
|
MemArenaAndroid.cpp
|
||||||
)
|
)
|
||||||
elseif(WIN32)
|
elseif(WIN32)
|
||||||
target_sources(common PRIVATE
|
target_sources(common PRIVATE
|
||||||
LdrWatcher.cpp
|
LdrWatcher.cpp
|
||||||
LdrWatcher.h
|
LdrWatcher.h
|
||||||
Logging/ConsoleListenerWin.cpp
|
Logging/ConsoleListenerWin.cpp
|
||||||
|
MemArenaWin.cpp
|
||||||
)
|
)
|
||||||
else()
|
else()
|
||||||
target_sources(common PRIVATE
|
target_sources(common PRIVATE
|
||||||
Logging/ConsoleListenerNix.cpp
|
Logging/ConsoleListenerNix.cpp
|
||||||
|
MemArenaUnix.cpp
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
|
@ -3,35 +3,28 @@
|
||||||
|
|
||||||
#include "Common/MemArena.h"
|
#include "Common/MemArena.h"
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <dlfcn.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <linux/ashmem.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "Common/CommonFuncs.h"
|
#include "Common/CommonFuncs.h"
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
#include "Common/Logging/Log.h"
|
#include "Common/Logging/Log.h"
|
||||||
#include "Common/MsgHandler.h"
|
#include "Common/MsgHandler.h"
|
||||||
#include "Common/StringUtil.h"
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <windows.h>
|
|
||||||
#else
|
|
||||||
#include <cerrno>
|
|
||||||
#include <cstring>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#ifdef ANDROID
|
|
||||||
#include <dlfcn.h>
|
|
||||||
#include <linux/ashmem.h>
|
|
||||||
#include <sys/ioctl.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Common
|
namespace Common
|
||||||
{
|
{
|
||||||
#ifdef ANDROID
|
|
||||||
#define ASHMEM_DEVICE "/dev/ashmem"
|
#define ASHMEM_DEVICE "/dev/ashmem"
|
||||||
|
|
||||||
static int AshmemCreateFileMapping(const char* name, size_t size)
|
static int AshmemCreateFileMapping(const char* name, size_t size)
|
||||||
|
@ -65,50 +58,21 @@ static int AshmemCreateFileMapping(const char* name, size_t size)
|
||||||
}
|
}
|
||||||
return fd;
|
return fd;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
void MemArena::GrabSHMSegment(size_t size)
|
void MemArena::GrabSHMSegment(size_t size)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
const std::string name = "dolphin-emu." + std::to_string(GetCurrentProcessId());
|
|
||||||
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
|
|
||||||
static_cast<DWORD>(size), UTF8ToTStr(name).c_str());
|
|
||||||
#elif defined(ANDROID)
|
|
||||||
fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size);
|
fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
{
|
|
||||||
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
|
NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed");
|
||||||
return;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
const std::string file_name = "/dolphin-emu." + std::to_string(getpid());
|
|
||||||
fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
shm_unlink(file_name.c_str());
|
|
||||||
if (ftruncate(fd, size) < 0)
|
|
||||||
ERROR_LOG_FMT(MEMMAP, "Failed to allocate low memory space");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemArena::ReleaseSHMSegment()
|
void MemArena::ReleaseSHMSegment()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
CloseHandle(hMemoryMapping);
|
|
||||||
hMemoryMapping = 0;
|
|
||||||
#else
|
|
||||||
close(fd);
|
close(fd);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
return MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
|
|
||||||
#else
|
|
||||||
void* retval = mmap(base, size, PROT_READ | PROT_WRITE,
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE,
|
||||||
MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
|
MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
|
||||||
|
|
||||||
|
@ -121,16 +85,11 @@ void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
||||||
{
|
{
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MemArena::ReleaseView(void* view, size_t size)
|
void MemArena::ReleaseView(void* view, size_t size)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
UnmapViewOfFile(view);
|
|
||||||
#else
|
|
||||||
munmap(view, size);
|
munmap(view, size);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
u8* MemArena::FindMemoryBase()
|
u8* MemArena::FindMemoryBase()
|
||||||
|
@ -141,24 +100,10 @@ u8* MemArena::FindMemoryBase()
|
||||||
const size_t memory_size = 0x400000000;
|
const size_t memory_size = 0x400000000;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
u8* base = static_cast<u8*>(VirtualAlloc(nullptr, memory_size, MEM_RESERVE, PAGE_READWRITE));
|
|
||||||
if (!base)
|
|
||||||
{
|
|
||||||
PanicAlertFmt("Failed to map enough memory space: {}", GetLastErrorString());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
VirtualFree(base, 0, MEM_RELEASE);
|
|
||||||
return base;
|
|
||||||
#else
|
|
||||||
#ifdef ANDROID
|
|
||||||
// Android 4.3 changed how mmap works.
|
// Android 4.3 changed how mmap works.
|
||||||
// if we map it private and then munmap it, we can't use the base returned.
|
// if we map it private and then munmap it, we can't use the base returned.
|
||||||
// This may be due to changes in them support a full SELinux implementation.
|
// This may be due to changes in them to support a full SELinux implementation.
|
||||||
const int flags = MAP_ANON | MAP_SHARED;
|
const int flags = MAP_ANON | MAP_SHARED;
|
||||||
#else
|
|
||||||
const int flags = MAP_ANON | MAP_PRIVATE;
|
|
||||||
#endif
|
|
||||||
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
||||||
if (base == MAP_FAILED)
|
if (base == MAP_FAILED)
|
||||||
{
|
{
|
||||||
|
@ -167,7 +112,5 @@ u8* MemArena::FindMemoryBase()
|
||||||
}
|
}
|
||||||
munmap(base, memory_size);
|
munmap(base, memory_size);
|
||||||
return static_cast<u8*>(base);
|
return static_cast<u8*>(base);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
|
@ -0,0 +1,83 @@
|
||||||
|
// Copyright 2008 Dolphin Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#include "Common/MemArena.h"
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "Common/CommonFuncs.h"
|
||||||
|
#include "Common/CommonTypes.h"
|
||||||
|
#include "Common/Logging/Log.h"
|
||||||
|
#include "Common/MsgHandler.h"
|
||||||
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
|
namespace Common
|
||||||
|
{
|
||||||
|
void MemArena::GrabSHMSegment(size_t size)
|
||||||
|
{
|
||||||
|
const std::string file_name = "/dolphin-emu." + std::to_string(getpid());
|
||||||
|
fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
ERROR_LOG_FMT(MEMMAP, "shm_open failed: {}", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
shm_unlink(file_name.c_str());
|
||||||
|
if (ftruncate(fd, size) < 0)
|
||||||
|
ERROR_LOG_FMT(MEMMAP, "Failed to allocate low memory space");
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemArena::ReleaseSHMSegment()
|
||||||
|
{
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
||||||
|
{
|
||||||
|
void* retval = mmap(base, size, PROT_READ | PROT_WRITE,
|
||||||
|
MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
|
||||||
|
|
||||||
|
if (retval == MAP_FAILED)
|
||||||
|
{
|
||||||
|
NOTICE_LOG_FMT(MEMMAP, "mmap failed");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemArena::ReleaseView(void* view, size_t size)
|
||||||
|
{
|
||||||
|
munmap(view, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8* MemArena::FindMemoryBase()
|
||||||
|
{
|
||||||
|
#if _ARCH_32
|
||||||
|
const size_t memory_size = 0x31000000;
|
||||||
|
#else
|
||||||
|
const size_t memory_size = 0x400000000;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const int flags = MAP_ANON | MAP_PRIVATE;
|
||||||
|
void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0);
|
||||||
|
if (base == MAP_FAILED)
|
||||||
|
{
|
||||||
|
PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
munmap(base, memory_size);
|
||||||
|
return static_cast<u8*>(base);
|
||||||
|
}
|
||||||
|
} // namespace Common
|
|
@ -0,0 +1,61 @@
|
||||||
|
// Copyright 2008 Dolphin Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#include "Common/MemArena.h"
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
#include "Common/CommonFuncs.h"
|
||||||
|
#include "Common/CommonTypes.h"
|
||||||
|
#include "Common/Logging/Log.h"
|
||||||
|
#include "Common/MsgHandler.h"
|
||||||
|
#include "Common/StringUtil.h"
|
||||||
|
|
||||||
|
namespace Common
|
||||||
|
{
|
||||||
|
void MemArena::GrabSHMSegment(size_t size)
|
||||||
|
{
|
||||||
|
const std::string name = "dolphin-emu." + std::to_string(GetCurrentProcessId());
|
||||||
|
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
|
||||||
|
static_cast<DWORD>(size), UTF8ToTStr(name).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemArena::ReleaseSHMSegment()
|
||||||
|
{
|
||||||
|
CloseHandle(hMemoryMapping);
|
||||||
|
hMemoryMapping = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* MemArena::CreateView(s64 offset, size_t size, void* base)
|
||||||
|
{
|
||||||
|
return MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemArena::ReleaseView(void* view, size_t size)
|
||||||
|
{
|
||||||
|
UnmapViewOfFile(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8* MemArena::FindMemoryBase()
|
||||||
|
{
|
||||||
|
#if _ARCH_32
|
||||||
|
const size_t memory_size = 0x31000000;
|
||||||
|
#else
|
||||||
|
const size_t memory_size = 0x400000000;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
u8* base = static_cast<u8*>(VirtualAlloc(nullptr, memory_size, MEM_RESERVE, PAGE_READWRITE));
|
||||||
|
if (!base)
|
||||||
|
{
|
||||||
|
PanicAlertFmt("Failed to map enough memory space: {}", GetLastErrorString());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
VirtualFree(base, 0, MEM_RELEASE);
|
||||||
|
return base;
|
||||||
|
}
|
||||||
|
} // namespace Common
|
|
@ -726,7 +726,7 @@
|
||||||
<ClCompile Include="Common\MathUtil.cpp" />
|
<ClCompile Include="Common\MathUtil.cpp" />
|
||||||
<ClCompile Include="Common\Matrix.cpp" />
|
<ClCompile Include="Common\Matrix.cpp" />
|
||||||
<ClCompile Include="Common\MD5.cpp" />
|
<ClCompile Include="Common\MD5.cpp" />
|
||||||
<ClCompile Include="Common\MemArena.cpp" />
|
<ClCompile Include="Common\MemArenaWin.cpp" />
|
||||||
<ClCompile Include="Common\MemoryUtil.cpp" />
|
<ClCompile Include="Common\MemoryUtil.cpp" />
|
||||||
<ClCompile Include="Common\MsgHandler.cpp" />
|
<ClCompile Include="Common\MsgHandler.cpp" />
|
||||||
<ClCompile Include="Common\NandPaths.cpp" />
|
<ClCompile Include="Common\NandPaths.cpp" />
|
||||||
|
|
Loading…
Reference in New Issue