[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
parent cbbb69273c
commit 220c657823
6 changed files with 16 additions and 6 deletions

View File

@ -99,7 +99,7 @@ typedef void* FileMappingHandle;
FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length,
PageAccess access, bool commit);
void CloseFileMappingHandle(FileMappingHandle handle);
void CloseFileMappingHandle(FileMappingHandle handle, std::wstring 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);

View File

@ -96,8 +96,9 @@ FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length,
return ret <= 0 ? nullptr : reinterpret_cast<FileMappingHandle>(ret);
}
void CloseFileMappingHandle(FileMappingHandle handle) {
close(static_cast<int>(reinterpret_cast<int64_t>(handle)));
void CloseFileMappingHandle(FileMappingHandle handle, std::wstring path) {
std::string full_path = "/" + xe::to_string(path);
shm_unlink(full_path.c_str());
}
void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,

View File

@ -152,7 +152,9 @@ FileMappingHandle CreateFileMappingHandle(std::wstring path, size_t length,
static_cast<DWORD>(length), full_path.c_str());
}
void CloseFileMappingHandle(FileMappingHandle handle) { CloseHandle(handle); }
void CloseFileMappingHandle(FileMappingHandle handle, std::wstring path) {
CloseHandle(handle);
}
void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,
PageAccess access, size_t file_offset) {

View File

@ -414,6 +414,13 @@ TEST_CASE("copy_and_swap_16_in_32_unaligned", "Copy and Swap") {
REQUIRE(true == true);
}
TEST_CASE("create_and_close_file_mapping", "Virtual Memory Mapping") {
auto memory = xe::memory::CreateFileMappingHandle(
L"test", 0x100, xe::memory::PageAccess::kReadWrite, false);
REQUIRE(memory);
xe::memory::CloseFileMappingHandle(memory, L"test");
}
} // namespace test
} // namespace base
} // namespace xe

View File

@ -42,7 +42,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;
}
}

View File

@ -112,7 +112,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;
}