From cd2e07f680d06425b73d8ecb1476adba97ceadaf Mon Sep 17 00:00:00 2001 From: DrChat Date: Tue, 19 Dec 2017 19:25:58 -0600 Subject: [PATCH] [Base] First-pass memory file mapping support --- src/xenia/base/memory_posix.cc | 36 ++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/xenia/base/memory_posix.cc b/src/xenia/base/memory_posix.cc index ea97ff900..085d5b2ff 100644 --- a/src/xenia/base/memory_posix.cc +++ b/src/xenia/base/memory_posix.cc @@ -8,7 +8,9 @@ */ #include "xenia/base/memory.h" +#include "xenia/base/string.h" +#include #include #include @@ -61,19 +63,45 @@ bool QueryProtect(void* base_address, size_t& length, PageAccess& access_out) { FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length, PageAccess access, bool commit) { - return nullptr; + int oflag; + switch (access) { + case PageAccess::kNoAccess: + oflag = 0; + break; + case PageAccess::kReadOnly: + oflag = O_RDONLY; + break; + case PageAccess::kReadWrite: + oflag = O_RDWR; + break; + default: + assert_always(); + return nullptr; + } + + oflag |= O_CREAT; + int ret = shm_open(xe::to_string(path).c_str(), oflag, 0777); + if (ret > 0) { + ftruncate64(ret, length); + } + + return ret <= 0 ? nullptr : reinterpret_cast(ret); } -void CloseFileMappingHandle(FileMappingHandle handle) {} +void CloseFileMappingHandle(FileMappingHandle handle) { + close((intptr_t)handle); +} void* MapFileView(FileMappingHandle handle, void* base_address, size_t length, PageAccess access, size_t file_offset) { - return nullptr; + uint32_t prot = ToPosixProtectFlags(access); + return mmap64(base_address, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, + reinterpret_cast(handle), file_offset); } bool UnmapFileView(FileMappingHandle handle, void* base_address, size_t length) { - return false; + return munmap(base_address, length) == 0; } } // namespace memory