diff --git a/src/allocator/fixed_size_block.rs b/src/allocator/fixed_size_block.rs index a698e09c..a4c517bb 100644 --- a/src/allocator/fixed_size_block.rs +++ b/src/allocator/fixed_size_block.rs @@ -7,3 +7,27 @@ const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048]; struct ListNode { next: Option<&'static mut ListNode>, } + +pub struct FixedSizeBlockAllocator { + list_heads: [Option<&'static mut ListNode>; BLOCK_SIZES.len()], + fallback_allocator: linked_list_allocator::Heap, +} + +impl FixedSizeBlockAllocator { + /// Creates an empty FixedSizeBlockAllocator. + pub const fn new() -> Self { + FixedSizeBlockAllocator { + list_heads: [None; BLOCK_SIZES.len()], + fallback_allocator: linked_list_allocator::Heap::empty(), + } + } + + /// Initialize the allocator with the given heap bounds. + /// + /// This function is unsafe because the caller must guarantee that the given + /// heap bounds are valid and that the heap is unused. This method must be + /// called only once. + pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) { + self.fallback_allocator.init(heap_start, heap_size); + } +} diff --git a/src/lib.rs b/src/lib.rs index 8b1c46f9..e79038ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ #![feature(alloc_error_handler)] #![feature(const_fn)] #![feature(alloc_layout_extra)] +#![feature(const_in_array_repeat_expressions)] #![test_runner(crate::test_runner)] #![reexport_test_harness_main = "test_main"]