buddy_slab_allocator/
lib.rs1#![no_std]
12#![feature(extern_item_impls)]
13
14mod error;
15pub use error::{AllocError, AllocResult};
16
17pub mod buddy;
18pub use buddy::{BuddyAllocator, ManagedSection};
19
20pub mod slab;
21pub use slab::{
22 PerCpuSlab, SizeClass, SlabAllocResult, SlabAllocator, SlabDeallocResult,
23 SlabPoolDeallocResult, SlabPoolTrait, SlabTrait, StaticSlabPool,
24};
25
26pub mod global;
27#[doc(hidden)]
28pub use global::__reset_global_allocator_singleton_for_tests;
29pub use global::GlobalAllocator;
30
31pub mod eii {
33 #[eii(virt_to_phys_impl)]
35 pub fn virt_to_phys(vaddr: usize) -> usize;
36
37 #[eii(slab_pool_impl)]
39 pub fn slab_pool() -> &'static dyn crate::SlabPoolTrait;
40}
41
42#[inline]
47pub(crate) const fn align_up(pos: usize, align: usize) -> usize {
48 (pos + align - 1) & !(align - 1)
49}
50
51#[inline]
52pub(crate) const fn is_aligned(addr: usize, align: usize) -> bool {
53 addr & (align - 1) == 0
54}
55
56#[cfg(test)]
57mod test_eii_impls {
58 use core::{alloc::Layout, ptr::NonNull};
59
60 use super::{
61 AllocError, AllocResult, SizeClass, SlabAllocResult, SlabPoolTrait, SlabTrait,
62 eii::{slab_pool_impl, virt_to_phys_impl},
63 };
64
65 struct NullSlabPool;
66 struct NullSlab;
67
68 impl SlabTrait for NullSlab {
69 fn cpu_id(&self) -> usize {
70 0
71 }
72
73 fn page_size(&self) -> usize {
74 0x1000
75 }
76
77 fn alloc(&self, _layout: Layout) -> AllocResult<SlabAllocResult> {
78 Err(AllocError::NotInitialized)
79 }
80
81 fn add_slab(&self, _size_class: SizeClass, _base: usize, _bytes: usize) {}
82
83 fn dealloc_local(&self, _ptr: NonNull<u8>, _layout: Layout) -> super::SlabDeallocResult {
84 super::SlabDeallocResult::Done
85 }
86 }
87
88 static NULL_SLAB: NullSlab = NullSlab;
89
90 impl SlabPoolTrait for NullSlabPool {
91 fn current_slab(&self) -> &dyn SlabTrait {
92 &NULL_SLAB
93 }
94
95 fn owner_slab(&self, _cpu_idx: usize) -> &dyn SlabTrait {
96 &NULL_SLAB
97 }
98 }
99
100 static NULL_SLAB_POOL: NullSlabPool = NullSlabPool;
101
102 #[virt_to_phys_impl]
103 fn test_virt_to_phys(vaddr: usize) -> usize {
104 vaddr
105 }
106
107 #[slab_pool_impl]
108 fn test_slab_pool() -> &'static dyn SlabPoolTrait {
109 &NULL_SLAB_POOL
110 }
111}