ReXGlue SDK
Loading...
Searching...
No Matches
virtual_memory.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <cstdlib>
17#include <filesystem>
18#include <memory>
19#include <string_view>
20
21#include <rex/platform.h>
22#include <rex/types.h>
23#include <rex/utility.h>
24
25namespace rex::memory {
26
27#if REX_PLATFORM_ANDROID
29REX_API void AndroidInitialize();
31REX_API void AndroidShutdown();
32#endif
33
38
43
44enum class PageAccess : u8 {
45 kNoAccess = 0,
46 kReadOnly = 1 << 0,
47 kReadWrite = kReadOnly | 1 << 1,
48 kExecuteReadOnly = kReadOnly | 1 << 2,
49 kExecuteReadWrite = kReadWrite | 1 << 2,
50};
51
53enum class AllocationType : u8 {
54 kReserve = 1 << 0,
55 kCommit = 1 << 1,
56 kReserveCommit = kReserve | kCommit,
57};
58
60enum class DeallocationType : u8 {
61 kRelease = 1 << 0,
62 kDecommit = 1 << 1,
63};
64
71
80REX_API void* AllocFixed(void* base_address, size_t length, AllocationType allocation_type,
81 PageAccess access);
82
89REX_API bool DeallocFixed(void* base_address, size_t length, DeallocationType deallocation_type);
90
96REX_API bool Protect(void* base_address, size_t length, PageAccess access,
97 PageAccess* out_old_access = nullptr);
98
106REX_API bool QueryProtect(void* base_address, size_t& length, PageAccess& out_access);
107
114template <typename T>
115inline T* AlignedAlloc(size_t alignment) {
116#if REX_PLATFORM_WIN32
117 return reinterpret_cast<T*>(_aligned_malloc(sizeof(T), alignment));
118#else
119 void* ptr = nullptr;
120 if (posix_memalign(&ptr, alignment, sizeof(T))) {
121 return nullptr;
122 }
123 return reinterpret_cast<T*>(ptr);
124#endif
125}
126
132template <typename T>
133void AlignedFree(T* ptr) {
134#if REX_PLATFORM_WIN32
135 _aligned_free(ptr);
136#else
137 free(ptr);
138#endif
139}
140
141#if REX_PLATFORM_WIN32
143using FileMappingHandle = void*;
146#else
151#endif
152
161REX_API FileMappingHandle CreateFileMappingHandle(const std::filesystem::path& path, size_t length,
162 PageAccess access, bool commit);
163
165REX_API std::filesystem::path CreateFileMappingName(std::string_view prefix);
166
173REX_API void RemoveStaleFileMappings(std::string_view prefix);
174
180REX_API void CloseFileMappingHandle(FileMappingHandle handle, const std::filesystem::path& path);
181
190REX_API void* MapFileView(FileMappingHandle handle, void* base_address, size_t length,
191 PageAccess access, size_t file_offset);
192
194REX_API bool UnmapFileView(FileMappingHandle handle, void* base_address, size_t length);
195
201inline void* LowAddress(void* address) {
202 // NOLINTNEXTLINE(performance-no-int-to-ptr): LowAddress truncates a host pointer
203 return reinterpret_cast<void*>(reinterpret_cast<u64>(address) & 0xFFFFFFFF);
204}
205
206} // namespace rex::memory
207
208namespace rex::memory {
209
211class REX_API MappedMemory : public rex::NonCopyable {
212 public:
213 enum class Mode : u8 {
214 kRead,
215 kReadWrite,
216 };
217
225 static std::unique_ptr<MappedMemory> Open(const std::filesystem::path& path, Mode mode,
226 size_t offset = 0, size_t length = 0);
227
228 MappedMemory() = default;
229
231 MappedMemory(void* data, size_t size) : data_(data), size_(size) {}
232 virtual ~MappedMemory() = default;
233
239 std::unique_ptr<MappedMemory> Range(size_t offset, size_t length) const {
240 return std::make_unique<MappedMemory>(GetData() + offset, length);
241 }
242
244 u8* GetData() const { return reinterpret_cast<u8*>(data_); }
245
246 size_t GetSize() const { return size_; }
247
253 virtual void Close(u64 truncate_size = 0) { (void)truncate_size; }
254
255 virtual void Flush() {}
256
264 virtual bool Remap(size_t offset, size_t length) {
265 (void)offset;
266 (void)length;
267 return false;
268 }
269
270 protected:
271 void* data_ = nullptr;
272 size_t size_ = 0;
273};
274
277 public:
278 virtual ~ChunkedMappedMemoryWriter() = default;
279
286 static std::unique_ptr<ChunkedMappedMemoryWriter> Open(const std::filesystem::path& path,
287 size_t chunk_size,
288 bool low_address_space = false);
289
296 virtual u8* Allocate(size_t length) = 0;
297
298 virtual void Flush() = 0;
299
301 virtual void FlushNew() = 0;
302
303 protected:
309 ChunkedMappedMemoryWriter(const std::filesystem::path& path, size_t chunk_size,
310 bool low_address_space)
311 : path_(path), chunk_size_(chunk_size), low_address_space_(low_address_space) {}
312
313 std::filesystem::path path_;
314 size_t chunk_size_;
315 bool low_address_space_;
316};
317
318} // namespace rex::memory
Definition utility.h:37
static std::unique_ptr< ChunkedMappedMemoryWriter > Open(const std::filesystem::path &path, size_t chunk_size, bool low_address_space=false)
ChunkedMappedMemoryWriter(const std::filesystem::path &path, size_t chunk_size, bool low_address_space)
Definition virtual_memory.h:309
virtual u8 * Allocate(size_t length)=0
Definition virtual_memory.h:211
virtual bool Remap(size_t offset, size_t length)
Definition virtual_memory.h:264
std::unique_ptr< MappedMemory > Range(size_t offset, size_t length) const
Definition virtual_memory.h:239
MappedMemory(void *data, size_t size)
Definition virtual_memory.h:231
u8 * GetData() const
Definition virtual_memory.h:244
static std::unique_ptr< MappedMemory > Open(const std::filesystem::path &path, Mode mode, size_t offset=0, size_t length=0)
virtual void Close(u64 truncate_size=0)
Definition virtual_memory.h:253
Definition endian.h:27
T * AlignedAlloc(size_t alignment)
Definition virtual_memory.h:115
void AlignedFree(T *ptr)
Definition virtual_memory.h:133
int FileMappingHandle
Definition virtual_memory.h:148
std::filesystem::path CreateFileMappingName(std::string_view prefix)
bool UnmapFileView(FileMappingHandle handle, void *base_address, size_t length)
void * AllocFixed(void *base_address, size_t length, AllocationType allocation_type, PageAccess access)
size_t GetAllocationGranularity()
AllocationType
Definition virtual_memory.h:53
@ kCommit
Definition virtual_memory.h:55
@ kReserve
Definition virtual_memory.h:54
bool Protect(void *base_address, size_t length, PageAccess access, PageAccess *out_old_access=nullptr)
bool DeallocFixed(void *base_address, size_t length, DeallocationType deallocation_type)
bool QueryProtect(void *base_address, size_t &length, PageAccess &out_access)
FileMappingHandle CreateFileMappingHandle(const std::filesystem::path &path, size_t length, PageAccess access, bool commit)
void RemoveStaleFileMappings(std::string_view prefix)
bool IsWritableExecutableMemorySupported()
constexpr FileMappingHandle kFileMappingHandleInvalid
Definition virtual_memory.h:150
DeallocationType
Definition virtual_memory.h:60
@ kRelease
Definition virtual_memory.h:61
@ kDecommit
Definition virtual_memory.h:62
void * MapFileView(FileMappingHandle handle, void *base_address, size_t length, PageAccess access, size_t file_offset)
void CloseFileMappingHandle(FileMappingHandle handle, const std::filesystem::path &path)
size_t GetPageSize()
void * LowAddress(void *address)
Definition virtual_memory.h:201
Platform flags, packing macros and export markers.
#define REX_API
Definition platform.h:177
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.