pub struct GlobalAllocator<const PAGE_SIZE: usize = { crate::DEFAULT_PAGE_SIZE }> { /* private fields */ }Expand description
Global allocator that coordinates composite and slab allocators
Implementations§
Source§impl<const PAGE_SIZE: usize> GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> GlobalAllocator<PAGE_SIZE>
pub const fn new() -> Self
Sourcepub fn set_addr_translator(&mut self, translator: &'static dyn AddrTranslator)
pub fn set_addr_translator(&mut self, translator: &'static dyn AddrTranslator)
Set the address translator so that the underlying page allocator can reason about physical address ranges (e.g. low-memory regions below 4GiB).
Sourcepub fn alloc_dma32_pages(
&mut self,
num_pages: usize,
alignment: usize,
) -> AllocResult<usize>
pub fn alloc_dma32_pages( &mut self, num_pages: usize, alignment: usize, ) -> AllocResult<usize>
Allocate low-memory pages (physical address < 4GiB). This is a thin wrapper over the composite allocator’s lowmem API.
Sourcepub fn init(&mut self, start_vaddr: usize, size: usize) -> AllocResult<()>
pub fn init(&mut self, start_vaddr: usize, size: usize) -> AllocResult<()>
Initialize allocator with given memory region
§Examples
use buddy_slab_allocator::GlobalAllocator;
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
allocator.init(0x8000_0000, 16 * 1024 * 1024).unwrap();Sourcepub fn add_memory(&mut self, start_vaddr: usize, size: usize) -> AllocResult<()>
pub fn add_memory(&mut self, start_vaddr: usize, size: usize) -> AllocResult<()>
Dynamically add memory region to allocator
Sourcepub fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>>
pub fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>>
Smart allocation based on size
Small allocations (≤2048 bytes) use slab allocator, larger allocations use page allocator.
§Examples
use buddy_slab_allocator::GlobalAllocator;
use core::alloc::Layout;
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
allocator.init(0x8000_0000, 16 * 1024 * 1024).unwrap();
let layout = Layout::from_size_align(64, 8).unwrap();
let ptr = allocator.alloc(layout).unwrap();
allocator.dealloc(ptr, layout);Sourcepub fn alloc_pages(
&mut self,
num_pages: usize,
alignment: usize,
) -> AllocResult<usize>
pub fn alloc_pages( &mut self, num_pages: usize, alignment: usize, ) -> AllocResult<usize>
Allocate pages
§Examples
use buddy_slab_allocator::{GlobalAllocator, PageAllocator};
const PAGE_SIZE: usize = 0x1000;
let mut allocator = GlobalAllocator::<PAGE_SIZE>::new();
allocator.init(0x8000_0000, 16 * 1024 * 1024).unwrap();
let addr = allocator.alloc_pages(4, PAGE_SIZE).unwrap();
allocator.dealloc_pages(addr, 4);Sourcepub fn dealloc_pages(&mut self, pos: usize, num_pages: usize)
pub fn dealloc_pages(&mut self, pos: usize, num_pages: usize)
Deallocate pages
Trait Implementations§
Source§impl<const PAGE_SIZE: usize> BaseAllocator for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> BaseAllocator for GlobalAllocator<PAGE_SIZE>
Source§impl<const PAGE_SIZE: usize> Default for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> Default for GlobalAllocator<PAGE_SIZE>
Source§impl<const PAGE_SIZE: usize> PageAllocator for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> PageAllocator for GlobalAllocator<PAGE_SIZE>
Source§const PAGE_SIZE: usize = PAGE_SIZE
const PAGE_SIZE: usize = PAGE_SIZE
The size of a memory page in bytes (must be a power of two).
Source§fn alloc_pages(
&mut self,
num_pages: usize,
alignment: usize,
) -> AllocResult<usize>
fn alloc_pages( &mut self, num_pages: usize, alignment: usize, ) -> AllocResult<usize>
Allocate contiguous memory pages with given count and alignment (in bytes). Read more
Source§fn dealloc_pages(&mut self, pos: usize, num_pages: usize)
fn dealloc_pages(&mut self, pos: usize, num_pages: usize)
Deallocate contiguous memory pages with given position and count. Read more
Source§fn alloc_pages_at(
&mut self,
base: usize,
num_pages: usize,
alignment: usize,
) -> AllocResult<usize>
fn alloc_pages_at( &mut self, base: usize, num_pages: usize, alignment: usize, ) -> AllocResult<usize>
Allocate contiguous memory pages with given base address, count and alignment (in bytes). Read more
Source§fn total_pages(&self) -> usize
fn total_pages(&self) -> usize
Returns the total number of memory pages managed by this allocator.
Source§fn used_pages(&self) -> usize
fn used_pages(&self) -> usize
Returns the number of allocated memory pages.
Source§fn available_pages(&self) -> usize
fn available_pages(&self) -> usize
Returns the number of available memory pages.
Auto Trait Implementations§
impl<const PAGE_SIZE: usize = { crate::DEFAULT_PAGE_SIZE }> !Freeze for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize = { crate::DEFAULT_PAGE_SIZE }> !RefUnwindSafe for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> Send for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> Sync for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize> Unpin for GlobalAllocator<PAGE_SIZE>
impl<const PAGE_SIZE: usize = { crate::DEFAULT_PAGE_SIZE }> !UnwindSafe for GlobalAllocator<PAGE_SIZE>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more