pub struct MemoryManager<M: Memory> { /* private fields */ }
Expand description
A memory manager simulates multiple memories within a single memory.
The memory manager can return up to 255 unique instances of VirtualMemory
, and each can be
used independently and can grow up to the bounds of the underlying memory.
By default, the memory manager divides the memory into “buckets” of 128 pages. Each
VirtualMemory
is internally represented as a list of buckets. Buckets of different memories
can be interleaved, but the VirtualMemory
interface gives the illusion of a continuous
address space.
Because a VirtualMemory
is a list of buckets, this implies that internally it grows one
bucket at a time.
The first page of the memory is reserved for the memory manager’s own state. The layout for this state is as follows:
V1 layout
-------------------------------------------------- <- Address 0
Magic "MGR" ↕ 3 bytes
--------------------------------------------------
Layout version ↕ 1 byte
--------------------------------------------------
Number of allocated buckets ↕ 2 bytes
--------------------------------------------------
Bucket size (in pages) = N ↕ 2 bytes
--------------------------------------------------
Reserved space ↕ 32 bytes
--------------------------------------------------
Size of memory 0 (in pages) ↕ 8 bytes
--------------------------------------------------
Size of memory 1 (in pages) ↕ 8 bytes
--------------------------------------------------
...
--------------------------------------------------
Size of memory 254 (in pages) ↕ 8 bytes
-------------------------------------------------- <- Bucket allocations
Bucket 1 ↕ 1 byte (1 byte indicating which memory owns it)
--------------------------------------------------
Bucket 2 ↕ 1 byte
--------------------------------------------------
...
--------------------------------------------------
Bucket `MAX_NUM_BUCKETS` ↕ 1 byte
--------------------------------------------------
Unallocated space ↕ 30'688 bytes
-------------------------------------------------- <- Buckets (Page 1)
Bucket 1 ↕ N pages
-------------------------------------------------- <- Page N + 1
Bucket 2 ↕ N pages
--------------------------------------------------
...
-------------------------------------------------- <- Page ((MAX_NUM_BUCKETS - 1) * N + 1)
Bucket MAX_NUM_BUCKETS ↕ N pages
Implementations§
source§impl<M: Memory> MemoryManager<M>
impl<M: Memory> MemoryManager<M>
sourcepub fn init_with_bucket_size(memory: M, bucket_size_in_pages: u16) -> Self
pub fn init_with_bucket_size(memory: M, bucket_size_in_pages: u16) -> Self
Initializes a MemoryManager
with the given memory and bucket size in pages.
sourcepub fn get(&self, id: MemoryId) -> VirtualMemory<M>
pub fn get(&self, id: MemoryId) -> VirtualMemory<M>
Returns the memory associated with the given ID.