Files
FoundryOS/libk/src/drivers/kalloc/allocator.rs
T
2025-02-28 04:18:35 +00:00

42 lines
1.1 KiB
Rust

use linked_list_allocator::LockedHeap;
use spin::{Mutex, MutexGuard};
use x86_64::structures::paging::{
mapper::MapToError,
Size4KiB,
};
use crate::drivers::kalloc::foundry_kalloc::FoundryAllocator;
/// We are currently using a linked list heap allocator which uses our underlying page allocator.
#[global_allocator]
/// This is now Rust's global allocator, so we can use stuff requiring heap allocations.
static ALLOCATOR: Locked<FoundryAllocator> = Locked::new(FoundryAllocator::new());
pub const HEAP_START: usize = 0x4444_4444_0000;
pub const HEAP_SIZE: usize = 1024 * 1024 * 1024;
/// Sets up the heap using the backing page frame allocator.
pub fn init_heap() -> Result<(), MapToError<Size4KiB>> {
// code to allocate frames is now done in the page fault interrupt handler!
unsafe {
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
}
Ok(())
}
pub struct Locked<T> {
inner: Mutex<T>,
}
impl<T> Locked<T> {
pub const fn new(inner: T) -> Self {
Locked {
inner: Mutex::new(inner)
}
}
pub fn lock(&self) -> MutexGuard<T> {
self.inner.lock()
}
}