Skip to content

VecAllocator

Description

Allocator backing the vector’s storage.

Parameters

Name Direction Description
v in Vector to query.

Usage example (Cross-references)

Usage examples (Cross-references)
         .length    = VecLen(&(v)),                                                                                        \
         .pos       = 0,                                                                                                   \
         .alignment = VecAllocator(&(v)) ? AllocatorAlignment(VecAllocator(&(v))) : 1,                                     \
         .dir       = 1}
         .length    = VecLen(&(v)),                                                                                        \
         .pos       = 0,                                                                                                   \
         .alignment = VecAllocator(&(v)) ? AllocatorAlignment(VecAllocator(&(v))) : 1,                                     \
         .dir       = -1}
                       .length    = VecLen(&(v)),                                                                          \
                       .pos       = 0,                                                                                     \
                       .alignment = VecAllocator(&(v)) ? AllocatorAlignment(VecAllocator(&(v))) : 1,                       \
                       .dir       = 1})
                       .length    = VecLen(&(v)),                                                                          \
                       .pos       = 0,                                                                                     \
                       .alignment = VecAllocator(&(v)) ? AllocatorAlignment(VecAllocator(&(v))) : 1,                       \
                       .dir       = -1})
                                   .copy_deinit = VecCopyDeinit(vs),                                                       \
                                   .data        = NULL,                                                                    \
                                   .allocator   = VecAllocator(vs),                                                        \
                                   .__magic     = VEC_MAGIC | MAGIC_VALIDATED_BIT},                                            \
         clone_vec(GENERIC_VEC(vd), GENERIC_VEC(vs), sizeof(VEC_DATATYPE(vd))))
    /// TAGS: Str, Access, Allocator
    ///
    #define StrAllocator(str) VecAllocator(str)
    
    ///
        if (!self)
            return;
        if (VecAllocator(&self->entries))
            VecDeinit(&self->entries);
        if (StrAllocator(&self->string_pool))
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 1 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 4 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 16 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == (GenericCopyInit)TestItemCopyInit &&
             VecCopyDeinit(&vec) == (GenericCopyDeinit)TestItemDeinit);
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 8 &&
             VecCopyInit(&vec) == (GenericCopyInit)TestItemCopyInit &&
             VecCopyDeinit(&vec) == (GenericCopyDeinit)TestItemDeinit);
        TestVec vec_h = VecInitWithDeepCopyT(vec_h, TestItemCopyInit, TestItemDeinit, &h64);
    
        bool result = (VecAllocator(&vec_a)->retry_limit == 17) && (VecAllocator(&vec_b)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_c)->retry_limit == 17) && (VecAllocator(&vec_d)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->retry_limit == 17) && (VecAllocator(&vec_f)->retry_limit == 17);
    
        bool result = (VecAllocator(&vec_a)->retry_limit == 17) && (VecAllocator(&vec_b)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_c)->retry_limit == 17) && (VecAllocator(&vec_d)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->retry_limit == 17) && (VecAllocator(&vec_f)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_g)->retry_limit == 17) && (VecAllocator(&vec_h)->retry_limit == 17);
        bool result = (VecAllocator(&vec_a)->retry_limit == 17) && (VecAllocator(&vec_b)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_c)->retry_limit == 17) && (VecAllocator(&vec_d)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->retry_limit == 17) && (VecAllocator(&vec_f)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_g)->retry_limit == 17) && (VecAllocator(&vec_h)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->alignment == 8) && (VecAllocator(&vec_f)->alignment == 16);
        result      = result && (VecAllocator(&vec_c)->retry_limit == 17) && (VecAllocator(&vec_d)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->retry_limit == 17) && (VecAllocator(&vec_f)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_g)->retry_limit == 17) && (VecAllocator(&vec_h)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->alignment == 8) && (VecAllocator(&vec_f)->alignment == 16);
        result      = result && (VecAllocator(&vec_g)->alignment == 32) && (VecAllocator(&vec_h)->alignment == 64);
        result      = result && (VecAllocator(&vec_e)->retry_limit == 17) && (VecAllocator(&vec_f)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_g)->retry_limit == 17) && (VecAllocator(&vec_h)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->alignment == 8) && (VecAllocator(&vec_f)->alignment == 16);
        result      = result && (VecAllocator(&vec_g)->alignment == 32) && (VecAllocator(&vec_h)->alignment == 64);
        result      = result && (VecCopyInit(&vec_c) == (GenericCopyInit)TestItemCopyInit);
        result      = result && (VecAllocator(&vec_g)->retry_limit == 17) && (VecAllocator(&vec_h)->retry_limit == 17);
        result      = result && (VecAllocator(&vec_e)->alignment == 8) && (VecAllocator(&vec_f)->alignment == 16);
        result      = result && (VecAllocator(&vec_g)->alignment == 32) && (VecAllocator(&vec_h)->alignment == 64);
        result      = result && (VecCopyInit(&vec_c) == (GenericCopyInit)TestItemCopyInit);
        result      = result && (VecCopyDeinit(&vec_d) == (GenericCopyDeinit)TestItemDeinit);
        VecInitStack(int, vec, 10) {
            // Stack-init: NULL allocator distinguishes from heap-init.
            if (VecLen(&vec) != 0 || VecCapacity(&vec) != 10 || VecBegin(&vec) == NULL || VecAllocator(&vec) != NULL) {
                result = false;
            }
        VecInitStack(TestItem, test_vec, 5) {
            if (VecLen(&test_vec) != 0 || VecCapacity(&test_vec) != 5 || VecBegin(&test_vec) == NULL ||
                VecAllocator(&test_vec) != NULL) {
                result = false;
            }
        bool result =
            (VecLen(&clone) == VecLen(&src) && VecCapacity(&clone) >= VecLen(&src) && VecBegin(&clone) != VecBegin(&src) &&
             VecAllocator(&clone)->alignment == VecAllocator(&src)->alignment);
    
        // Check the actual data
        // int[]. Stronger alignment is exercised separately - it's not what
        // this test is asserting.
        IntVec dst = VecInit(VecAllocator(&src));
        // intentional bypass: testing hook propagation; no public VecSetCopyHooks mutator exists
        dst.copy_init   = src.copy_init;
        bool cloned     = VecPushBackArrR(&dst, VecBegin(&src), VecLen(&src));
    
        bool allocator_matches = VecAllocator(&dst) == VecAllocator(&src);
    
        bool result = cloned && VecCopyInit(&dst) == VecCopyInit(&src) && VecCopyDeinit(&dst) == VecCopyDeinit(&src) &&
    
        bool result = cloned && VecCopyInit(&dst) == VecCopyInit(&src) && VecCopyDeinit(&dst) == VecCopyDeinit(&src) &&
                      VecAllocator(&dst)->effort == ALLOCATOR_EFFORT_RETRY_FALLBACK &&
                      VecAllocator(&dst)->retry_limit == 11 && allocator_matches && VecLen(&src) == 3 &&
                      VecAt(&src, 0) == 10 && VecAt(&src, 1) == 20 && VecAt(&src, 2) == 30 && VecLen(&dst) == 3 &&
        bool result = cloned && VecCopyInit(&dst) == VecCopyInit(&src) && VecCopyDeinit(&dst) == VecCopyDeinit(&src) &&
                      VecAllocator(&dst)->effort == ALLOCATOR_EFFORT_RETRY_FALLBACK &&
                      VecAllocator(&dst)->retry_limit == 11 && allocator_matches && VecLen(&src) == 3 &&
                      VecAt(&src, 0) == 10 && VecAt(&src, 1) == 20 && VecAt(&src, 2) == 30 && VecLen(&dst) == 3 &&
                      VecAt(&dst, 0) == 10 && VecAt(&dst, 1) == 20 && VecAt(&dst, 2) == 30;
        // Check initial state
        bool result =
            (VecLen(&vec) == 0 && VecCapacity(&vec) == 0 && VecBegin(&vec) == NULL && VecAllocator(&vec)->alignment == 1 &&
             VecCopyInit(&vec) == NULL && VecCopyDeinit(&vec) == NULL);
        // Check initial state
        result = result && (VecLen(&test_vec) == 0 && VecCapacity(&test_vec) == 0 && VecBegin(&test_vec) == NULL &&
                            VecAllocator(&test_vec)->alignment == 1 && VecCopyInit(&test_vec) == NULL &&
                            VecCopyDeinit(&test_vec) == NULL);
Last updated on