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
try_read_memory
Attempts to read memory without throwing exceptions.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
try_write_memory
Attempts to write memory without throwing exceptions.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 committingmemory_region_kind
default:"private_allocation"
Type of memory region to create
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 spaceuint64_t
default:"0"
Minimum address to start searching from
memory_region_kind
default:"private_allocation"
Type of memory region to create
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
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
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
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
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)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
region_info structure containing:
allocation_base: Base address of the allocationallocation_length: Total allocation sizeis_reserved: Whether the region is reservedis_committed: Whether the region is committedinitial_permissions: Original protection flagskind: 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
overlaps_reserved_region
Checks if an address range overlaps with any reserved region.true if the range overlaps, false otherwise.
get_region_kind
Retrieves the kind of memory region at an address.Address Space Management
get_default_allocation_address
set_default_allocation_address
uint64_t
New default allocation address
get_layout_version
get_reserved_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
memory_stats structure containing:
reserved_memory: Total bytes reservedcommitted_memory: Total bytes committed
Enumerations
memory_region_kind
Usage Example
See Also
- memory_interface - Low-level memory backend interface
- process_context - Process memory structures
- windows_emulator - Main emulator using memory_manager