buddy_slab_allocator/lib.rs
1//! # buddy-slab-allocator
2//!
3//! A `#![no_std]` memory allocator featuring:
4//!
5//! - **Buddy page allocator** — page-metadata-based with intrusive free lists
6//! - **Slab allocator** — bitmap-based with lock-free cross-CPU freeing (Linux SLUB inspired)
7//! - **Global allocator** — composes buddy + per-CPU slab, implements [`core::alloc::GlobalAlloc`]
8//!
9//! Both buddy and slab allocators can be used standalone.
10
11#![no_std]
12
13mod error;
14pub use error::{AllocError, AllocResult};
15
16pub mod buddy;
17pub use buddy::{BuddyAllocator, ManagedSection};
18
19pub mod slab;
20pub use slab::{SizeClass, SlabAllocResult, SlabAllocator, SlabDeallocResult};
21
22pub mod global;
23pub use global::GlobalAllocator;
24
25// ---------------------------------------------------------------------------
26// OsImpl trait — the only interface the allocator needs from the OS / platform
27// ---------------------------------------------------------------------------
28
29/// Platform abstraction required by [`GlobalAllocator`].
30///
31/// Implementations must be safe to call from any CPU at any time.
32pub trait OsImpl: Sync + Send {
33 /// Return the index of the current CPU (0-based).
34 fn current_cpu_idx(&self) -> usize;
35
36 /// Translate a virtual address to a physical address.
37 fn virt_to_phys(&self, vaddr: usize) -> usize;
38}
39
40// ---------------------------------------------------------------------------
41// Utility helpers (crate-internal)
42// ---------------------------------------------------------------------------
43
44#[inline]
45pub(crate) const fn align_up(pos: usize, align: usize) -> usize {
46 (pos + align - 1) & !(align - 1)
47}
48
49#[inline]
50pub(crate) const fn is_aligned(addr: usize, align: usize) -> bool {
51 addr & (align - 1) == 0
52}