slab_allocator

Struct Heap

Source
pub struct Heap {
    pub(crate) slabs: [Slab; 7],
    pub(crate) fallback: Heap,
    pub(crate) user: usize,
    pub(crate) allocated: usize,
    pub(crate) total: usize,
}
Expand description

堆内存分配器

Fields§

§slabs: [Slab; 7]

Slab 分配器数组

§fallback: Heap

Fallback Linked List Allocator

§user: usize

用户请求的字节数

§allocated: usize

实际分配的字节数

§total: usize

堆中的总字节数

Implementations§

Source§

impl Heap

Source

pub const fn new() -> Self

创建一个空的堆内存分配器

Source

pub fn empty() -> Self

创建一个空的堆内存分配器

Source

pub(crate) fn get_inner(&self, index: usize) -> AllocType

根据索引获取分配器类型

§参数
  • index: 分配器索引
§返回

返回对应的分配器类型,如果索引小于 ALLOCATORS_NUM - 1,则返回 Slab 分配器, 否则返回 Fallback 分配器

Source

pub unsafe fn add_to_heap(&mut self, start: usize, end: usize)

添加一段内存[start, end)到堆中

由于 Linked List Allocator 需要一个连续的内存块, 所以不会在此被扩展

Source

pub(crate) unsafe fn init_fallback(&mut self, start: usize, size: usize)

初始化 Fallback Allocator (Linked List Allocator)

只能被调用一次

Source

pub unsafe fn extend_fallback(&mut self, size: usize)

扩展 Fallback Allocator (Linked List Allocator) 的内存

仅扩展 Linked List Allocator 之后的内存,需要确保 Linked List Allocator 已经初始化 并且内存空间 [start + original_size, start + original_size + size) 未被使用

Source

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

初始化堆内存分配器

§参数
  • start: 内存起始地址
  • size: 内存大小
§说明

将内存分配给 Slab 分配器和 Fallback 分配器

Source

pub(crate) fn select_allocator(&self, layout: &Layout) -> AllocType

根据内存布局选择合适的分配器

§参数
  • layout: 内存布局
§返回

返回对应的分配器类型,如果大小小于等于最大 Slab 大小,则返回 Slab 分配器, 否则返回 Fallback 分配器

Source

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

从堆中分配一段满足 layout 要求的内存

§参数
  • layout: 内存布局
§返回

返回一个指向分配内存的指针,如果分配失败,则返回错误

Source

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

从堆中释放一段内存

Source

pub fn stats_alloc_user(&self) -> usize

返回用户请求的字节数

Source

pub fn stats_alloc_actual(&self) -> usize

返回实际分配的字节数

Source

pub fn stats_total_bytes(&self) -> usize

返回堆中的总字节数

Trait Implementations§

Source§

impl Debug for Heap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

用于格式化输出 Heap 结构体的信息

Auto Trait Implementations§

§

impl Freeze for Heap

§

impl RefUnwindSafe for Heap

§

impl Send for Heap

§

impl !Sync for Heap

§

impl Unpin for Heap

§

impl !UnwindSafe for Heap

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.