Skip to main content
The memory_manager class implements Windows-style virtual memory management with support for reserving, committing, protecting, and releasing memory regions.

Overview

This class provides comprehensive memory management that mirrors Windows virtual memory semantics:
  • Reserve/commit two-phase allocation model
  • Memory protection and permissions
  • Section views and image mappings
  • Memory-mapped I/O (MMIO) regions
  • 64KB allocation granularity
  • Address space layout versioning
  • Serialization support for snapshots

Constructor

memory_interface&
Reference to the underlying memory backend

Memory Constants

Core Memory Operations

read_memory

Reads memory from the virtual address space.
uint64_t
Virtual address to read from
void*
Destination buffer
size_t
Number of bytes to read
Throws: Exception if the memory region is not accessible.

try_read_memory

Attempts to read memory without throwing exceptions.
Returns: true if successful, false if the read failed.

write_memory

Writes memory to the virtual address space.
uint64_t
Virtual address to write to
const void*
Source buffer
size_t
Number of bytes to write
Throws: Exception if the memory region is not writable.

try_write_memory

Attempts to write memory without throwing exceptions.
Returns: true if successful, false if the write failed.

Allocation and Deallocation

allocate_memory (at specific address)

Allocates memory at a specific virtual address.
uint64_t
Desired allocation address (must be aligned to 64KB)
size_t
Size in bytes to allocate
nt_memory_permission
Initial memory protection flags
bool
default:"false"
If true, only reserve the address space without committing
memory_region_kind
default:"private_allocation"
Type of memory region to create
Returns: true if allocation succeeded, false otherwise.

allocate_memory (find free address)

Allocates memory at an automatically chosen address.
size_t
Size in bytes to allocate
nt_memory_permission
Initial memory protection flags
bool
default:"false"
If true, only reserve the address space
uint64_t
default:"0"
Minimum address to start searching from
memory_region_kind
default:"private_allocation"
Type of memory region to create
Returns: Base address of the allocated region, or 0 on failure.

commit_memory

Commits previously reserved memory, making it accessible.
uint64_t
Base address to commit
size_t
Number of bytes to commit
nt_memory_permission
Memory protection flags for committed pages
Returns: true if successful, false otherwise.

decommit_memory

Decommits memory, freeing physical backing but retaining the reservation.
uint64_t
Base address to decommit
size_t
Number of bytes to decommit
Returns: true if successful, false otherwise.

release_memory

Releases a memory allocation completely.
uint64_t
Base address of the allocation to release
size_t
Size of the region to release
Returns: true if successful, false otherwise.

unmap_all_memory

Unmaps all allocated memory regions.

Memory Protection

protect_memory

Changes the protection flags for a memory region.
uint64_t
Base address of the region
size_t
Size of the region
nt_memory_permission
New protection flags
nt_memory_permission*
default:"nullptr"
Optional output parameter to receive previous permissions
Returns: true if successful, false otherwise.

Memory-Mapped I/O

allocate_mmio

Allocates a memory-mapped I/O region with custom read/write callbacks.
uint64_t
Virtual address for the MMIO region
size_t
Size of the MMIO region
mmio_read_callback
Callback invoked on reads: void(uint64_t addr, void* data, size_t size)
mmio_write_callback
Callback invoked on writes: void(uint64_t addr, const void* data, size_t size)
Returns: true if allocation succeeded, false otherwise.

Region Queries

get_region_info

Retrieves detailed information about a memory region.
uint64_t
Address within the region to query
Returns: A region_info structure containing:
  • allocation_base: Base address of the allocation
  • allocation_length: Total allocation size
  • is_reserved: Whether the region is reserved
  • is_committed: Whether the region is committed
  • initial_permissions: Original protection flags
  • kind: Type of memory region

find_free_allocation_base

Finds a free address range suitable for allocation.
size_t
Required size for the allocation
uint64_t
default:"0"
Minimum address to start searching from
Returns: Base address for the allocation, or 0 if no space found.

overlaps_reserved_region

Checks if an address range overlaps with any reserved region.
Returns: true if the range overlaps, false otherwise.

get_region_kind

Retrieves the kind of memory region at an address.
Returns: The memory region kind enumeration value.

Address Space Management

get_default_allocation_address

Returns: The default base address for automatic allocations.

set_default_allocation_address

uint64_t
New default allocation address

get_layout_version

Returns: A version number that increments whenever the memory layout changes.

get_reserved_regions

Returns: Map of all reserved memory regions.

Serialization

serialize_memory_state

utils::buffer_serializer&
Output buffer for serialized data
bool
Whether this is a snapshot (affects what gets serialized)

deserialize_memory_state

Statistics

compute_memory_stats

Returns: A memory_stats structure containing:
  • reserved_memory: Total bytes reserved
  • committed_memory: Total bytes committed

Enumerations

memory_region_kind

Usage Example

See Also