Skip to content
HeapAllocatorInit

HeapAllocatorInit

Description

Initialize a HeapAllocator with default alignment (1, meaning “no stronger than the backing page allocator’s natural alignment”). Use as a designated-initializer:

HeapAllocator heap = HeapAllocatorInit(); Vec(int) v = VecInit(&heap); … VecDeinit(&v); HeapAllocatorDeinit(&heap);

Usage example (Cross-references)

Usage examples (Cross-references)
    
    static bool test_basic_alloc_free(void) {
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
    
    static bool test_zeroed_alloc(void) {
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
    
    static bool test_free_then_alloc_recycles(void) {
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
    static bool test_large_alloc_passthrough(void) {
        // > 2 KiB hits the page-allocator passthrough path inside Heap.
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
    
    static bool test_realloc_same_bin_keeps_pointer(void) {
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
    
    static bool test_realloc_cross_bin_copies(void) {
        HeapAllocator heap  = HeapAllocatorInit();
        Allocator    *alloc = ALLOCATOR_OF(&heap);
        // Two HeapAllocators on the same stack frame must not share any
        // state — the library never owns global heap bookkeeping.
        HeapAllocator h1     = HeapAllocatorInit();
        HeapAllocator h2     = HeapAllocatorInit();
        Allocator    *alloc1 = ALLOCATOR_OF(&h1);
        // state — the library never owns global heap bookkeeping.
        HeapAllocator h1     = HeapAllocatorInit();
        HeapAllocator h2     = HeapAllocatorInit();
        Allocator    *alloc1 = ALLOCATOR_OF(&h1);
        Allocator    *alloc2 = ALLOCATOR_OF(&h2);
        // that the pointer-based allocator stored in the vector reflects each
        // allocator's settings.
        HeapAllocator h_default = HeapAllocatorInit();
        HeapAllocator h8        = HeapAllocatorInitAligned(8);
        HeapAllocator h16       = HeapAllocatorInitAligned(16);
        typedef Vec(int) IntVec;
    
        HeapAllocator local_heap    = HeapAllocatorInit();
        local_heap.base.effort      = ALLOCATOR_EFFORT_RETRY_FALLBACK;
        local_heap.base.retry_limit = 11;
    #define LOG_FATAL(...)                                                                                                 \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #define LOG_ERROR(...)                                                                                                 \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #define LOG_INFO(...)                                                                                                  \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #define LOG_SYS_FATAL(...)                                                                                             \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #define LOG_SYS_ERROR(...)                                                                                             \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #define LOG_SYS_INFO(...)                                                                                              \
        do {                                                                                                               \
            HeapAllocator log_alloc_ = HeapAllocatorInit();                                                                \
            Str           m_         = StrInit(&log_alloc_);                                                               \
            StrWriteFmt(&m_, __VA_ARGS__);                                                                                 \
    #endif
    
    #define DefaultAllocatorInit()      HeapAllocatorInit()
    #define DefaultAllocatorDeinit(ptr) HeapAllocatorDeinit(ptr)
Last updated on