pub struct GlobalAllocator { /* private fields */ }
Expand description

The global allocator used by ArceOS.

It combines a ByteAllocator and a PageAllocator into a simple two-level allocator: firstly tries allocate from the byte allocator, if there is no memory, asks the page allocator for more memory and adds it to the byte allocator.

Currently, TlsfByteAllocator is used as the byte allocator, while BitmapPageAllocator is used as the page allocator.

Implementations§

source§

impl GlobalAllocator

source

pub const fn new() -> Self

Creates an empty GlobalAllocator.

source

pub const fn name(&self) -> &'static str

Returns the name of the allocator.

source

pub fn init(&self, start_vaddr: usize, size: usize)

Initializes the allocator with the given region.

It firstly adds the whole region to the page allocator, then allocates a small region (32 KB) to initialize the byte allocator. Therefore, the given region must be larger than 32 KB.

source

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

Add the given region to the allocator.

It will add the whole region to the byte allocator.

source

pub fn alloc(&self, layout: Layout) -> AllocResult<NonNull<u8>>

Allocate arbitrary number of bytes. Returns the left bound of the allocated region.

It firstly tries to allocate from the byte allocator. If there is no memory, it asks the page allocator for more memory and adds it to the byte allocator.

align_pow2 must be a power of 2, and the returned region bound will be aligned to it.

source

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

Gives back the allocated region to the byte allocator.

The region should be allocated by alloc, and align_pow2 should be the same as the one used in alloc. Otherwise, the behavior is undefined.

source

pub fn alloc_pages( &self, num_pages: usize, align_pow2: usize ) -> AllocResult<usize>

Allocates contiguous pages.

It allocates num_pages pages from the page allocator.

align_pow2 must be a power of 2, and the returned region bound will be aligned to it.

source

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

Gives back the allocated pages starts from pos to the page allocator.

The pages should be allocated by alloc_pages, and align_pow2 should be the same as the one used in alloc_pages. Otherwise, the behavior is undefined.

source

pub fn used_bytes(&self) -> usize

Returns the number of allocated bytes in the byte allocator.

source

pub fn available_bytes(&self) -> usize

Returns the number of available bytes in the byte allocator.

source

pub fn used_pages(&self) -> usize

Returns the number of allocated pages in the page allocator.

source

pub fn available_pages(&self) -> usize

Returns the number of available pages in the page allocator.

Trait Implementations§

source§

impl GlobalAlloc for GlobalAllocator

source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocate memory as described by the given layout. Read more
source§

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Deallocate the block of memory at the given ptr pointer with the given layout. Read more
1.28.0 · source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more
1.28.0 · source§

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

Shrink or grow a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more

Auto Trait Implementations§

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

§

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

§

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.