From e6d93d5743a92a10a6c51e0316abc5ce549cc65a Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 8 Sep 2020 15:51:13 -0600 Subject: [PATCH] Allow const creation of buddy allocator --- src/allocator/frame/buddy.rs | 13 ++++++++++++- src/lib.rs | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/allocator/frame/buddy.rs b/src/allocator/frame/buddy.rs index 7d8fb0e8a6..f8b4f5ffce 100644 --- a/src/allocator/frame/buddy.rs +++ b/src/allocator/frame/buddy.rs @@ -47,6 +47,13 @@ impl BuddyAllocator { const MAP_PAGE_BYTES: usize = (A::PAGE_SIZE - mem::size_of::()); const MAP_PAGE_BITS: usize = Self::MAP_PAGE_BYTES * 8; + pub const unsafe fn empty() -> Self { + Self { + table_virt: VirtualAddress::new(0), + phantom: PhantomData, + } + } + pub unsafe fn new(mut bump_allocator: BumpAllocator) -> Option { // Allocate buddy table let table_phys = bump_allocator.allocate_one()?; @@ -132,7 +139,7 @@ impl BuddyAllocator { impl FrameAllocator for BuddyAllocator { unsafe fn allocate(&mut self, count: FrameCount) -> Option { //TODO: support other sizes - if count.data() != 1 { + if self.table_virt.data() == 0 || count.data() != 1 { return None; } @@ -171,6 +178,10 @@ impl FrameAllocator for BuddyAllocator { } unsafe fn free(&mut self, base: PhysicalAddress, count: FrameCount) { + if self.table_virt.data() == 0 { + return; + } + let size = count.data() * A::PAGE_SIZE; for i in 0 .. Self::BUDDY_ENTRIES { let virt = self.table_virt.add(i * mem::size_of::()); diff --git a/src/lib.rs b/src/lib.rs index e0b4f6de08..a4ae108f9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![feature(asm)] +#![feature(const_fn)] pub use crate::{ allocator::*,