#![no_std]
#![feature(extern_item_impls)]
mod error;
pub use error::{AllocError, AllocResult};
pub mod buddy;
pub use buddy::{BuddyAllocator, ManagedSection};
pub mod slab;
pub use slab::{
PerCpuSlab, SizeClass, SlabAllocResult, SlabAllocator, SlabDeallocResult,
SlabPoolDeallocResult, SlabPoolTrait, SlabTrait, StaticSlabPool,
};
pub mod global;
#[doc(hidden)]
pub use global::__reset_global_allocator_singleton_for_tests;
pub use global::GlobalAllocator;
pub mod eii {
#[eii(virt_to_phys_impl)]
pub fn virt_to_phys(vaddr: usize) -> usize;
#[eii(slab_pool_impl)]
pub fn slab_pool() -> &'static dyn crate::SlabPoolTrait;
}
#[inline]
pub(crate) const fn align_up(pos: usize, align: usize) -> usize {
(pos + align - 1) & !(align - 1)
}
#[inline]
pub(crate) const fn is_aligned(addr: usize, align: usize) -> bool {
addr & (align - 1) == 0
}
#[cfg(test)]
mod test_eii_impls {
use core::{alloc::Layout, ptr::NonNull};
use super::eii::{slab_pool_impl, virt_to_phys_impl};
use super::{AllocError, AllocResult, SizeClass, SlabAllocResult, SlabPoolTrait, SlabTrait};
struct NullSlabPool;
struct NullSlab;
impl SlabTrait for NullSlab {
fn cpu_id(&self) -> usize {
0
}
fn page_size(&self) -> usize {
0x1000
}
fn alloc(&self, _layout: Layout) -> AllocResult<SlabAllocResult> {
Err(AllocError::NotInitialized)
}
fn add_slab(&self, _size_class: SizeClass, _base: usize, _bytes: usize) {}
fn dealloc_local(&self, _ptr: NonNull<u8>, _layout: Layout) -> super::SlabDeallocResult {
super::SlabDeallocResult::Done
}
}
static NULL_SLAB: NullSlab = NullSlab;
impl SlabPoolTrait for NullSlabPool {
fn current_slab(&self) -> &dyn SlabTrait {
&NULL_SLAB
}
fn owner_slab(&self, _cpu_idx: usize) -> &dyn SlabTrait {
&NULL_SLAB
}
}
static NULL_SLAB_POOL: NullSlabPool = NullSlabPool;
#[virt_to_phys_impl]
fn test_virt_to_phys(vaddr: usize) -> usize {
vaddr
}
#[slab_pool_impl]
fn test_slab_pool() -> &'static dyn SlabPoolTrait {
&NULL_SLAB_POOL
}
}