use crate::cell_memory::Memory;
use log::{debug, warn};
use std::collections::HashMap;
#[derive(Clone)]
pub struct Alloc {
cursor: u64,
sizes: HashMap<u64, u64>,
}
impl Alloc {
pub const ALLOC_START: u64 = 0x1000_0000;
pub fn new() -> Self {
Self {
cursor: Self::ALLOC_START,
sizes: HashMap::new(),
}
}
pub fn alloc(&mut self, bits: impl Into<u64>) -> u64 {
let bits: u64 = bits.into();
if bits == 0 {
warn!("An allocation of 0 bits was requested");
}
let bits_in_byte: u64 = Memory::BITS_IN_BYTE.into();
let cell_bytes: u64 = Memory::CELL_BYTES.into();
let bytes = {
let mut bytes = bits / bits_in_byte;
if bits % bits_in_byte != 0 {
bytes += 1; }
bytes
};
let current_offset_bytes = self.cursor % cell_bytes;
let bytes_remaining_in_cell = cell_bytes - current_offset_bytes;
if bytes > bytes_remaining_in_cell {
self.cursor += bytes_remaining_in_cell;
assert_eq!(self.cursor % cell_bytes, 0);
}
let rval = self.cursor;
self.cursor += bytes;
self.sizes.insert(rval, bits);
debug!("Allocated {} bits at 0x{:x}", bits, rval);
rval
}
pub fn get_allocation_size(&self, addr: impl Into<u64>) -> Option<u64> {
self.sizes.get(&addr.into()).copied()
}
}