Skip to main content

Module allocator

Module allocator 

Source
Expand description

§XWOS RUST:全局内存分配器

Rust的 #![no_std] 环境要求用户定义 global_allocator ,作为动态内存管理的实现。

§允许动态内存的情况

若用户需要使用基于动态内存的特性,例如 Box<T>Arc<T> , 需要在应用代码中定义 GLOBAL_ALLOCATOR 并赋值为 XwrustAllocator

#![no_std]
use xwrust::xwmm::allocator::XwrustAllocator;

#[global_allocator]
pub static GLOBAL_ALLOCATOR: XwrustAllocator = XwrustAllocator;

#[no_mangle]
pub unsafe extern "C" fn xwrust_main() {
    // 用户代码
}

§禁止动态内存的情况

若用户禁止在代码中使用动态内存,只使用静态内存, 需要在应用代码中定义 GLOBAL_ALLOCATOR 并赋值为 DummyAllocator

use xwrust::xwmm::allocator::DummyAllocator;

#[global_allocator]
pub static GLOBAL_ALLOCATOR: DummyAllocator = DummyAllocator;

#[no_mangle]
pub unsafe extern "C" fn xwrust_main() {
    // 用户代码
}

在禁止使用动态内存管理的场合下,下列模块不可以使用:

Structs§

DummyAllocator
虚假的内存分配器
XwrustAllocator
内存分配器