Struct axalloc::GlobalAllocator
source · 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
impl GlobalAllocator
sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an empty GlobalAllocator
.
sourcepub fn init(&self, start_vaddr: usize, size: usize)
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.
sourcepub fn add_memory(&self, start_vaddr: usize, size: usize) -> AllocResult
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.
sourcepub fn alloc(&self, layout: Layout) -> AllocResult<NonNull<u8>>
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.
sourcepub fn alloc_pages(
&self,
num_pages: usize,
align_pow2: usize
) -> AllocResult<usize>
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.
sourcepub fn dealloc_pages(&self, pos: usize, num_pages: usize)
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.
sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Returns the number of allocated bytes in the byte allocator.
sourcepub fn available_bytes(&self) -> usize
pub fn available_bytes(&self) -> usize
Returns the number of available bytes in the byte allocator.
sourcepub fn used_pages(&self) -> usize
pub fn used_pages(&self) -> usize
Returns the number of allocated pages in the page allocator.
sourcepub fn available_pages(&self) -> usize
pub fn available_pages(&self) -> usize
Returns the number of available pages in the page allocator.
Trait Implementations§
source§impl GlobalAlloc for GlobalAllocator
impl GlobalAlloc for GlobalAllocator
source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout
. Read more