From 49e194009bbaaeb6cda5cc7db7fd415fbfead275 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Thu, 11 Jul 2019 09:44:42 -0400 Subject: [PATCH] [memory linux] Properly unlink shared memory shm_unlink(name) is the proper way to close a shared memory in linux. Prior to this, xenia was creating and not cleaning up shared memory handle which would accumulate in /dev/shm. shm_unlink is the proper way of doing this. Add filename to CloseFileMappingHandle signature. Add simple test to open and close. --- src/xenia/base/memory.h | 3 ++- src/xenia/base/memory_posix.cc | 6 ++++-- src/xenia/base/memory_win.cc | 5 ++++- src/xenia/base/testing/memory_test.cc | 6 +++--- src/xenia/cpu/backend/x64/x64_code_cache.cc | 2 +- src/xenia/memory.cc | 2 +- 6 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/xenia/base/memory.h b/src/xenia/base/memory.h index e1125a5e3..c0dfba3c6 100644 --- a/src/xenia/base/memory.h +++ b/src/xenia/base/memory.h @@ -101,7 +101,8 @@ typedef void* FileMappingHandle; FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, size_t length, PageAccess access, bool commit); -void CloseFileMappingHandle(FileMappingHandle handle); +void CloseFileMappingHandle(FileMappingHandle handle, + const std::filesystem::path& path); void* MapFileView(FileMappingHandle handle, void* base_address, size_t length, PageAccess access, size_t file_offset); bool UnmapFileView(FileMappingHandle handle, void* base_address, size_t length); diff --git a/src/xenia/base/memory_posix.cc b/src/xenia/base/memory_posix.cc index 81020d094..975f1eed4 100644 --- a/src/xenia/base/memory_posix.cc +++ b/src/xenia/base/memory_posix.cc @@ -97,8 +97,10 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, return ret <= 0 ? nullptr : reinterpret_cast(ret); } -void CloseFileMappingHandle(FileMappingHandle handle) { - close(static_cast(reinterpret_cast(handle))); +void CloseFileMappingHandle(FileMappingHandle handle, + const std::filesystem::path& path) { + auto full_path = "/" / path; + shm_unlink(full_path.c_str()); } void* MapFileView(FileMappingHandle handle, void* base_address, size_t length, diff --git a/src/xenia/base/memory_win.cc b/src/xenia/base/memory_win.cc index ed39e1028..343285d94 100644 --- a/src/xenia/base/memory_win.cc +++ b/src/xenia/base/memory_win.cc @@ -153,7 +153,10 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, static_cast(length), full_path.c_str()); } -void CloseFileMappingHandle(FileMappingHandle handle) { CloseHandle(handle); } +void CloseFileMappingHandle(FileMappingHandle handle, + const std::filesystem::path& path) { + CloseHandle(handle); +} void* MapFileView(FileMappingHandle handle, void* base_address, size_t length, PageAccess access, size_t file_offset) { diff --git a/src/xenia/base/testing/memory_test.cc b/src/xenia/base/testing/memory_test.cc index 1fe2184fa..b267a4f74 100644 --- a/src/xenia/base/testing/memory_test.cc +++ b/src/xenia/base/testing/memory_test.cc @@ -422,7 +422,7 @@ TEST_CASE("create_and_close_file_mapping", "Virtual Memory Mapping") { auto memory = xe::memory::CreateFileMappingHandle( path, 0x100, xe::memory::PageAccess::kReadWrite, true); REQUIRE(memory); - xe::memory::CloseFileMappingHandle(memory); + xe::memory::CloseFileMappingHandle(memory, path); } TEST_CASE("map_view", "Virtual Memory Mapping") { @@ -439,7 +439,7 @@ TEST_CASE("map_view", "Virtual Memory Mapping") { REQUIRE(reinterpret_cast(view) == address); xe::memory::UnmapFileView(memory, reinterpret_cast(address), length); - xe::memory::CloseFileMappingHandle(memory); + xe::memory::CloseFileMappingHandle(memory, path); } TEST_CASE("read_write_view", "Virtual Memory Mapping") { @@ -466,7 +466,7 @@ TEST_CASE("read_write_view", "Virtual Memory Mapping") { } xe::memory::UnmapFileView(memory, reinterpret_cast(address), length); - xe::memory::CloseFileMappingHandle(memory); + xe::memory::CloseFileMappingHandle(memory, path); } } // namespace test diff --git a/src/xenia/cpu/backend/x64/x64_code_cache.cc b/src/xenia/cpu/backend/x64/x64_code_cache.cc index 6ea280908..c59786b5f 100644 --- a/src/xenia/cpu/backend/x64/x64_code_cache.cc +++ b/src/xenia/cpu/backend/x64/x64_code_cache.cc @@ -43,7 +43,7 @@ X64CodeCache::~X64CodeCache() { if (mapping_) { xe::memory::UnmapFileView(mapping_, generated_code_base_, kGeneratedCodeSize); - xe::memory::CloseFileMappingHandle(mapping_); + xe::memory::CloseFileMappingHandle(mapping_, file_name_); mapping_ = nullptr; } } diff --git a/src/xenia/memory.cc b/src/xenia/memory.cc index 8f20a79af..82991a158 100644 --- a/src/xenia/memory.cc +++ b/src/xenia/memory.cc @@ -115,7 +115,7 @@ Memory::~Memory() { // Unmap all views and close mapping. if (mapping_) { UnmapViews(); - xe::memory::CloseFileMappingHandle(mapping_); + xe::memory::CloseFileMappingHandle(mapping_, file_name_); mapping_base_ = nullptr; mapping_ = nullptr; }