Allow const creation of buddy allocator

This commit is contained in:
Jeremy Soller
2020-09-08 15:51:13 -06:00
parent 5990a04e13
commit e6d93d5743
2 changed files with 13 additions and 1 deletions
+12 -1
View File
@@ -47,6 +47,13 @@ impl<A: Arch> BuddyAllocator<A> {
const MAP_PAGE_BYTES: usize = (A::PAGE_SIZE - mem::size_of::<BuddyMapFooter>());
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<A>) -> Option<Self> {
// Allocate buddy table
let table_phys = bump_allocator.allocate_one()?;
@@ -132,7 +139,7 @@ impl<A: Arch> BuddyAllocator<A> {
impl<A: Arch> FrameAllocator for BuddyAllocator<A> {
unsafe fn allocate(&mut self, count: FrameCount) -> Option<PhysicalAddress> {
//TODO: support other sizes
if count.data() != 1 {
if self.table_virt.data() == 0 || count.data() != 1 {
return None;
}
@@ -171,6 +178,10 @@ impl<A: Arch> FrameAllocator for BuddyAllocator<A> {
}
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::<BuddyEntry>());
+1
View File
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(asm)]
#![feature(const_fn)]
pub use crate::{
allocator::*,