ReXGlue SDK
Loading...
Searching...
No Matches
rex::memory Namespace Reference

Classes

class  BitStream
class  ByteStream
class  ChunkedMappedMemoryWriter
class  GuestMembase
struct  HostStats
struct  IsMappedPtr
struct  IsMappedPtr< MappedPtr< T > >
class  MappedMemory
class  MappedPtr
class  MappedPtr< char >
class  MappedPtr< char16_t >
class  MappedPtr< void >
struct  MappedPtrInnerType
struct  MappedPtrInnerType< MappedPtr< T > >
class  RingBuffer
class  TagScope
struct  TagStats

Typedefs

using FourCCT = u32
using RingSizeT = u32
using FileMappingHandle = int

Enumerations

enum class  Tag : u8 { kCore = 0 , kThirdParty = 1 }
enum class  PageAccess : u8 {
  kNoAccess = 0 , kReadOnly = 1 << 0 , kReadWrite = kReadOnly | 1 << 1 , kExecuteReadOnly = kReadOnly | 1 << 2 ,
  kExecuteReadWrite = kReadWrite | 1 << 2
}
enum class  AllocationType : u8 { kReserve = 1 << 0 , kCommit = 1 << 1 , kReserveCommit = kReserve | kCommit }
enum class  DeallocationType : u8 { kRelease = 1 << 0 , kDecommit = 1 << 1 }

Functions

void Copy128Aligned (void *dest, const void *src, size_t count)
void copy_and_swap_16_aligned (void *dest, const void *src, size_t count)
void copy_and_swap_16_unaligned (void *dest, const void *src, size_t count)
void copy_and_swap_32_aligned (void *dest, const void *src, size_t count)
void copy_and_swap_32_unaligned (void *dest, const void *src, size_t count)
void copy_and_swap_64_aligned (void *dest, const void *src, size_t count)
void copy_and_swap_64_unaligned (void *dest, const void *src, size_t count)
void copy_and_swap_16_in_32_aligned (void *dest, const void *src, size_t count)
void copy_and_swap_16_in_32_unaligned (void *dest, const void *src, size_t count)
template<typename T>
void copy_and_swap (T *dest, const T *src, size_t count)
template<typename T>
T load (const void *mem)
template<typename T>
T load_and_swap (const void *mem)
template<>
std::string load_and_swap< std::string > (const void *mem)
template<>
std::u16string load_and_swap< std::u16string > (const void *mem)
template<typename T>
void store (void *mem, const T &value)
template<typename T>
void store_and_swap (void *mem, const T &value)
template<>
void store_and_swap< std::string_view > (void *mem, const std::string_view &value)
template<>
void store_and_swap< std::string > (void *mem, const std::string &value)
template<>
void store_and_swap< std::u16string_view > (void *mem, const std::u16string_view &value)
template<>
void store_and_swap< std::u16string > (void *mem, const std::u16string &value)
constexpr u32 LoadUint24BE (const u8 *p)
constexpr u32 LoadUint24LE (const u8 *p)
constexpr void StoreUint24LE (u8 *p, u32 value)
constexpr FourCCT make_fourcc (char a, char b, char c, char d)
constexpr FourCCT make_fourcc (const std::string_view fourcc)
void * HostAlloc (size_t size, size_t align=0) noexcept
void HostFree (void *ptr, size_t size=0, size_t align=0) noexcept
size_t HostSize (const void *ptr) noexcept
HostStats GetHostStats () noexcept
void SetAllocStackDepth (i32 depth) noexcept
i32 GetAllocStackDepth () noexcept
Tag GetThreadTag () noexcept
const char * GetTagName (Tag tag) noexcept
void SetTagName (Tag tag, const char *name) noexcept
template<typename T = u8*>
T GuestPtr (u8 *base, u32 guest_address) noexcept
u32 HostPtrToGuest (const u8 *base, const void *host_address) noexcept
size_t GetPageSize ()
size_t GetAllocationGranularity ()
bool IsWritableExecutableMemorySupported ()
void * AllocFixed (void *base_address, size_t length, AllocationType allocation_type, PageAccess access)
bool DeallocFixed (void *base_address, size_t length, DeallocationType deallocation_type)
bool Protect (void *base_address, size_t length, PageAccess access, PageAccess *out_old_access=nullptr)
bool QueryProtect (void *base_address, size_t &length, PageAccess &out_access)
template<typename T>
T * AlignedAlloc (size_t alignment)
template<typename T>
void AlignedFree (T *ptr)
FileMappingHandle CreateFileMappingHandle (const std::filesystem::path &path, size_t length, PageAccess access, bool commit)
std::filesystem::path CreateFileMappingName (std::string_view prefix)
void RemoveStaleFileMappings (std::string_view prefix)
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)
void * LowAddress (void *address)

