Skip to content

Commit

Permalink
a basic linked list allocator implementation without free-block merging
Browse files Browse the repository at this point in the history
  • Loading branch information
Blindspot22 committed Aug 7, 2024
1 parent 5955b56 commit 1c65628
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use x86_64::{
VirtAddr,
};
use alloc::alloc::{GlobalAlloc, Layout};
use bump::BumpAllocator;
use linked_list::LinkedListAllocator;
use core::ptr::null_mut;

pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB

#[global_allocator]
static ALLOCATOR: Locked<BumpAllocator> = Locked::new(BumpAllocator::new());
static ALLOCATOR: Locked<LinkedListAllocator> =
Locked::new(LinkedListAllocator::new());
/// A wrapper around spin::Mutex to permit trait implementations.
pub struct Locked<A> {
inner: spin::Mutex<A>,
Expand Down
9 changes: 9 additions & 0 deletions src/allocator/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ impl LinkedListAllocator {
// region suitable for allocation
Ok(alloc_start)
}

fn size_align(layout: Layout) -> (usize, usize) {
let layout = layout
.align_to(mem::align_of::<ListNode>())
.expect("adjusting alignment failed")
.pad_to_align();
let size = layout.size().max(mem::size_of::<ListNode>());
(size, layout.align())
}
}
struct ListNode {
size: usize,
Expand Down

0 comments on commit 1c65628

Please sign in to comment.