From c723ec4054112b1ae7e9452c587fc743b3560b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Tue, 31 Dec 2024 08:24:43 -0800 Subject: [PATCH] Added memory map/unmap functions. --- include/bx/os.h | 12 +++++++ src/os.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 96 insertions(+), 3 deletions(-) diff --git a/include/bx/os.h b/include/bx/os.h index 4c0301909..3515d0cba 100644 --- a/include/bx/os.h +++ b/include/bx/os.h @@ -16,6 +16,9 @@ # define BX_DL_EXT "so" #endif // +BX_ERROR_RESULT(kErrorMemoryMapFailed, BX_MAKEFOURCC('b', 'x', '8', '0') ); +BX_ERROR_RESULT(kErrorMemoryUnmapFailed, BX_MAKEFOURCC('b', 'x', '8', '1') ); + namespace bx { /// @@ -58,6 +61,15 @@ namespace bx /// [[noreturn]] void exit(int32_t _exitCode); + /// + void* memoryMap(void* _address, size_t _size, Error* _err); + + /// + void memoryUnmap(void* _address, size_t _size, Error* _err); + + /// + size_t memoryPageSize(); + } // namespace bx #include "inline/os.inl" diff --git a/src/os.cpp b/src/os.cpp index e24b9aa5e..039be6f6d 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -10,7 +10,7 @@ #if BX_CRT_MSVC # include #else -# include +# include // syscall, _SC_PAGESIZE #endif // BX_CRT_MSVC #if BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT @@ -43,13 +43,14 @@ # if BX_PLATFORM_ANDROID # include // mallinfo -# elif BX_PLATFORM_LINUX \ +# elif BX_PLATFORM_LINUX \ || BX_PLATFORM_RPI # include // fopen -# include // syscall +# include # include # elif BX_PLATFORM_OSX # include // mach_task_basic_info +# include # endif // BX_PLATFORM_ANDROID #endif // BX_PLATFORM_ @@ -373,4 +374,84 @@ namespace bx ::exit(_exitCode); } + void* memoryMap(void* _address, size_t _size, Error* _err) + { + BX_ERROR_SCOPE(_err); + +#if BX_PLATFORM_LINUX || BX_PLATFORM_OSX + constexpr int32_t flags = 0 + | MAP_ANON + | MAP_PRIVATE + ; + + void* result = mmap(_address, _size, PROT_READ | PROT_WRITE, flags, -1 /*fd*/, 0 /*offset*/); + + if (MAP_FAILED == result) + { + BX_ERROR_SET( + _err + , kErrorMemoryMapFailed + , "kErrorMemoryMapFailed" + ); + + return NULL; + } + + return result; +#elif BX_PLATFORM_WINDOWS + void* result = VirtualAlloc(_address, _size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + + return result; +#else + BX_UNUSED(_address, _size); + BX_ERROR_SET(_err, kErrorMemoryMapFailed, "Not implemented!"); + return NULL; +#endif // BX_PLATFORM_* + } + + void memoryUnmap(void* _address, size_t _size, Error* _err) + { + BX_ERROR_SCOPE(_err); + +#if BX_PLATFORM_LINUX || BX_PLATFORM_OSX + int32_t result = munmap(_address, _size); + + if (-1 == result) + { + BX_ERROR_SET( + _err + , kErrorMemoryUnmapFailed + , "kErrorMemoryUnmapFailed" + ); + } +#elif BX_PLATFORM_WINDOWS + if (!VirtualFree(_address, _size, MEM_RELEASE) ) + { + BX_ERROR_SET( + _err + , kErrorMemoryUnmapFailed + , "kErrorMemoryUnmapFailed" + ); + } +#else + BX_UNUSED(_address, _size); + BX_ERROR_SET(_err, kErrorMemoryUnmapFailed, "Not implemented!"); +#endif // BX_PLATFORM_* + } + + size_t memoryPageSize() + { + size_t pageSize; +#if BX_PLATFORM_WINDOWS + SYSTEM_INFO si; //SystemInfo si; + memSet(&si, 0, sizeof(si) ); + ::GetSystemInfo(&si); + pageSize = si.dwAllocationGranularity; +#else + pageSize = sysconf(_SC_PAGESIZE); +#endif // BX_PLATFORM_WINDOWS + + return pageSize; + } + } // namespace bx