2022-04-11 17:23:11 -05:00
|
|
|
//! The allocator to be implemented by ableOS
|
2022-06-22 13:59:24 -05:00
|
|
|
//!
|
|
|
|
//! NOTE: All memory regions are taken from https://wiki.osdev.org/Memory_Map_(x86)
|
2021-11-27 09:19:08 -06:00
|
|
|
|
2021-11-23 05:53:06 -06:00
|
|
|
use alloc::alloc::{GlobalAlloc, Layout};
|
2022-06-22 13:59:24 -05:00
|
|
|
use core::{fmt::Display, ptr::null_mut};
|
2021-11-23 05:53:06 -06:00
|
|
|
|
2022-06-22 13:59:24 -05:00
|
|
|
const HEAP_START: usize = 600_000_000;
|
|
|
|
const BLOCK_SIZE: usize = 1024;
|
|
|
|
const BLOCK_COUNT: usize = 512;
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct MemoryRegion {
|
2022-08-01 06:50:41 -05:00
|
|
|
_start: usize,
|
|
|
|
_end: usize,
|
2022-06-22 13:59:24 -05:00
|
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct AAlloc {
|
|
|
|
current_region: usize,
|
|
|
|
memory_regions: [Option<MemoryRegion>; 512],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AAlloc {
|
|
|
|
pub fn add_region(&mut self, mem: MemoryRegion) {
|
|
|
|
self.memory_regions[self.current_region] = Some(mem);
|
|
|
|
self.current_region += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn intialize() {
|
|
|
|
info!("Heap Start: {}", HEAP_START);
|
|
|
|
info!("Heap Size: {}", BLOCK_SIZE * BLOCK_COUNT);
|
|
|
|
info!("Heap End: {}", HEAP_START + BLOCK_SIZE * BLOCK_COUNT);
|
|
|
|
|
|
|
|
let mut aalloc = AAlloc {
|
|
|
|
current_region: 0,
|
|
|
|
memory_regions: [None; 512],
|
|
|
|
};
|
|
|
|
|
|
|
|
aalloc.add_region(MemoryRegion {
|
2022-08-01 06:50:41 -05:00
|
|
|
_start: 0x00100000,
|
|
|
|
_end: 0x00EFFFFF,
|
2022-06-22 13:59:24 -05:00
|
|
|
});
|
|
|
|
debug!("{}", aalloc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for AAlloc {
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
|
|
|
write!(f, "AAlloc {{\n\tcurrent_region: {},\n", self.current_region)?;
|
|
|
|
|
|
|
|
for x in 0..self.current_region {
|
|
|
|
if let Some(region) = self.memory_regions[x] {
|
|
|
|
write!(f, "\tRegion {}: {:?}\n", x, region)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write!(f, "}}")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl GlobalAlloc for AAlloc {
|
2021-11-23 05:53:06 -06:00
|
|
|
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
|
2022-03-11 13:51:47 -06:00
|
|
|
println!("Allocating memory");
|
|
|
|
|
|
|
|
println!("{}", _layout.size());
|
|
|
|
println!("{}", _layout.align());
|
|
|
|
|
2021-11-23 05:53:06 -06:00
|
|
|
null_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
|
|
|
|
panic!("dealloc should be never called")
|
|
|
|
}
|
|
|
|
}
|