Merge branch 'master' into vulkan
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "xenia/base/assert.h"
|
||||
#include "xenia/base/byte_order.h"
|
||||
#include "xenia/base/platform.h"
|
||||
|
||||
namespace xe {
|
||||
namespace memory {
|
||||
@@ -96,12 +97,21 @@ void AlignedFree(T* ptr) {
|
||||
#endif // XE_COMPILER_MSVC
|
||||
}
|
||||
|
||||
#if XE_PLATFORM_WIN32
|
||||
// HANDLE.
|
||||
typedef void* FileMappingHandle;
|
||||
constexpr FileMappingHandle kFileMappingHandleInvalid = nullptr;
|
||||
#else
|
||||
// File descriptor.
|
||||
typedef int FileMappingHandle;
|
||||
constexpr FileMappingHandle kFileMappingHandleInvalid = -1;
|
||||
#endif
|
||||
|
||||
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);
|
||||
|
||||
@@ -40,7 +40,13 @@ void* AllocFixed(void* base_address, size_t length,
|
||||
AllocationType allocation_type, PageAccess access) {
|
||||
// mmap does not support reserve / commit, so ignore allocation_type.
|
||||
uint32_t prot = ToPosixProtectFlags(access);
|
||||
return mmap(base_address, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
void* result = mmap(base_address, length, prot,
|
||||
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
|
||||
if (result == MAP_FAILED) {
|
||||
return nullptr;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
bool DeallocFixed(void* base_address, size_t length,
|
||||
@@ -80,25 +86,28 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path,
|
||||
assert_always();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
oflag |= O_CREAT;
|
||||
int ret = shm_open(path.c_str(), oflag, 0777);
|
||||
if (ret > 0) {
|
||||
ftruncate64(ret, length);
|
||||
auto full_path = "/" / path;
|
||||
int ret = shm_open(full_path.c_str(), oflag, 0777);
|
||||
if (ret < 0) {
|
||||
return kFileMappingHandleInvalid;
|
||||
}
|
||||
|
||||
return ret <= 0 ? nullptr : reinterpret_cast<FileMappingHandle>(ret);
|
||||
ftruncate64(ret, length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CloseFileMappingHandle(FileMappingHandle handle) {
|
||||
close((intptr_t)handle);
|
||||
void CloseFileMappingHandle(FileMappingHandle handle,
|
||||
const std::filesystem::path& path) {
|
||||
close(handle);
|
||||
auto full_path = "/" / path;
|
||||
shm_unlink(full_path.c_str());
|
||||
}
|
||||
|
||||
void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,
|
||||
PageAccess access, size_t file_offset) {
|
||||
uint32_t prot = ToPosixProtectFlags(access);
|
||||
return mmap64(base_address, length, prot, MAP_PRIVATE | MAP_ANONYMOUS,
|
||||
reinterpret_cast<intptr_t>(handle), file_offset);
|
||||
return mmap64(base_address, length, prot, MAP_PRIVATE | MAP_ANONYMOUS, handle,
|
||||
file_offset);
|
||||
}
|
||||
|
||||
bool UnmapFileView(FileMappingHandle handle, void* base_address,
|
||||
|
||||
@@ -147,12 +147,16 @@ FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path,
|
||||
bool commit) {
|
||||
DWORD protect =
|
||||
ToWin32ProtectFlags(access) | (commit ? SEC_COMMIT : SEC_RESERVE);
|
||||
auto full_path = "Local" / path;
|
||||
return CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, protect,
|
||||
static_cast<DWORD>(length >> 32),
|
||||
static_cast<DWORD>(length), 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,
|
||||
PageAccess access, size_t file_offset) {
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
#include "xenia/base/memory.h"
|
||||
|
||||
#include "third_party/catch/include/catch.hpp"
|
||||
#include "third_party/fmt/include/fmt/format.h"
|
||||
|
||||
#include "xenia/base/clock.h"
|
||||
|
||||
namespace xe {
|
||||
namespace base {
|
||||
@@ -414,6 +417,58 @@ 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 path = fmt::format("xenia_test_{}", Clock::QueryHostTickCount());
|
||||
auto memory = xe::memory::CreateFileMappingHandle(
|
||||
path, 0x100, xe::memory::PageAccess::kReadWrite, true);
|
||||
REQUIRE(memory != xe::memory::FileMappingHandleInvalid);
|
||||
xe::memory::CloseFileMappingHandle(memory, path);
|
||||
}
|
||||
|
||||
TEST_CASE("map_view", "Virtual Memory Mapping") {
|
||||
auto path = fmt::format("xenia_test_{}", Clock::QueryHostTickCount());
|
||||
const size_t length = 0x100;
|
||||
auto memory = xe::memory::CreateFileMappingHandle(
|
||||
path, length, xe::memory::PageAccess::kReadWrite, true);
|
||||
REQUIRE(memory != xe::memory::FileMappingHandleInvalid);
|
||||
|
||||
uintptr_t address = 0x100000000;
|
||||
auto view =
|
||||
xe::memory::MapFileView(memory, reinterpret_cast<void*>(address), length,
|
||||
xe::memory::PageAccess::kReadWrite, 0);
|
||||
REQUIRE(reinterpret_cast<uintptr_t>(view) == address);
|
||||
|
||||
xe::memory::UnmapFileView(memory, reinterpret_cast<void*>(address), length);
|
||||
xe::memory::CloseFileMappingHandle(memory, path);
|
||||
}
|
||||
|
||||
TEST_CASE("read_write_view", "Virtual Memory Mapping") {
|
||||
const size_t length = 0x100;
|
||||
auto path = fmt::format("xenia_test_{}", Clock::QueryHostTickCount());
|
||||
auto memory = xe::memory::CreateFileMappingHandle(
|
||||
path, length, xe::memory::PageAccess::kReadWrite, true);
|
||||
REQUIRE(memory != xe::memory::FileMappingHandleInvalid);
|
||||
|
||||
uintptr_t address = 0x100000000;
|
||||
auto view =
|
||||
xe::memory::MapFileView(memory, reinterpret_cast<void*>(address), length,
|
||||
xe::memory::PageAccess::kReadWrite, 0);
|
||||
REQUIRE(reinterpret_cast<uintptr_t>(view) == address);
|
||||
|
||||
for (uint32_t i = 0; i < length; i += sizeof(uint8_t)) {
|
||||
auto p_value = reinterpret_cast<uint8_t*>(address + i);
|
||||
*p_value = i;
|
||||
}
|
||||
for (uint32_t i = 0; i < length; i += sizeof(uint8_t)) {
|
||||
auto p_value = reinterpret_cast<uint8_t*>(address + i);
|
||||
uint8_t value = *p_value;
|
||||
REQUIRE(value == i);
|
||||
}
|
||||
|
||||
xe::memory::UnmapFileView(memory, reinterpret_cast<void*>(address), length);
|
||||
xe::memory::CloseFileMappingHandle(memory, path);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace base
|
||||
} // namespace xe
|
||||
|
||||
@@ -813,7 +813,7 @@ TEST_CASE("Create and Run Thread", "Thread") {
|
||||
result = Wait(Thread::GetCurrentThread(), false, 50ms);
|
||||
REQUIRE(result == WaitResult::kTimeout);
|
||||
|
||||
params.stack_size = 16 * 1024;
|
||||
params.stack_size = 16 * 1024 * 1024;
|
||||
thread = Thread::Create(params, [] {
|
||||
while (true) {
|
||||
Thread::Exit(-1);
|
||||
|
||||
@@ -505,6 +505,7 @@ class PosixCondition<Thread> : public PosixConditionBase {
|
||||
}
|
||||
}
|
||||
if (pthread_create(&thread_, &attr, ThreadStartRoutine, start_data) != 0) {
|
||||
pthread_attr_destroy(&attr);
|
||||
return false;
|
||||
}
|
||||
pthread_attr_destroy(&attr);
|
||||
@@ -1006,7 +1007,10 @@ Thread* Thread::GetCurrentThread() {
|
||||
pthread_t handle = pthread_self();
|
||||
|
||||
current_thread_ = new PosixThread(handle);
|
||||
atexit([] { delete current_thread_; });
|
||||
// TODO(bwrsandman): Disabling deleting thread_local current thread to prevent
|
||||
// assert in destructor. Since this is thread local, the
|
||||
// "memory leaking" is controlled.
|
||||
// atexit([] { delete current_thread_; });
|
||||
|
||||
return current_thread_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user