Module global

Module global 

Source
Expand description

Global Allocator Bridge

This module provides a bridge between the global-allocator interface of the rust standard library and the allocators of this crate. The stabilized interface of the rust compiler and standard-library to the global allocator is provided by the core::alloc::GlobalAlloc trait and the global_allocator attribute. The types provided by this module implement this trait and can be used to register a global allocator.

Only one crate in every dependency graph can use the global_allocator attribute to mark one static variable as the global allocator of the entire application. The type of it must implement GlobalAlloc. Note that this attribute can only be used in the crate-root, not in sub-modules.

UEFI is, however, not a natural fit for the global-allocator trait. On UEFI systems, access to all system APIs is done through the system table, which is passed as argument to the application entry-point. Therefore, it is up to the implementor of the entry-point to set up the global state inherent to rust’s global allocator.

§Examples

The following UEFI application simply registers an allocator with its system-table and then invokes uefi_run(). The latter can then operate under the assumption that an allocator is available and ready. Once the function returns, the allocator is automatically torn down.

This is a typical use of the r-efi-alloc crate. Only applications that actually exit the boot-services, or access UEFI outside of regular UEFI application and driver environments will have to use the custom allocator interfaces.

#![no_main]
#![no_std]

use r_efi::efi;
use r_efi_alloc::{alloc::Allocator, global::Bridge};

#[global_allocator]
static GLOBAL_ALLOCATOR: Bridge = Bridge::new();

#[no_mangle]
pub extern "C" fn efi_main(
    h: efi::Handle,
    st: *mut efi::SystemTable,
) -> efi::Status {
    unsafe {
        let mut allocator = Allocator::from_system_table(st, efi::LOADER_DATA);
        let _attachment = GLOBAL_ALLOCATOR.attach(&mut allocator);

        efi_run(h, st)
    }
}

pub fn efi_run(h: efi::Handle, st: *mut efi::SystemTable) -> efi::Status {
    ...
}

Structs§

Attachment
Bridge Attachment
Bridge
Bridge for Global Allocators