mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-17 06:47:49 +00:00
Add FixedSizeBlockAllocator type
This commit is contained in:
@@ -7,3 +7,27 @@ const BLOCK_SIZES: &[usize] = &[8, 16, 32, 64, 128, 256, 512, 1024, 2048];
|
|||||||
struct ListNode {
|
struct ListNode {
|
||||||
next: Option<&'static mut 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#![feature(alloc_error_handler)]
|
#![feature(alloc_error_handler)]
|
||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
#![feature(alloc_layout_extra)]
|
#![feature(alloc_layout_extra)]
|
||||||
|
#![feature(const_in_array_repeat_expressions)]
|
||||||
#![test_runner(crate::test_runner)]
|
#![test_runner(crate::test_runner)]
|
||||||
#![reexport_test_harness_main = "test_main"]
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user