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
Reference to the underlying memory backend
Memory Constants
Core Memory Operations
read_memory
Reads memory from the virtual address space.Virtual address to read from
Destination buffer
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.Virtual address to write to
Source buffer
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.Desired allocation address (must be aligned to 64KB)
Size in bytes to allocate
Initial memory protection flags
If
true, only reserve the address space without committingType of memory region to create
true if allocation succeeded, false otherwise.
allocate_memory (find free address)
Allocates memory at an automatically chosen address.Size in bytes to allocate
Initial memory protection flags
If
true, only reserve the address spaceMinimum address to start searching from
Type of memory region to create
commit_memory
Commits previously reserved memory, making it accessible.Base address to commit
Number of bytes to commit
Memory protection flags for committed pages
true if successful, false otherwise.
decommit_memory
Decommits memory, freeing physical backing but retaining the reservation.Base address to decommit
Number of bytes to decommit
true if successful, false otherwise.
release_memory
Releases a memory allocation completely.Base address of the allocation to release
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.Base address of the region
Size of the region
New protection flags
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.Virtual address for the MMIO region
Size of the MMIO region
Callback invoked on reads:
void(uint64_t addr, void* data, size_t size)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.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.Required size for the allocation
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
New default allocation address
get_layout_version
get_reserved_regions
Serialization
serialize_memory_state
Output buffer for serialized data
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