Skip to content
ArenaAllocatorInit

ArenaAllocatorInit

Description

Construct an ArenaAllocator with default alignment (1). Use as a designated-initializer. The arena starts empty: no chunks, no rollback snapshot. The first allocation triggers a kernel page map for the initial chunk.

ArenaAllocator a = ArenaAllocatorInit(); void *p = AllocatorAlloc(&a, 128, false); ArenaAllocatorDeinit(&a);

Success

Returns a fully-initialised ArenaAllocator value. No OS calls are made; the first alloc triggers the lazy page-map for the initial chunk.

Failure

Cannot fail at macro-expansion time.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    void bench_init(void) {
        g_arena      = ArenaAllocatorInit();
        g_arena_live = true;
    }
        // page-backed chunk handles a typical response with zero per-free
        // bookkeeping.
        ArenaAllocator scratch = ArenaAllocatorInit();
    
        DnsWireBuf query = VecInitT(query, &scratch);
    
    static bool test_basic_bump(void) {
        ArenaAllocator arena      = ArenaAllocatorInit();
        Allocator     *alloc_base = ALLOCATOR_OF(&arena);
        u8            *a          = (u8 *)AllocatorAlloc(alloc_base, 16, true);
    
    static bool test_grow_last_in_place(void) {
        ArenaAllocator arena      = ArenaAllocatorInit();
        Allocator     *alloc_base = ALLOCATOR_OF(&arena);
        u8            *p          = (u8 *)AllocatorAlloc(alloc_base, 16, true);
        // sizes (that's the whole point), so remap of a non-last pointer
        // is a caller bug and aborts via LOG_FATAL.
        ArenaAllocator arena      = ArenaAllocatorInit();
        Allocator     *alloc_base = ALLOCATOR_OF(&arena);
        u8            *a          = (u8 *)AllocatorAlloc(alloc_base, 16, true);
    static bool test_reject_foreign_free(void) {
        // Free of a pointer not in any arena chunk is a caller bug.
        ArenaAllocator arena      = ArenaAllocatorInit();
        Allocator     *alloc_base = ALLOCATOR_OF(&arena);
        char           stack_byte = 0;
    
    static bool test_vec_on_arena(void) {
        ArenaAllocator arena = ArenaAllocatorInit();
        typedef Vec(int) IntVec;
        IntVec v  = VecInit(&arena);
    
    static bool test_reset(void) {
        ArenaAllocator arena      = ArenaAllocatorInit();
        Allocator     *alloc_base = ALLOCATOR_OF(&arena);
        u8            *a          = (u8 *)AllocatorAlloc(alloc_base, 4096, true);
Last updated on