Variables

constexpr size_t kMaxTags = 16
template<typename T>
constexpr bool is_mapped_ptr_v = IsMappedPtr<T>::value
constexpr FileMappingHandle kFileMappingHandleInvalid = -1

Detailed Description

Guest memory types and helpers.

Typedef Documentation

◆ FileMappingHandle

File mapping handle, a file descriptor.

◆ FourCCT

using rex::memory::FourCCT = u32

4 character code packed into 32 bits.

Enumeration Type Documentation

◆ AllocationType

enum class rex::memory::AllocationType : u8
strong

Allocation mode AllocFixed uses.

Enumerator
kReserve 

Reserve address space without backing it.

kCommit 

Back reserved address space with memory.

◆ DeallocationType

enum class rex::memory::DeallocationType : u8
strong

Deallocation mode DeallocFixed uses.

Enumerator
kRelease 

Return the address space to the system.

kDecommit 

Release the backing memory and keep the reservation.

◆ Tag

enum class rex::memory::Tag : u8
strong

Subsystem that owns a host allocation. A module defines its own value below kMaxTags.

Enumerator
kCore 

Default tag.

kThirdParty 

Memory requested by libraries that have an allocation hook.

Function Documentation

◆ AlignedAlloc()

template<typename T>
T * rex::memory::AlignedAlloc ( size_t alignment)
inline

Allocates aligned host memory for one object.

Parameters
alignmentAlignment in bytes, a power of 2.
Returns
Uninitialized storage for a T, or null on failure.

◆ AlignedFree()

template<typename T>
void rex::memory::AlignedFree ( T * ptr)

Frees memory AlignedAlloc returned.

Parameters
ptrPointer AlignedAlloc returned.

◆ AllocFixed()

void * rex::memory::AllocFixed ( void * base_address,
size_t length,
AllocationType allocation_type,
PageAccess access )

Allocates host pages at a page aligned address.

Parameters
base_addressRequested address, or null to let the system choose.
allocation_typeReserve, commit or both.
accessAccess rights of the new pages.
Returns
Address of the allocation, or null when the range is not available.

◆ CloseFileMappingHandle()

void rex::memory::CloseFileMappingHandle ( FileMappingHandle handle,
const std::filesystem::path & path )

Closes a handle CreateFileMappingHandle returned. POSIX unlinks the object.

Parameters
pathName the object was created with.

◆ Copy128Aligned()

void rex::memory::Copy128Aligned ( void * dest,
const void * src,
size_t count )

Copies 16 byte blocks between 16 byte aligned buffers.

Parameters
countNumber of 16 byte blocks.

◆ copy_and_swap()

template<typename T>
void rex::memory::copy_and_swap ( T * dest,
const T * src,
size_t count )

Copies values and swaps the byte order of each.

Template Parameters
TElement type, 1, 2, 4 or 8 bytes.

◆ copy_and_swap_16_aligned()

void rex::memory::copy_and_swap_16_aligned ( void * dest,
const void * src,
size_t count )

