use crate::map_engine::{
Memory,
types::{Address, Bytes, NULL},
};
const ALLOCATOR_LAYOUT_VERSION: u8 = 1;
const CHUNK_LAYOUT_VERSION: u8 = 1;
const ALLOCATOR_MAGIC: &[u8; 3] = b"BTA"; const CHUNK_MAGIC: &[u8; 3] = b"CHK";
pub struct Allocator<M: Memory> {
header_addr: Address,
allocation_size: Bytes,
num_allocated_chunks: u64,
free_list_head: Address,
memory: M,
}
#[repr(C, packed)]
#[derive(PartialEq, Debug)]
struct AllocatorHeader {
magic: [u8; 3],
version: u8,
_alignment: [u8; 4],
allocation_size: Bytes,
num_allocated_chunks: u64,
free_list_head: Address,
_buffer: [u8; 16],
}
impl AllocatorHeader {
fn size() -> Bytes {
Bytes::from(core::mem::size_of::<Self>() as u64)
}
}
impl<M: Memory> Allocator<M> {
pub fn new(memory: M, addr: Address, allocation_size: Bytes) -> Self {
let mut allocator = Self {
header_addr: addr,
allocation_size,
num_allocated_chunks: 0,
free_list_head: NULL, memory,
};
allocator.clear();
allocator
}
pub fn clear(&mut self) {
self.free_list_head = self.header_addr + AllocatorHeader::size();
let chunk = ChunkHeader::null();
chunk.save(self.free_list_head, &self.memory);
self.num_allocated_chunks = 0;
self.save()
}
pub fn load(memory: M, addr: Address) -> Self {
let header = AllocatorHeader::read(&memory, addr);
assert_eq!(&header.magic, ALLOCATOR_MAGIC, "Bad magic.");
assert_eq!(
header.version, ALLOCATOR_LAYOUT_VERSION,
"Unsupported version."
);
Self {
header_addr: addr,
allocation_size: header.allocation_size,
num_allocated_chunks: header.num_allocated_chunks,
free_list_head: header.free_list_head,
memory,
}
}
pub fn allocate(&mut self) -> Address {
let chunk_addr = self.free_list_head;
let mut chunk = ChunkHeader::load(chunk_addr, &self.memory);
assert!(
!chunk.allocated,
"Attempting to allocate an already allocated chunk."
);
chunk.allocated = true;
chunk.save(chunk_addr, &self.memory);
if chunk.next != NULL {
self.free_list_head = chunk.next;
} else {
self.free_list_head += self.chunk_size();
ChunkHeader::null().save(self.free_list_head, &self.memory);
}
self.num_allocated_chunks += 1;
self.save();
chunk_addr + ChunkHeader::size()
}
pub fn deallocate(&mut self, address: Address) {
let chunk_addr = address - ChunkHeader::size();
let mut chunk = ChunkHeader::load(chunk_addr, &self.memory);
assert!(chunk.allocated);
chunk.allocated = false;
chunk.next = self.free_list_head;
chunk.save(chunk_addr, &self.memory);
self.free_list_head = chunk_addr;
self.num_allocated_chunks -= 1;
self.save();
}
pub fn save(&self) {
let header = AllocatorHeader {
magic: *ALLOCATOR_MAGIC,
version: ALLOCATOR_LAYOUT_VERSION,
_alignment: [0; 4],
num_allocated_chunks: self.num_allocated_chunks,
allocation_size: self.allocation_size,
free_list_head: self.free_list_head,
_buffer: [0; 16],
};
crate::map_engine::write(&self.memory, self.header_addr.get(), &header.encode());
}
fn chunk_size(&self) -> Bytes {
self.allocation_size + ChunkHeader::size()
}
#[inline]
pub fn into_memory(self) -> M {
self.memory
}
#[inline]
pub fn memory(&self) -> &M {
&self.memory
}
}
#[derive(Debug)]
#[repr(C, packed)]
struct ChunkHeader {
magic: [u8; 3],
version: u8,
allocated: bool,
_alignment: [u8; 3],
next: Address,
}
impl ChunkHeader {
fn null() -> Self {
Self {
magic: *CHUNK_MAGIC,
version: CHUNK_LAYOUT_VERSION,
allocated: false,
_alignment: [0; 3],
next: NULL,
}
}
fn save<M: Memory>(&self, address: Address, memory: &M) {
crate::map_engine::write(memory, address.get(), &self.encode());
}
fn load<M: Memory>(address: Address, memory: &M) -> Self {
let header = ChunkHeader::read(memory, address);
assert_eq!(&header.magic, CHUNK_MAGIC, "Bad magic.");
assert_eq!(header.version, CHUNK_LAYOUT_VERSION, "Unsupported version.");
header
}
fn size() -> Bytes {
Bytes::from(core::mem::size_of::<Self>() as u64)
}
}
impl AllocatorHeader {
fn read(memory: &impl Memory, address: Address) -> Self {
let mut bytes = [0; 48];
memory.read(address.get(), &mut bytes);
Self {
magic: bytes[0..3].try_into().unwrap(),
version: bytes[3],
_alignment: bytes[4..8].try_into().unwrap(),
allocation_size: Bytes::from(u64::from_le_bytes(bytes[8..16].try_into().unwrap())),
num_allocated_chunks: u64::from_le_bytes(bytes[16..24].try_into().unwrap()),
free_list_head: Address::from(u64::from_le_bytes(bytes[24..32].try_into().unwrap())),
_buffer: bytes[32..48].try_into().unwrap(),
}
}
fn encode(&self) -> [u8; 48] {
let mut bytes = [0; 48];
bytes[0..3].copy_from_slice(&self.magic);
bytes[3] = self.version;
bytes[4..8].copy_from_slice(&self._alignment);
bytes[8..16].copy_from_slice(&{ self.allocation_size }.get().to_le_bytes());
bytes[16..24].copy_from_slice(&self.num_allocated_chunks.to_le_bytes());
bytes[24..32].copy_from_slice(&self.free_list_head.get().to_le_bytes());
bytes[32..48].copy_from_slice(&self._buffer);
bytes
}
}
impl ChunkHeader {
fn read(memory: &impl Memory, address: Address) -> Self {
let mut bytes = [0; 16];
memory.read(address.get(), &mut bytes);
Self {
magic: bytes[0..3].try_into().unwrap(),
version: bytes[3],
allocated: match bytes[4] {
0 => false,
1 => true,
_ => panic!("invalid chunk allocation flag"),
},
_alignment: bytes[5..8].try_into().unwrap(),
next: Address::from(u64::from_le_bytes(bytes[8..16].try_into().unwrap())),
}
}
fn encode(&self) -> [u8; 16] {
let mut bytes = [0; 16];
bytes[0..3].copy_from_slice(&self.magic);
bytes[3] = self.version;
bytes[4] = self.allocated as u8;
bytes[5..8].copy_from_slice(&self._alignment);
bytes[8..16].copy_from_slice(&self.next.get().to_le_bytes());
bytes
}
}