Skip to content

PageAllocator

Description

Create an allocator descriptor backed by direct OS page mapping. All allocations are rounded up to a multiple of the system page size and zeroed by the kernel before being handed back. No libc heap functions are invoked.

Success

Returns a page-backed allocator descriptor.

Failure

Function cannot fail.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    static bool pool_state_init(Allocator *alloc) {
        Allocator  page = PageAllocator();
        PoolState *state;
        size       slot_size = (size)alloc->flags;
    }
    
    Allocator PageAllocator(void) {
        return (Allocator) {
            .state        = NULL,
    
    Allocator PageAllocatorAligned(size alignment) {
        Allocator alloc = PageAllocator();
        if (alignment) {
            alloc.alignment = alignment;
    
    static bool arena_state_init(Allocator *alloc) {
        Allocator   page = PageAllocator();
        ArenaState *state;
        char       *result;
    
        (void)zeroed; // Pages from PageAllocator are zero-initialized.
    
        if (!bytes) {
    static void heap_lazy_init(void) {
        if (!g_heap.initialized) {
            g_heap.page = PageAllocator();
            for (u32 i = 0; i < HEAP_NUM_BINS; i++) {
                g_heap.bins[i] = NULL;
    
    static bool test_basic_alloc_and_free(void) {
        Allocator alloc = PageAllocator();
        void     *ptr   = AllocatorAlloc(&alloc, 128, true);
        bool      ok    = (ptr != NULL);
    
    static bool test_realloc_grow_then_shrink(void) {
        Allocator alloc = PageAllocator();
        size      page  = PageAllocatorPageSize();
        char     *ptr   = (char *)AllocatorAlloc(&alloc, 64, true);
    static bool test_vec_with_page_allocator(void) {
        typedef Vec(int) IntVec;
        IntVec v  = VecInit(PageAllocator());
        bool   ok = true;
Last updated on