Copies 16 bit values and swaps the byte order of each. Both buffers are 16 byte aligned.

Parameters
countNumber of 16 bit values.

◆ copy_and_swap_16_in_32_aligned()

void rex::memory::copy_and_swap_16_in_32_aligned ( void * dest,
const void * src,
size_t count )

Copies 32 bit values and swaps the two 16 bit halves of each. Both buffers are 16 byte aligned.

Parameters
countNumber of 32 bit values.

◆ copy_and_swap_16_in_32_unaligned()

void rex::memory::copy_and_swap_16_in_32_unaligned ( void * dest,
const void * src,
size_t count )

Copies 32 bit values and swaps the two 16 bit halves of each.

Parameters
countNumber of 32 bit values.

◆ copy_and_swap_16_unaligned()

void rex::memory::copy_and_swap_16_unaligned ( void * dest,
const void * src,
size_t count )

Copies 16 bit values and swaps the byte order of each.

Parameters
countNumber of 16 bit values.

◆ copy_and_swap_32_aligned()

void rex::memory::copy_and_swap_32_aligned ( void * dest,
const void * src,
size_t count )

Copies 32 bit values and swaps the byte order of each. Both buffers are 16 byte aligned.

Parameters
countNumber of 32 bit values.

◆ copy_and_swap_32_unaligned()

void rex::memory::copy_and_swap_32_unaligned ( void * dest,
const void * src,
size_t count )

Copies 32 bit values and swaps the byte order of each.

Parameters
countNumber of 32 bit values.

◆ copy_and_swap_64_aligned()

void rex::memory::copy_and_swap_64_aligned ( void * dest,
const void * src,
size_t count )

Copies 64 bit values and swaps the byte order of each. Both buffers are 16 byte aligned.

Parameters
countNumber of 64 bit values.

◆ copy_and_swap_64_unaligned()

void rex::memory::copy_and_swap_64_unaligned ( void * dest,
const void * src,
size_t count )

Copies 64 bit values and swaps the byte order of each.

Parameters
countNumber of 64 bit values.

◆ CreateFileMappingHandle()

FileMappingHandle rex::memory::CreateFileMappingHandle ( const std::filesystem::path & path,
size_t length,
PageAccess access,
bool commit )

Creates a named shared memory object.

Parameters
accessAccess rights of views of the object.
commitTrue to commit the whole object up front. Only Windows reads it.
Returns
Handle to the object, or kFileMappingHandleInvalid on failure or when an object with that name already exists.

◆ CreateFileMappingName()

std::filesystem::path rex::memory::CreateFileMappingName ( std::string_view prefix)

Builds a unique name from the prefix, process ID, underscore and random hex.

◆ DeallocFixed()

bool rex::memory::DeallocFixed ( void * base_address,
size_t length,
DeallocationType deallocation_type )

Decommits or releases host pages.

Parameters
lengthByte count. Windows ignores it for kRelease and releases the whole region.
deallocation_typeDecommit or release.

◆ GetAllocationGranularity()

size_t rex::memory::GetAllocationGranularity ( )
Returns
Granularity in bytes. 64 KiB on Windows, the page size on POSIX.

◆ GetAllocStackDepth()

i32 rex::memory::GetAllocStackDepth ( )
nodiscardnoexcept

Returns how many call stack frames the profiler keeps per allocation, 0 for none.

◆ GetHostStats()

HostStats rex::memory::GetHostStats ( )
noexcept

Returns a snapshot of the counters, not atomic across tags.

◆ GetPageSize()

size_t rex::memory::GetPageSize ( )
Returns
Page size in bytes, usually 4 KiB.

◆ GetTagName()

const char * rex::memory::GetTagName ( Tag tag)
noexcept
Returns
The name, a string with static storage, or "Invalid" for a slot no module has named.

◆ GetThreadTag()

Tag rex::memory::GetThreadTag ( )
noexcept

