mirror of
https://github.com/libretro/ppsspp.git
synced 2024-11-23 16:19:44 +00:00
The MemArena code has way too many ifdefs. Split it up by platform.
This commit is contained in:
parent
19e309c91e
commit
73a2a1bc0f
@ -337,7 +337,9 @@ add_library(Common STATIC
|
||||
Common/KeyMap.h
|
||||
Common/LogManager.cpp
|
||||
Common/LogManager.h
|
||||
Common/MemArena.cpp
|
||||
Common/MemArenaAndroid.cpp
|
||||
Common/MemArenaPosix.cpp
|
||||
Common/MemArenaWin32.cpp
|
||||
Common/MemArena.h
|
||||
Common/MemoryUtil.cpp
|
||||
Common/MemoryUtil.h
|
||||
|
@ -292,7 +292,9 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="KeyMap.cpp" />
|
||||
<ClCompile Include="LogManager.cpp" />
|
||||
<ClCompile Include="MemArena.cpp" />
|
||||
<ClCompile Include="MemArenaAndroid.cpp" />
|
||||
<ClCompile Include="MemArenaPosix.cpp" />
|
||||
<ClCompile Include="MemArenaWin32.cpp" />
|
||||
<ClCompile Include="MemoryUtil.cpp" />
|
||||
<ClCompile Include="MipsEmitter.cpp" />
|
||||
<ClCompile Include="Misc.cpp" />
|
||||
@ -315,9 +317,6 @@
|
||||
<ClCompile Include="x64Analyzer.cpp" />
|
||||
<ClCompile Include="x64Emitter.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="CMakeLists.txt" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ext\native\native.vcxproj">
|
||||
<Project>{c4df647e-80ea-4111-a0a8-218b1b711e18}</Project>
|
||||
@ -326,4 +325,4 @@
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
</Project>
|
@ -81,7 +81,6 @@
|
||||
<ClCompile Include="CPUDetect.cpp" />
|
||||
<ClCompile Include="FileUtil.cpp" />
|
||||
<ClCompile Include="LogManager.cpp" />
|
||||
<ClCompile Include="MemArena.cpp" />
|
||||
<ClCompile Include="MemoryUtil.cpp" />
|
||||
<ClCompile Include="Misc.cpp" />
|
||||
<ClCompile Include="MsgHandler.cpp" />
|
||||
@ -132,9 +131,9 @@
|
||||
<ClCompile Include="Vulkan\VulkanMemory.cpp">
|
||||
<Filter>Vulkan</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="CMakeLists.txt" />
|
||||
<ClCompile Include="MemArenaPosix.cpp" />
|
||||
<ClCompile Include="MemArenaWin32.cpp" />
|
||||
<ClCompile Include="MemArenaAndroid.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Filter Include="Crypto">
|
||||
@ -150,4 +149,4 @@
|
||||
<UniqueIdentifier>{c14d66ef-5f7c-4565-975a-72774e7ccfb9}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
@ -1,246 +0,0 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0 or later versions.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "FileUtil.h"
|
||||
#include "MemoryUtil.h"
|
||||
#include "MemArena.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "CommonWindows.h"
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#ifdef __ANDROID__
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/ashmem.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
// Hopefully this ABI will never change...
|
||||
|
||||
#define ASHMEM_DEVICE "/dev/ashmem"
|
||||
|
||||
/*
|
||||
* ashmem_create_region - creates a new ashmem region and returns the file
|
||||
* descriptor, or <0 on error
|
||||
*
|
||||
* `name' is an optional label to give the region (visible in /proc/pid/maps)
|
||||
* `size' is the size of the region, in page-aligned bytes
|
||||
*/
|
||||
int ashmem_create_region(const char *name, size_t size) {
|
||||
int fd, ret;
|
||||
|
||||
fd = open(ASHMEM_DEVICE, O_RDWR);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
if (name) {
|
||||
char buf[ASHMEM_NAME_LEN];
|
||||
strncpy(buf, name, sizeof(buf));
|
||||
ret = ioctl(fd, ASHMEM_SET_NAME, buf);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
||||
return fd;
|
||||
|
||||
error:
|
||||
ERROR_LOG(MEMMAP, "NASTY ASHMEM ERROR: ret = %08x", ret);
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ashmem_set_prot_region(int fd, int prot) {
|
||||
return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
|
||||
}
|
||||
|
||||
int ashmem_pin_region(int fd, size_t offset, size_t len) {
|
||||
// Even on 64-bit, it seems these arguments are 32-bit and thus need a cast to avoid warnings.
|
||||
struct ashmem_pin pin = { (uint32_t)offset, (uint32_t)len };
|
||||
return ioctl(fd, ASHMEM_PIN, &pin);
|
||||
}
|
||||
|
||||
int ashmem_unpin_region(int fd, size_t offset, size_t len) {
|
||||
struct ashmem_pin pin = { (uint32_t)offset, (uint32_t)len };
|
||||
return ioctl(fd, ASHMEM_UNPIN, &pin);
|
||||
}
|
||||
#endif // Android
|
||||
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO sysInfo;
|
||||
#else
|
||||
static const std::string tmpfs_location = "/dev/shm";
|
||||
static const std::string tmpfs_ram_temp_file = "/dev/shm/gc_mem.tmp";
|
||||
|
||||
// do not make this "static"
|
||||
#if defined(BB)
|
||||
std::string ram_temp_file = "/home/root/gc_mem.tmp";
|
||||
#else
|
||||
std::string ram_temp_file = "/tmp/gc_mem.tmp";
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
|
||||
#ifdef _WIN32
|
||||
size_t MemArena::roundup(size_t x) {
|
||||
int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;
|
||||
return (x + gran - 1) & ~(gran - 1);
|
||||
}
|
||||
#else
|
||||
size_t MemArena::roundup(size_t x) {
|
||||
return x;
|
||||
}
|
||||
#endif
|
||||
|
||||
void MemArena::GrabLowMemSpace(size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL);
|
||||
GetSystemInfo(&sysInfo);
|
||||
#elif defined(__ANDROID__)
|
||||
// Use ashmem so we don't have to allocate a file on disk!
|
||||
fd = ashmem_create_region("PPSSPP_RAM", size);
|
||||
// Note that it appears that ashmem is pinned by default, so no need to pin.
|
||||
if (fd < 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));
|
||||
return;
|
||||
}
|
||||
#else
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
|
||||
// Some platforms (like Raspberry Pi) end up flushing to disk.
|
||||
// To avoid this, we try to use /dev/shm (tmpfs) if it exists.
|
||||
fd = -1;
|
||||
if (File::Exists(tmpfs_location)) {
|
||||
fd = open(tmpfs_ram_temp_file.c_str(), O_RDWR | O_CREAT, mode);
|
||||
if (fd >= 0) {
|
||||
// Great, this definitely shouldn't flush to disk.
|
||||
ram_temp_file = tmpfs_ram_temp_file;
|
||||
}
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
fd = open(ram_temp_file.c_str(), O_RDWR | O_CREAT, mode);
|
||||
}
|
||||
if (fd < 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to grab memory space as a file: %s of size: %08x errno: %d", ram_temp_file.c_str(), (int)size, (int)(errno));
|
||||
return;
|
||||
}
|
||||
// delete immediately, we keep the fd so it still lives
|
||||
if (unlink(ram_temp_file.c_str()) != 0) {
|
||||
WARN_LOG(MEMMAP, "Failed to unlink %s", ram_temp_file.c_str());
|
||||
}
|
||||
if (ftruncate(fd, size) != 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to ftruncate %d (%s) to size %08x", (int)fd, ram_temp_file.c_str(), (int)size);
|
||||
}
|
||||
return;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MemArena::ReleaseSpace() {
|
||||
#ifdef _WIN32
|
||||
CloseHandle(hMemoryMapping);
|
||||
hMemoryMapping = 0;
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void *MemArena::CreateView(s64 offset, size_t size, void *base)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
size = roundup(size);
|
||||
void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
|
||||
return ptr;
|
||||
#else
|
||||
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED |
|
||||
// Do not sync memory to underlying file. Linux has this by default.
|
||||
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
||||
MAP_NOSYNC |
|
||||
#endif
|
||||
((base == 0) ? 0 : MAP_FIXED), fd, offset);
|
||||
|
||||
if (retval == MAP_FAILED)
|
||||
{
|
||||
NOTICE_LOG(MEMMAP, "mmap on %s (fd: %d) failed", ram_temp_file.c_str(), (int)fd);
|
||||
return 0;
|
||||
}
|
||||
return retval;
|
||||
#endif
|
||||
}
|
||||
|
||||
void MemArena::ReleaseView(void* view, size_t size) {
|
||||
#ifdef _WIN32
|
||||
UnmapViewOfFile(view);
|
||||
#else
|
||||
munmap(view, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
u8* MemArena::Find4GBBase() {
|
||||
#ifdef _M_X64
|
||||
#ifdef _WIN32
|
||||
// 64 bit
|
||||
u8 *base = (u8*)VirtualAlloc(0, 0xE1000000, MEM_RESERVE, PAGE_READWRITE);
|
||||
if (base) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
}
|
||||
return base;
|
||||
#else
|
||||
// Very precarious - mmap cannot return an error when trying to map already used pages.
|
||||
// This makes the Windows approach above unusable on Linux, so we will simply pray...
|
||||
return reinterpret_cast<u8*>(0x2300000000ULL);
|
||||
#endif
|
||||
|
||||
#elif defined(ARM64)
|
||||
|
||||
// Very precarious - mmap cannot return an error when trying to map already used pages.
|
||||
// This makes the Windows approach above unusable on Linux, so we will simply pray...
|
||||
return reinterpret_cast<u8*>(0x2300000000ULL);
|
||||
|
||||
#else
|
||||
|
||||
#ifdef _WIN32
|
||||
u8* base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE, PAGE_READWRITE);
|
||||
if (base) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
}
|
||||
return base;
|
||||
#else
|
||||
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
|
||||
MAP_ANON | MAP_SHARED, -1, 0);
|
||||
if (base == MAP_FAILED) {
|
||||
PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
munmap(base, 0x10000000);
|
||||
return static_cast<u8*>(base);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
@ -27,8 +27,9 @@
|
||||
// Multiple views can mirror the same section of the block, which makes it very convient for emulating
|
||||
// memory mirrors.
|
||||
|
||||
class MemArena
|
||||
{
|
||||
struct MemArenaData;
|
||||
|
||||
class MemArena {
|
||||
public:
|
||||
size_t roundup(size_t x);
|
||||
void GrabLowMemSpace(size_t size);
|
||||
@ -36,11 +37,12 @@ public:
|
||||
void *CreateView(s64 offset, size_t size, void *base = 0);
|
||||
void ReleaseView(void *view, size_t size);
|
||||
// This only finds 1 GB in 32-bit
|
||||
static u8 *Find4GBBase();
|
||||
private:
|
||||
u8 *Find4GBBase();
|
||||
|
||||
private:
|
||||
#ifdef _WIN32
|
||||
HANDLE hMemoryMapping;
|
||||
SYSTEM_INFO sysInfo;
|
||||
#else
|
||||
int fd;
|
||||
#endif
|
||||
|
130
Common/MemArenaAndroid.cpp
Normal file
130
Common/MemArenaAndroid.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0 or later versions.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
#include "ppsspp_config.h"
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
#include "MemoryUtil.h"
|
||||
#include "MemArena.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#include <sys/ioctl.h>
|
||||
#include <linux/ashmem.h>
|
||||
|
||||
// Hopefully this ABI will never change...
|
||||
|
||||
#define ASHMEM_DEVICE "/dev/ashmem"
|
||||
|
||||
// ashmem_create_region - creates a new ashmem region and returns the file
|
||||
// descriptor, or <0 on error
|
||||
// `name' is an optional label to give the region (visible in /proc/pid/maps)
|
||||
// `size' is the size of the region, in page-aligned bytes
|
||||
static int ashmem_create_region(const char *name, size_t size) {
|
||||
int fd = open(ASHMEM_DEVICE, O_RDWR);
|
||||
if (fd < 0)
|
||||
return fd;
|
||||
|
||||
int ret;
|
||||
if (name) {
|
||||
char buf[ASHMEM_NAME_LEN];
|
||||
strncpy(buf, name, sizeof(buf));
|
||||
ret = ioctl(fd, ASHMEM_SET_NAME, buf);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
}
|
||||
|
||||
ret = ioctl(fd, ASHMEM_SET_SIZE, size);
|
||||
if (ret < 0)
|
||||
goto error;
|
||||
|
||||
return fd;
|
||||
|
||||
error:
|
||||
ERROR_LOG(MEMMAP, "NASTY ASHMEM ERROR: ret = %08x", ret);
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ashmem_set_prot_region(int fd, int prot) {
|
||||
return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);
|
||||
}
|
||||
|
||||
static int ashmem_pin_region(int fd, size_t offset, size_t len) {
|
||||
// Even on 64-bit, it seems these arguments are 32-bit and thus need a cast to avoid warnings.
|
||||
struct ashmem_pin pin = { (uint32_t)offset, (uint32_t)len };
|
||||
return ioctl(fd, ASHMEM_PIN, &pin);
|
||||
}
|
||||
|
||||
static int ashmem_unpin_region(int fd, size_t offset, size_t len) {
|
||||
struct ashmem_pin pin = { (uint32_t)offset, (uint32_t)len };
|
||||
return ioctl(fd, ASHMEM_UNPIN, &pin);
|
||||
}
|
||||
|
||||
// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
|
||||
size_t MemArena::roundup(size_t x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
void MemArena::GrabLowMemSpace(size_t size) {
|
||||
// Use ashmem so we don't have to allocate a file on disk!
|
||||
fd = ashmem_create_region("PPSSPP_RAM", size);
|
||||
// Note that it appears that ashmem is pinned by default, so no need to pin.
|
||||
if (fd < 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to grab ashmem space of size: %08x errno: %d", (int)size, (int)(errno));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void MemArena::ReleaseSpace() {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void *MemArena::CreateView(s64 offset, size_t size, void *base) {
|
||||
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | ((base == 0) ? 0 : MAP_FIXED), fd, offset);
|
||||
if (retval == MAP_FAILED) {
|
||||
NOTICE_LOG(MEMMAP, "mmap on ashmem (fd: %d) failed", (int)fd);
|
||||
return 0;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void MemArena::ReleaseView(void* view, size_t size) {
|
||||
munmap(view, size);
|
||||
}
|
||||
|
||||
u8* MemArena::Find4GBBase() {
|
||||
#if PPSSPP_ARCH(64BIT)
|
||||
// Just grab some random 4GB...
|
||||
return reinterpret_cast<u8*>(0x2300000000ULL);
|
||||
#else
|
||||
// Address masking is used in 32-bit mode, so we can get away with less memory.
|
||||
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
|
||||
MAP_ANON | MAP_SHARED, -1, 0);
|
||||
if (base == MAP_FAILED) {
|
||||
PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
munmap(base, 0x10000000);
|
||||
return static_cast<u8*>(base);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // __ANDROID__
|
117
Common/MemArenaPosix.cpp
Normal file
117
Common/MemArenaPosix.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0 or later versions.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
#include "ppsspp_config.h"
|
||||
|
||||
#if !defined(_WIN32) && !defined(ANDROID)
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "FileUtil.h"
|
||||
#include "MemoryUtil.h"
|
||||
#include "MemArena.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
|
||||
static const std::string tmpfs_location = "/dev/shm";
|
||||
static const std::string tmpfs_ram_temp_file = "/dev/shm/gc_mem.tmp";
|
||||
|
||||
// do not make this "static"
|
||||
std::string ram_temp_file = "/tmp/gc_mem.tmp";
|
||||
|
||||
size_t MemArena::roundup(size_t x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
void MemArena::GrabLowMemSpace(size_t size) {
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
|
||||
// Some platforms (like Raspberry Pi) end up flushing to disk.
|
||||
// To avoid this, we try to use /dev/shm (tmpfs) if it exists.
|
||||
fd = -1;
|
||||
if (File::Exists(tmpfs_location)) {
|
||||
fd = open(tmpfs_ram_temp_file.c_str(), O_RDWR | O_CREAT, mode);
|
||||
if (fd >= 0) {
|
||||
// Great, this definitely shouldn't flush to disk.
|
||||
ram_temp_file = tmpfs_ram_temp_file;
|
||||
}
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
fd = open(ram_temp_file.c_str(), O_RDWR | O_CREAT, mode);
|
||||
}
|
||||
if (fd < 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to grab memory space as a file: %s of size: %08x errno: %d", ram_temp_file.c_str(), (int)size, (int)(errno));
|
||||
return;
|
||||
}
|
||||
// delete immediately, we keep the fd so it still lives
|
||||
if (unlink(ram_temp_file.c_str()) != 0) {
|
||||
WARN_LOG(MEMMAP, "Failed to unlink %s", ram_temp_file.c_str());
|
||||
}
|
||||
if (ftruncate(fd, size) != 0) {
|
||||
ERROR_LOG(MEMMAP, "Failed to ftruncate %d (%s) to size %08x", (int)fd, ram_temp_file.c_str(), (int)size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void MemArena::ReleaseSpace() {
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void *MemArena::CreateView(s64 offset, size_t size, void *base)
|
||||
{
|
||||
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED |
|
||||
// Do not sync memory to underlying file. Linux has this by default.
|
||||
#if defined(__DragonFly__) || defined(__FreeBSD__)
|
||||
MAP_NOSYNC |
|
||||
#endif
|
||||
((base == 0) ? 0 : MAP_FIXED), fd, offset);
|
||||
|
||||
if (retval == MAP_FAILED) {
|
||||
NOTICE_LOG(MEMMAP, "mmap on %s (fd: %d) failed", ram_temp_file.c_str(), (int)fd);
|
||||
return 0;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
void MemArena::ReleaseView(void* view, size_t size) {
|
||||
munmap(view, size);
|
||||
}
|
||||
|
||||
u8* MemArena::Find4GBBase() {
|
||||
// Now, create views in high memory where there's plenty of space.
|
||||
#if PPSSPP_ARCH(64BIT)
|
||||
// Very precarious - mmap cannot return an error when trying to map already used pages.
|
||||
// This makes the Windows approach above unusable on Linux, so we will simply pray...
|
||||
return reinterpret_cast<u8*>(0x2300000000ULL);
|
||||
#else
|
||||
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
|
||||
MAP_ANON | MAP_SHARED, -1, 0);
|
||||
if (base == MAP_FAILED) {
|
||||
PanicAlert("Failed to map 256 MB of memory space: %s", strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
munmap(base, 0x10000000);
|
||||
return static_cast<u8*>(base);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
68
Common/MemArenaWin32.cpp
Normal file
68
Common/MemArenaWin32.cpp
Normal file
@ -0,0 +1,68 @@
|
||||
// Copyright (C) 2003 Dolphin Project.
|
||||
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, version 2.0 or later versions.
|
||||
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License 2.0 for more details.
|
||||
|
||||
// A copy of the GPL 2.0 should have been included with the program.
|
||||
// If not, see http://www.gnu.org/licenses/
|
||||
|
||||
// Official SVN repository and contact information can be found at
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
|
||||
#include "ppsspp_config.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include "MemArena.h"
|
||||
#include "CommonWindows.h"
|
||||
|
||||
// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
|
||||
size_t MemArena::roundup(size_t x) {
|
||||
int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;
|
||||
return (x + gran - 1) & ~(gran - 1);
|
||||
}
|
||||
|
||||
void MemArena::GrabLowMemSpace(size_t size) {
|
||||
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)(size), NULL);
|
||||
GetSystemInfo(&sysInfo);
|
||||
}
|
||||
|
||||
void MemArena::ReleaseSpace() {
|
||||
CloseHandle(hMemoryMapping);
|
||||
hMemoryMapping = 0;
|
||||
}
|
||||
|
||||
void *MemArena::CreateView(s64 offset, size_t size, void *base) {
|
||||
size = roundup(size);
|
||||
void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void MemArena::ReleaseView(void* view, size_t size) {
|
||||
UnmapViewOfFile(view);
|
||||
}
|
||||
|
||||
u8* MemArena::Find4GBBase() {
|
||||
// Now, create views in high memory where there's plenty of space.
|
||||
#if PPSSPP_ARCH(32BIT)
|
||||
// Caller will need to find one in a different way.
|
||||
return nullptr;
|
||||
|
||||
#elif PPSSPP_ARCH(64BIT)
|
||||
u8 *base = (u8*)VirtualAlloc(0, 0xE1000000, MEM_RESERVE, PAGE_READWRITE);
|
||||
if (base) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
}
|
||||
return base;
|
||||
#else
|
||||
#error Arch not supported
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
@ -140,8 +140,7 @@ static bool Memory_TryBase(u32 flags) {
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < num_views; i++)
|
||||
{
|
||||
for (i = 0; i < num_views; i++) {
|
||||
const MemoryView &view = views[i];
|
||||
if (view.size == 0)
|
||||
continue;
|
||||
@ -153,9 +152,11 @@ static bool Memory_TryBase(u32 flags) {
|
||||
#if PPSSPP_ARCH(64BIT)
|
||||
*view.out_ptr = (u8*)g_arena.CreateView(
|
||||
position, view.size, base + view.virtual_address);
|
||||
if (!*view.out_ptr)
|
||||
goto bail;
|
||||
#else
|
||||
if (CanIgnoreView(view)) {
|
||||
// No need to create multiple identical views.
|
||||
// This is handled by address masking in 32-bit, no view needs to be created.
|
||||
*view.out_ptr = *views[i - 1].out_ptr;
|
||||
} else {
|
||||
*view.out_ptr = (u8*)g_arena.CreateView(
|
||||
@ -172,13 +173,11 @@ static bool Memory_TryBase(u32 flags) {
|
||||
|
||||
bail:
|
||||
// Argh! ERROR! Free what we grabbed so far so we can try again.
|
||||
for (int j = 0; j <= i; j++)
|
||||
{
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
if (*views[j].out_ptr)
|
||||
{
|
||||
if (*views[j].out_ptr) {
|
||||
if (!CanIgnoreView(views[j])) {
|
||||
g_arena.ReleaseView(*views[j].out_ptr, views[j].size);
|
||||
}
|
||||
@ -188,63 +187,49 @@ bail:
|
||||
return false;
|
||||
}
|
||||
|
||||
void MemoryMap_Setup(u32 flags)
|
||||
{
|
||||
// Find a base to reserve 256MB
|
||||
bool MemoryMap_Setup(u32 flags) {
|
||||
// Figure out how much memory we need to allocate in total.
|
||||
size_t total_mem = 0;
|
||||
|
||||
for (int i = 0; i < num_views; i++)
|
||||
{
|
||||
for (int i = 0; i < num_views; i++) {
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
if (!CanIgnoreView(views[i]))
|
||||
total_mem += g_arena.roundup(views[i].size);
|
||||
}
|
||||
|
||||
// Grab some pagefile backed memory out of the void ...
|
||||
g_arena.GrabLowMemSpace(total_mem);
|
||||
// 32-bit Windows retrieves base a different way
|
||||
#if defined(_M_X64) || !defined(_WIN32)
|
||||
// This really shouldn't fail - in 64-bit, there will always be enough address space.
|
||||
// Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors.
|
||||
base = MemArena::Find4GBBase();
|
||||
#endif
|
||||
|
||||
// Now, create views in high memory where there's plenty of space.
|
||||
#if defined(_WIN32) && !defined(_M_X64)
|
||||
// Try a whole range of possible bases. Return once we got a valid one.
|
||||
int base_attempts = 0;
|
||||
u32 max_base_addr = 0x7FFF0000 - 0x10000000;
|
||||
base = g_arena.Find4GBBase();
|
||||
|
||||
for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000)
|
||||
{
|
||||
base_attempts++;
|
||||
base = (u8 *)base_addr;
|
||||
if (Memory_TryBase(flags))
|
||||
{
|
||||
INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
|
||||
base_attempts = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (base_attempts)
|
||||
PanicAlert("No possible memory base pointer found!");
|
||||
#else
|
||||
// Try base we retrieved earlier
|
||||
if (!Memory_TryBase(flags))
|
||||
{
|
||||
if (!base) {
|
||||
#if defined(_WIN32) && PPSSPP_ARCH(32BIT)
|
||||
// Try a whole range of possible bases. Return once we got a valid one.
|
||||
int base_attempts = 0;
|
||||
u32 max_base_addr = 0x7FFF0000 - 0x10000000;
|
||||
for (u32 base_addr = 0x01000000; base_addr < max_base_addr; base_addr += 0x400000) {
|
||||
base_attempts++;
|
||||
base = (u8 *)base_addr;
|
||||
if (Memory_TryBase(flags)) {
|
||||
INFO_LOG(MEMMAP, "Found valid memory base at %p after %i tries.", base, base_attempts);
|
||||
base_attempts = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
ERROR_LOG(MEMMAP, "MemoryMap_Setup: Failed finding a memory base.");
|
||||
PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
|
||||
// Should return true...
|
||||
return Memory_TryBase(flags);
|
||||
}
|
||||
|
||||
void MemoryMap_Shutdown(u32 flags)
|
||||
{
|
||||
for (int i = 0; i < num_views; i++)
|
||||
{
|
||||
void MemoryMap_Shutdown(u32 flags) {
|
||||
for (int i = 0; i < num_views; i++) {
|
||||
if (views[i].size == 0)
|
||||
continue;
|
||||
SKIP(flags, views[i].flags);
|
||||
@ -255,9 +240,7 @@ void MemoryMap_Shutdown(u32 flags)
|
||||
g_arena.ReleaseSpace();
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
|
||||
void Init() {
|
||||
// On some 32 bit platforms, you can only map < 32 megs at a time.
|
||||
const static int MAX_MMAP_SIZE = 31 * 1024 * 1024;
|
||||
_dbg_assert_msg_(MEMMAP, g_MemorySize < MAX_MMAP_SIZE * 3, "ACK - too much memory for three mmap views.");
|
||||
@ -276,8 +259,7 @@ void Init()
|
||||
base, m_pPhysicalRAM, m_pUncachedRAM);
|
||||
}
|
||||
|
||||
void DoState(PointerWrap &p)
|
||||
{
|
||||
void DoState(PointerWrap &p) {
|
||||
auto s = p.Section("Memory", 1, 3);
|
||||
if (!s)
|
||||
return;
|
||||
|
@ -116,7 +116,7 @@ struct MemoryView
|
||||
};
|
||||
|
||||
// Uses a memory arena to set up an emulator-friendly memory map
|
||||
void MemoryMap_Setup(u32 flags);
|
||||
bool MemoryMap_Setup(u32 flags);
|
||||
void MemoryMap_Shutdown(u32 flags);
|
||||
|
||||
// Init and Shutdown
|
||||
|
Loading…
Reference in New Issue
Block a user