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.
The memory manager divides the memory into “buckets” of 1024 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 time (1024 pages). This implication makes the memory manager ideal for a small number
of memories storing large amounts of data, as opposed to a large number of memories storing
small amounts of data.
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