Skip to main content

GlobalAllocator

Struct GlobalAllocator 

Source
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>

Source

pub const fn new() -> Self

Source

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).

Source

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.

Source

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();
Source

pub fn add_memory(&mut self, start_vaddr: usize, size: usize) -> AllocResult<()>

Dynamically add memory region to allocator

Source

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);
Source

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);
Source

pub fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout)

Deallocate memory

Source

pub fn dealloc_pages(&mut self, pos: usize, num_pages: usize)

Deallocate pages

Source

pub fn realloc( &mut self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8

Reallocate memory

Trait Implementations§

Source§

impl<const PAGE_SIZE: usize> BaseAllocator for GlobalAllocator<PAGE_SIZE>

Source§

fn init(&mut self, start: usize, size: usize)

Initialize the allocator with a free memory region. Read more
Source§

fn add_memory(&mut self, start: usize, size: usize) -> AllocResult

Add a free memory region to the allocator. Read more
Source§

impl<const PAGE_SIZE: usize> Default for GlobalAllocator<PAGE_SIZE>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const PAGE_SIZE: usize> PageAllocator for GlobalAllocator<PAGE_SIZE>

Source§

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>

Allocate contiguous memory pages with given count and alignment (in bytes). Read more
Source§

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>

Allocate contiguous memory pages with given base address, count and alignment (in bytes). Read more
Source§

fn total_pages(&self) -> usize

Returns the total number of memory pages managed by this allocator.
Source§

fn used_pages(&self) -> usize

Returns the number of allocated memory pages.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.