Skip to content
HeapAllocatorAligned

HeapAllocatorAligned

Description

Create a heap-backed allocator descriptor with a custom alignment. All allocations issued through the returned descriptor are aligned to alignment bytes. alignment must be a power of two; passing 0 falls back to the default _Alignof(max_align_t).

Parameters

Name Direction Description
alignment in Required alignment in bytes (power of two).

Usage example (from documentation)

  Allocator simd_alloc = HeapAllocatorAligned(32);
  Vec(SimdVec) v = VecInit(simd_alloc);

Success

Returns a heap-backed allocator descriptor with the requested alignment.

Failure

Function cannot fail (non-pow2 alignment surfaces as an allocation failure at use-time).

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    Allocator HeapAllocatorAligned(size alignment) {
        Allocator alloc = HeapAllocator();
        if (alignment) {
        // Test with a vector with alignment > 1
        typedef Vec(int) AlignedIntVec;
        AlignedIntVec aligned_vec = VecInit(HeapAllocatorAligned(8));
    
        // Add some data
        // Create a vector with 8-byte alignment
        typedef Vec(int) AlignedIntVec;
        AlignedIntVec aligned_vec = VecInit(HeapAllocatorAligned(8));
    
        // For 8-byte alignment, each int (4 bytes) should be padded to 8 bytes
        // Test with int type and 4-byte alignment
        typedef Vec(int) IntVec;
        IntVec vec = VecInit(HeapAllocatorAligned(4));
    
        // Check initial state
        // Test with struct type and 16-byte alignment
        typedef Vec(TestItem) TestVec;
        TestVec test_vec = VecInit(HeapAllocatorAligned(16));
    
        // Check initial state
        // Test with struct type, custom copy/deinit functions, and 8-byte alignment
        typedef Vec(TestItem) TestVec;
        TestVec vec = VecInitWithDeepCopy(TestItemCopyInit, TestItemDeinit, HeapAllocatorAligned(8));
    
        // Check initial state
    
        typedef Graph(int) IntGraph;
        IntGraph graph = GraphInit(HeapAllocatorAligned(32));
    
        GraphNodeId node_id = GraphAddNodeR(&graph, 11);
Last updated on