[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.
This commit is contained in:
Sandy Carter 2019-07-11 09:44:42 -04:00 committed by Triang3l
parent 2c7009ca80
commit 49e194009b
6 changed files with 15 additions and 9 deletions

View File

@ -101,7 +101,8 @@ typedef void* FileMappingHandle;
FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path,
size_t length, PageAccess access, size_t length, PageAccess access,
bool commit); 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, void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,
PageAccess access, size_t file_offset); PageAccess access, size_t file_offset);
bool UnmapFileView(FileMappingHandle handle, void* base_address, size_t length); bool UnmapFileView(FileMappingHandle handle, void* base_address, size_t length);

View File

@ -97,8 +97,10 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path,
return ret <= 0 ? nullptr : reinterpret_cast<FileMappingHandle>(ret); return ret <= 0 ? nullptr : reinterpret_cast<FileMappingHandle>(ret);
} }
void CloseFileMappingHandle(FileMappingHandle handle) { void CloseFileMappingHandle(FileMappingHandle handle,
close(static_cast<int>(reinterpret_cast<int64_t>(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, void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,

View File

@ -153,7 +153,10 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path,
static_cast<DWORD>(length), full_path.c_str()); static_cast<DWORD>(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, void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,
PageAccess access, size_t file_offset) { PageAccess access, size_t file_offset) {

View File

@ -422,7 +422,7 @@ TEST_CASE("create_and_close_file_mapping", "Virtual Memory Mapping") {
auto memory = xe::memory::CreateFileMappingHandle( auto memory = xe::memory::CreateFileMappingHandle(
path, 0x100, xe::memory::PageAccess::kReadWrite, true); path, 0x100, xe::memory::PageAccess::kReadWrite, true);
REQUIRE(memory); REQUIRE(memory);
xe::memory::CloseFileMappingHandle(memory); xe::memory::CloseFileMappingHandle(memory, path);
} }
TEST_CASE("map_view", "Virtual Memory Mapping") { TEST_CASE("map_view", "Virtual Memory Mapping") {
@ -439,7 +439,7 @@ TEST_CASE("map_view", "Virtual Memory Mapping") {
REQUIRE(reinterpret_cast<uintptr_t>(view) == address); REQUIRE(reinterpret_cast<uintptr_t>(view) == address);
xe::memory::UnmapFileView(memory, reinterpret_cast<void*>(address), length); xe::memory::UnmapFileView(memory, reinterpret_cast<void*>(address), length);
xe::memory::CloseFileMappingHandle(memory); xe::memory::CloseFileMappingHandle(memory, path);
} }
TEST_CASE("read_write_view", "Virtual Memory Mapping") { 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<void*>(address), length); xe::memory::UnmapFileView(memory, reinterpret_cast<void*>(address), length);
xe::memory::CloseFileMappingHandle(memory); xe::memory::CloseFileMappingHandle(memory, path);
} }
} // namespace test } // namespace test

View File

@ -43,7 +43,7 @@ X64CodeCache::~X64CodeCache() {
if (mapping_) { if (mapping_) {
xe::memory::UnmapFileView(mapping_, generated_code_base_, xe::memory::UnmapFileView(mapping_, generated_code_base_,
kGeneratedCodeSize); kGeneratedCodeSize);
xe::memory::CloseFileMappingHandle(mapping_); xe::memory::CloseFileMappingHandle(mapping_, file_name_);
mapping_ = nullptr; mapping_ = nullptr;
} }
} }

View File

@ -115,7 +115,7 @@ Memory::~Memory() {
// Unmap all views and close mapping. // Unmap all views and close mapping.
if (mapping_) { if (mapping_) {
UnmapViews(); UnmapViews();
xe::memory::CloseFileMappingHandle(mapping_); xe::memory::CloseFileMappingHandle(mapping_, file_name_);
mapping_base_ = nullptr; mapping_base_ = nullptr;
mapping_ = nullptr; mapping_ = nullptr;
} }