Skip to content

std.os.uefi.pool_allocator

Standard libraryZig 0.17.1Official documentation
Sourcestd/os/uefi/pool_allocator.zig

Source line 1

std.os.uefi.pool_allocator.Allocator

alias

Source line 8

Alias of std.mem.Allocator

std.os.uefi.pool_allocator.assert

alias

Source line 6

Alias of std.debug.assert

std.os.uefi.pool_allocator.mem

alias

Source line 3

Alias of std.mem

std.os.uefi.pool_allocator.pool_allocator

constant

Source line 87

Supports the full Allocator interface, including alignment. For a direct call of allocatePool, see raw_pool_allocator.

std.os.uefi.pool_allocator.pool_allocator_vtable

constant

Source line 92

std.os.uefi.pool_allocator.raw_pool_allocator

constant

Source line 100

Asserts allocations are 8 byte aligned and calls boot_services.allocatePool.

std.os.uefi.pool_allocator.raw_pool_allocator_table

constant

Source line 105

std.os.uefi.pool_allocator.std

alias

Source line 1

Alias of std

std.os.uefi.pool_allocator.uefi

alias

Source line 4

Alias of std.os.uefi

std.os.uefi.pool_allocator.uefi_alloc

function

Source line 112

fn uefi_alloc( _: *anyopaque, len: usize, alignment: mem.Alignment, ret_addr: usize, ) ?[*]u8

Parameters

_: *anyopaque
len: usize
alignment: mem.Alignment
ret_addr: usize

std.os.uefi.pool_allocator.uefi_free

function

Source line 160

fn uefi_free( _: *anyopaque, buf: []u8, alignment: mem.Alignment, ret_addr: usize, ) void

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
ret_addr: usize

std.os.uefi.pool_allocator.uefi_remap

function

Source line 145

fn uefi_remap( _: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize, ) ?[*]u8

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
new_len: usize
ret_addr: usize

std.os.uefi.pool_allocator.uefi_resize

function

Source line 130

fn uefi_resize( _: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize, ) bool

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
new_len: usize
ret_addr: usize

std.os.uefi.pool_allocator.UefiPoolAllocator

namespace

Source line 10

std.os.uefi.pool_allocator.UefiPoolAllocator.alloc

function

Source line 15

fn alloc( _: *anyopaque, len: usize, alignment: mem.Alignment, ret_addr: usize, ) ?[*]u8

Parameters

_: *anyopaque
len: usize
alignment: mem.Alignment
ret_addr: usize

std.os.uefi.pool_allocator.UefiPoolAllocator.free

function

Source line 73

fn free( _: *anyopaque, buf: []u8, alignment: mem.Alignment, ret_addr: usize, ) void

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
ret_addr: usize

std.os.uefi.pool_allocator.UefiPoolAllocator.getHeader

function

Source line 11

fn getHeader(ptr: [*]u8) *[*]align(8) u8

Parameters

ptr: [*]u8

std.os.uefi.pool_allocator.UefiPoolAllocator.remap

function

Source line 59

fn remap( _: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize, ) ?[*]u8

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
new_len: usize
ret_addr: usize

std.os.uefi.pool_allocator.UefiPoolAllocator.resize

function

Source line 45

fn resize( _: *anyopaque, buf: []u8, alignment: mem.Alignment, new_len: usize, ret_addr: usize, ) bool

Parameters

_: *anyopaque
buf: []u8
alignment: mem.Alignment
new_len: usize
ret_addr: usize
Source code

std/os/uefi/pool_allocator.zig

const std = @import("std");

const mem = std.mem;
const uefi = std.os.uefi;

const assert = std.debug.assert;

const Allocator = mem.Allocator;

