Skip to content

PoolAllocator

Description

Create a pool allocator that hands out fixed-size slots. slot_size is recorded on the descriptor and verified on every allocation request. The pool grows by mapping fresh page-backed chunks through PageAllocator when the free list is empty.

Parameters

Name Direction Description
slot_size in Size of each allocation in bytes. Must be > 0. Sizes above UINT32_MAX are clamped down.

Usage example (from documentation)

  Allocator node_pool = PoolAllocator(sizeof(MyNode));
  List(MyNode) list = ListInit(node_pool);

Success

Returns a pool allocator descriptor.

Failure

Function cannot fail at creation. Out-of-memory shows up as a NULL from the first AllocatorAlloc call.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    Allocator PoolAllocator(size slot_size) {
        return PoolAllocatorAligned(slot_size, 1);
    }
    
    static bool test_basic_alloc_and_free(void) {
        Allocator pool = PoolAllocator(sizeof(Node));
        Node     *a    = (Node *)AllocatorAlloc(&pool, sizeof(Node), true);
        Node     *b    = (Node *)AllocatorAlloc(&pool, sizeof(Node), true);
    
    static bool test_free_then_alloc_recycles(void) {
        Allocator pool = PoolAllocator(sizeof(Node));
        Node     *a    = (Node *)AllocatorAlloc(&pool, sizeof(Node), true);
        bool      ok   = (a != NULL);
    
    static bool test_grow_across_chunks(void) {
        Allocator pool = PoolAllocator(sizeof(Node));
        Node     *slots[600];
        bool      ok = true;
    
    static bool test_oversized_request_fails(void) {
        Allocator pool = PoolAllocator(sizeof(int));
        void     *big  = AllocatorAlloc(&pool, 4096, true);
        bool      ok   = (big == NULL);
    
    static bool test_free_half_then_realloc(void) {
        Allocator pool = PoolAllocator(sizeof(Node));
        Node     *slots[200];
        bool      ok = true;
Last updated on