summaryrefslogtreecommitdiffstats
path: root/library/std/src/sys/uefi/alloc.rs
blob: 789e3cbd81ade351d52c13210af2a67731e88de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Global Allocator for UEFI.
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc)

use crate::alloc::{GlobalAlloc, Layout, System};

const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        // Return null pointer if boot services are not available
        if crate::os::uefi::env::boot_services().is_none() {
            return crate::ptr::null_mut();
        }

        // If boot services is valid then SystemTable is not null.
        let system_table = crate::os::uefi::env::system_table().as_ptr().cast();
        // The caller must ensure non-0 layout
        unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        // Do nothing if boot services are not available
        if crate::os::uefi::env::boot_services().is_none() {
            return;
        }

        // If boot services is valid then SystemTable is not null.
        let system_table = crate::os::uefi::env::system_table().as_ptr().cast();
        // The caller must ensure non-0 layout
        unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) }
    }
}