const UefiPoolAllocator = struct {
    fn getHeader(ptr: [*]u8) *[*]align(8) u8 {
        return @as(*[*]align(8) u8, @ptrFromInt(@intFromPtr(ptr) - @sizeOf(usize)));
    }

    fn alloc(
        _: *anyopaque,
        len: usize,
        alignment: mem.Alignment,
        ret_addr: usize,
    ) ?[*]u8 {
        _ = ret_addr;

        assert(len > 0);

        const ptr_align = alignment.toByteUnits();

        const metadata_len = mem.alignForward(usize, @sizeOf(usize), ptr_align);

        const full_len = metadata_len + len;

        const unaligned_slice = uefi.system_table.boot_services.?.allocatePool(
            uefi.efi_pool_memory_type,
            full_len,
        ) catch return null;

        const unaligned_addr = @intFromPtr(unaligned_slice.ptr);
        const aligned_addr = mem.alignForward(usize, unaligned_addr + @sizeOf(usize), ptr_align);

        const aligned_ptr = unaligned_slice.ptr + (aligned_addr - unaligned_addr);
        getHeader(aligned_ptr).* = unaligned_slice.ptr;

        return aligned_ptr;
    }

    fn resize(
        _: *anyopaque,
        buf: []u8,
        alignment: mem.Alignment,
        new_len: usize,
        ret_addr: usize,
    ) bool {
        _ = ret_addr;
        _ = alignment;

        if (new_len > buf.len) return false;
        return true;
    }

    fn remap(
        _: *anyopaque,
        buf: []u8,
        alignment: mem.Alignment,
        new_len: usize,
        ret_addr: usize,
    ) ?[*]u8 {
        _ = alignment;
        _ = ret_addr;

        if (new_len > buf.len) return null;
        return buf.ptr;
    }

    fn free(
        _: *anyopaque,
        buf: []u8,
        alignment: mem.Alignment,
        ret_addr: usize,
    ) void {
        _ = alignment;
        _ = ret_addr;
        uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*) catch unreachable;
    }
};

/// Supports the full Allocator interface, including alignment.
/// For a direct call of `allocatePool`, see `raw_pool_allocator`.
pub const pool_allocator = Allocator{
    .ptr = undefined,
    .vtable = &pool_allocator_vtable,
};

const pool_allocator_vtable = Allocator.VTable{
    .alloc = UefiPoolAllocator.alloc,
    .resize = UefiPoolAllocator.resize,
    .remap = UefiPoolAllocator.remap,
    .free = UefiPoolAllocator.free,
};

/// Asserts allocations are 8 byte aligned and calls `boot_services.allocatePool`.
pub const raw_pool_allocator = Allocator{
    .ptr = undefined,
    .vtable = &raw_pool_allocator_table,
};

const raw_pool_allocator_table = Allocator.VTable{
    .alloc = uefi_alloc,
    .resize = uefi_resize,
    .remap = uefi_remap,
    .free = uefi_free,
};

fn uefi_alloc(
    _: *anyopaque,
    len: usize,
    alignment: mem.Alignment,
    ret_addr: usize,
) ?[*]u8 {
    _ = ret_addr;

    std.debug.assert(@backingInt(alignment) <= 3);

    const slice = uefi.system_table.boot_services.?.allocatePool(
        uefi.efi_pool_memory_type,
        len,
    ) catch return null;

    return slice.ptr;
}

fn uefi_resize(
    _: *anyopaque,
    buf: []u8,
    alignment: mem.Alignment,
    new_len: usize,
    ret_addr: usize,
) bool {
    _ = ret_addr;

    std.debug.assert(@backingInt(alignment) <= 3);

    if (new_len > buf.len) return false;
    return true;
}

fn uefi_remap(
    _: *anyopaque,
    buf: []u8,
    alignment: mem.Alignment,
    new_len: usize,
    ret_addr: usize,
) ?[*]u8 {
    _ = ret_addr;

    std.debug.assert(@backingInt(alignment) <= 3);

    if (new_len > buf.len) return null;
    return buf.ptr;
}

fn uefi_free(
    _: *anyopaque,
    buf: []u8,
    alignment: mem.Alignment,
    ret_addr: usize,
) void {
    _ = alignment;
    _ = ret_addr;
    uefi.system_table.boot_services.?.freePool(@alignCast(buf.ptr)) catch unreachable;
}