Returns the tag of the calling thread. New allocations on this thread receive it.

◆ GuestPtr()

template<typename T = u8*>
T rex::memory::GuestPtr ( u8 * base,
u32 guest_address )
inlinenoexcept

Translates a guest address to a host pointer with the arithmetic recompiled code uses.

◆ HostAlloc()

void * rex::memory::HostAlloc ( size_t size,
size_t align = 0 )
noexcept

Allocates host memory under the tag of the calling thread. Works before main and after Shutdown.

Parameters
sizeThe size in bytes, 0 returns a unique block.
alignThe alignment in bytes, a power of 2, 0 for the default alignment of 16.
Returns
The block, or null when the system is out of memory or the alignment is not valid.

◆ HostFree()

void rex::memory::HostFree ( void * ptr,
size_t size = 0,
size_t align = 0 )
noexcept

Frees a block made by HostAlloc.

Parameters
ptrThe block, null does nothing.
sizeThe size passed to HostAlloc, 0 when unknown.
alignThe alignment passed to HostAlloc, 0 when unknown.

◆ HostPtrToGuest()

u32 rex::memory::HostPtrToGuest ( const u8 * base,
const void * host_address )
inlinenoexcept

Translates a host pointer into guest memory back to its guest address. Inverts GuestPtr.

Parameters
host_addressThe host pointer, null for no address.
Returns
The guest address, 0 for a null pointer.

◆ HostSize()

size_t rex::memory::HostSize ( const void * ptr)
noexcept

Returns the requested size of a block made by HostAlloc.

Parameters
ptrThe block, not null.

◆ IsWritableExecutableMemorySupported()

bool rex::memory::IsWritableExecutableMemorySupported ( )

Reports whether host pages can use PageAccess::kExecuteReadWrite.

Returns
True when writable executable pages are supported.

◆ load()

template<typename T>
T rex::memory::load ( const void * mem)
inline

Loads a value from memory of any alignment.

◆ load_and_swap()

template<typename T>
T rex::memory::load_and_swap ( const void * mem)
inline

Loads a value from memory and swaps its byte order.

Template Parameters
TValue type, 1, 2, 4 or 8 bytes.
Returns
The byte swapped value at mem.

◆ load_and_swap< std::string >()

template<>
std::string rex::memory::load_and_swap< std::string > ( const void * mem)
inline

Loads a NUL terminated 8 bit string.

Returns
The characters before the terminator.

◆ load_and_swap< std::u16string >()

template<>
std::u16string rex::memory::load_and_swap< std::u16string > ( const void * mem)
inline

Loads a NUL terminated big endian UTF-16 string.

Returns
The code units before the terminator in host byte order.

◆ LoadUint24BE()

u32 rex::memory::LoadUint24BE ( const u8 * p)
constexpr

Reads a 24 bit big endian value.

Parameters
pThe 3 source bytes.

◆ LoadUint24LE()

u32 rex::memory::LoadUint24LE ( const u8 * p)
constexpr

Reads a 24 bit little endian value.

Parameters
pThe 3 source bytes.

◆ LowAddress()

void * rex::memory::LowAddress ( void * address)
inline

Truncates a host pointer to its low 32 bits.

Returns
The pointer with the upper 32 bits cleared.

◆ make_fourcc() [1/2]

FourCCT rex::memory::make_fourcc ( char a,
char b,
char c,
char d )
constexpr

Packs 4 characters into a FourCC in host byte order.

Parameters
aFirst character, the most significant byte.
dFourth character, the least significant byte.
Returns
The packed code. make_fourcc('a', 'b', 'c', 'd') is 0x61626364.

◆ make_fourcc() [2/2]

FourCCT rex::memory::make_fourcc ( const std::string_view fourcc)
constexpr

Packs a 4 character string into a FourCC in host byte order.

Parameters
fourccString of exactly 4 characters. Any other length throws std::runtime_error.
Returns
The packed code. make_fourcc("abcd") is 0x61626364.

