- implemented a custom allocator (fixed size block) with a fallback (linked list allocator) for larger block sizes

- apic code still not working (commented out, check lib.rs)
This commit is contained in:
2025-02-28 04:18:35 +00:00
parent e38c20dbd3
commit 8a3e9e3afc
7 changed files with 280 additions and 34 deletions
+20 -4
View File
@@ -1,15 +1,15 @@
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.
pub type FoundryAllocator = LockedHeap;
#[global_allocator]
/// This is now Rust's global allocator, so we can use stuff requiring heap allocations.
static ALLOCATOR: FoundryAllocator = FoundryAllocator::empty();
static ALLOCATOR: Locked<FoundryAllocator> = Locked::new(FoundryAllocator::new());
pub const HEAP_START: usize = 0x4444_4444_0000;
pub const HEAP_SIZE: usize = 1024 * 1024 * 1024;
@@ -19,8 +19,24 @@ 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 as *mut u8, HEAP_SIZE);
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()
}
}
+231
View File
@@ -0,0 +1,231 @@
use alloc::collections::LinkedList;
use core::alloc::{GlobalAlloc, Layout};
use core::{mem, ptr};
use core::ptr::NonNull;
use crate::drivers::kalloc::allocator::Locked;
const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048];
struct ListNode {
next: Option<&'static mut ListNode>,
}
pub struct FoundryAllocator {
list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()],
fallback: Locked<FoundryFallbackAllocator>,
}
impl Default for FoundryAllocator {
fn default() -> Self {
Self::new()
}
}
impl FoundryAllocator {
pub const fn new() -> Self {
const EMPTY: Option<&'static mut ListNode> = None;
Self {
list_heads: [EMPTY; BLOCK_SIZES.len()],
fallback: Locked::new(FoundryFallbackAllocator::new()),
}
}
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) { unsafe {
self.fallback.lock().init(heap_start, heap_size);
}}
unsafe fn fallback_alloc(&mut self, layout: Layout) -> *mut u8 { unsafe {
self.fallback.alloc(layout)
}}
fn block_size(&self, layout: &Layout) -> Option<usize> {
let required_block_size = layout.size().max(layout.align());
BLOCK_SIZES.iter().position(|&s| s >= required_block_size)
}
}
unsafe impl GlobalAlloc for Locked<FoundryAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(index) => {
match allocator.list_heads[index].take() {
Some(node) => {
allocator.list_heads[index] = node.next.take();
node as *mut ListNode as *mut u8
}
None => {
// no block exists in list => allocate new block
let block_size = BLOCK_SIZES[index];
// only works if all block sizes are a power of 2
let block_align = block_size;
let layout = Layout::from_size_align(block_size, block_align)
.unwrap();
unsafe { allocator.fallback_alloc(layout) }
}
}
}
None => unsafe { allocator.fallback_alloc(layout) },
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
match allocator.block_size(&layout) {
Some(idx) => {
let new_node = ListNode {
next: allocator.list_heads[idx].take(),
};
let new_ptr = ptr as *mut ListNode;
unsafe { new_ptr.write(new_node) };
allocator.list_heads[idx] = Some( unsafe { &mut *new_ptr });
}
None => {;
unsafe { allocator.fallback.dealloc(ptr, layout) };
}
}
}
}
struct FallbackListNode {
size: usize,
next: Option<&'static mut FallbackListNode>,
}
impl FallbackListNode {
const fn new(size: usize) -> Self {
Self {
size,
next: None,
}
}
fn start_addr(&self) -> usize {
self as *const Self as usize
}
fn end_addr(&self) -> usize {
self.start_addr() + self.size
}
}
pub struct FoundryFallbackAllocator {
head: FallbackListNode,
}
impl FoundryFallbackAllocator {
pub const fn new() -> Self {
Self {
head: FallbackListNode::new(0),
}
}
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
unsafe { self.add_region(heap_start, heap_size) };
}
unsafe fn add_region(&mut self, addr: usize, size: usize) { unsafe {
let mut node = FallbackListNode::new(size);
node.next = self.head.next.take();
let node_ptr = addr as *mut FallbackListNode;
node_ptr.write(node);
self.head.next = Some(&mut *node_ptr);
}}
fn find_region(&mut self, size: usize, align: usize) -> Option<(&'static mut FallbackListNode, usize)> {
let mut current = &mut self.head;
// look for a large enough memory region in linked list
while let Some(ref mut region) = current.next {
if let Ok(alloc_start) = Self::alloc_from_region(&region, size, align) {
// region suitable for allocation -> remove node from list
let next = region.next.take();
let ret = Some((current.next.take().unwrap(), alloc_start));
current.next = next;
return ret;
} else {
// region not suitable -> continue with next region
current = current.next.as_mut().unwrap();
}
}
None
}
const fn align_up(addr: usize, align: usize) -> usize {
(addr + align - 1) & !(align - 1)
}
fn size_align(layout: Layout) -> (usize, usize) {
let layout = layout
.align_to(align_of::<ListNode>())
.expect("adjusting alignment failed")
.pad_to_align();
let size = layout.size().max(size_of::<ListNode>());
(size, layout.align())
}
fn alloc_from_region(region: &FallbackListNode, size: usize, align: usize) -> Result<usize, ()>
{
let alloc_start = Self::align_up(region.start_addr(), align);
let alloc_end = alloc_start.checked_add(size).ok_or(())?;
if alloc_end > region.end_addr() {
return Err(());
}
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 && excess_size < size_of::<ListNode>() {
return Err(());
}
Ok(alloc_start)
}
}
unsafe impl GlobalAlloc for Locked<FoundryFallbackAllocator> {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let mut allocator = self.lock();
// perform layout adjustments
let (size, align) = FoundryFallbackAllocator::size_align(layout);
if let Some((region, alloc_start)) = allocator.find_region(size, align) {
let alloc_end = alloc_start.checked_add(size).expect("overflow");
let excess_size = region.end_addr() - alloc_end;
if excess_size > 0 {
unsafe { allocator.add_region(alloc_end, excess_size) };
}
alloc_start as *mut u8
} else {
ptr::null_mut()
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
let mut allocator = self.lock();
// perform layout adjustments
let (size, _) = FoundryFallbackAllocator::size_align(layout);
unsafe {
allocator.add_region(ptr as usize, size)
}
}
}
+1
View File
@@ -1 +1,2 @@
pub mod allocator;
mod foundry_kalloc;