◆ MapFileView()

void * rex::memory::MapFileView ( FileMappingHandle handle,
void * base_address,
size_t length,
PageAccess access,
size_t file_offset )

Maps a view of a shared memory object.

Parameters
handleHandle CreateFileMappingHandle returned.
base_addressRequested address, or null to let the system choose.
file_offsetOffset of the view in the object, page aligned.
Returns
Address of the view, or null on failure.

◆ Protect()

bool rex::memory::Protect ( void * base_address,
size_t length,
PageAccess access,
PageAccess * out_old_access = nullptr )

Changes the access rights of host pages.

Parameters
out_old_accessReceives the previous access rights of the first page, or null.

◆ QueryProtect()

bool rex::memory::QueryProtect ( void * base_address,
size_t & length,
PageAccess & out_access )

Returns the access rights of the region that holds an address.

Parameters
base_addressAddress inside the region.
lengthReceives the byte count of consecutive pages with the same access rights.
out_accessReceives the access rights.

◆ RemoveStaleFileMappings()

void rex::memory::RemoveStaleFileMappings ( std::string_view prefix)

Removes shared memory objects whose name starts with the prefix, a dead process ID and an underscore. Only Linux removes any.

Parameters
prefixLeading part of the names to inspect.

◆ SetAllocStackDepth()

void rex::memory::SetAllocStackDepth ( i32 depth)
noexcept

Sets how many call stack frames the profiler keeps per allocation. No effect without profiling.

Parameters
depthThe frame count, clamped to 0 through 32, 0 for none.

◆ SetTagName()

void rex::memory::SetTagName ( Tag tag,
const char * name )
noexcept

Names a tag for the profiler and diagnostics. Works before main.

Parameters
tagThe tag, below kMaxTags. A larger value does nothing.
nameThe name, shorter than 32 characters. The allocator copies it.

◆ store()

template<typename T>
void rex::memory::store ( void * mem,
const T & value )
inline

Stores a value to memory of any alignment.

◆ store_and_swap()

template<typename T>
void rex::memory::store_and_swap ( void * mem,
const T & value )
inline

Swaps the byte order of a value and stores it to memory.

Template Parameters
TValue type, 1, 2, 4 or 8 bytes.

◆ store_and_swap< std::string >()

template<>
void rex::memory::store_and_swap< std::string > ( void * mem,
const std::string & value )
inline

Stores the characters of an 8 bit string without a terminator.

◆ store_and_swap< std::string_view >()

template<>
void rex::memory::store_and_swap< std::string_view > ( void * mem,
const std::string_view & value )
inline

Stores the characters of an 8 bit string without a terminator.

◆ store_and_swap< std::u16string >()

template<>
void rex::memory::store_and_swap< std::u16string > ( void * mem,
const std::u16string & value )
inline

Stores a UTF-16 string as big endian code units without a terminator.

◆ store_and_swap< std::u16string_view >()

template<>
void rex::memory::store_and_swap< std::u16string_view > ( void * mem,
const std::u16string_view & value )
inline

Stores a UTF-16 string as big endian code units without a terminator.

◆ StoreUint24LE()

void rex::memory::StoreUint24LE ( u8 * p,
u32 value )
constexpr

Writes a 24 bit little endian value.

Parameters
pThe 3 destination bytes.
valueValue whose top 8 bits are discarded.

◆ UnmapFileView()

bool rex::memory::UnmapFileView ( FileMappingHandle handle,
void * base_address,
size_t length )

Unmaps a view MapFileView returned.

Variable Documentation

◆ kFileMappingHandleInvalid

FileMappingHandle rex::memory::kFileMappingHandleInvalid = -1
constexpr

Value CreateFileMappingHandle returns on failure.

◆ kMaxTags

size_t rex::memory::kMaxTags = 16
inlineconstexpr

The number of tag slots. A tag value at or above it is not valid.