int TARRAY_LL_ENSURE_CAPACITY()

in inc/c_util/tarray_ll.h [182:235]


int TARRAY_LL_ENSURE_CAPACITY(C)(TARRAY_LL(T) tarray, uint32_t capacity)                                                                                        \
{                                                                                                                                                               \
    int result;                                                                                                                                                 \
    if (tarray == NULL)                                                                                                                                         \
    {                                                                                                                                                           \
        /*Codes_SRS_TARRAY_02_005: [ If tarray is NULL then TARRAY_ENSURE_CAPACITY(T) shall fail and return a non-zero value. ]*/                               \
        LogError("invalid argument TARRAY(" MU_TOSTRING(T) ") tarray=%p, uint32_t capacity=%" PRIu32 "",                                                        \
            tarray, capacity);                                                                                                                                  \
        result = MU_FAILURE;                                                                                                                                    \
    }                                                                                                                                                           \
    else                                                                                                                                                        \
    {                                                                                                                                                           \
        /*Codes_SRS_TARRAY_02_006: [ If tarray->capacity is greater than or equal to capacity then TARRAY_ENSURE_CAPACITY(T) shall succeed and return 0. ]*/    \
        if (capacity <= tarray->capacity)                                                                                                                       \
        {                                                                                                                                                       \
            /*we already have the capacity*/                                                                                                                    \
            result = 0;                                                                                                                                         \
        }                                                                                                                                                       \
        else                                                                                                                                                    \
        {                                                                                                                                                       \
            /*Codes_SRS_TARRAY_02_010: [ If capacity is greater than 2147483648 then TARRAY_ENSURE_CAPACITY(T) shall fail and return a non-zero value. ]*/      \
            if(capacity > (((uint32_t)1)<<31))                                                                                                                  \
            {                                                                                                                                                   \
                /*Codes_SRS_TARRAY_02_009: [ If there are any failures then TARRAY_ENSURE_CAPACITY(T) shall fail and return a non-zero value. ]*/               \
                LogError("capacity (%" PRIu32 ") cannot be improved by doubling", capacity);                                                                    \
                result = MU_FAILURE;                                                                                                                            \
            }                                                                                                                                                   \
            else                                                                                                                                                \
            {                                                                                                                                                   \
                /*Codes_SRS_TARRAY_02_007: [ TARRAY_ENSURE_CAPACITY(T) shall shall call realloc_2 to resize arr to the next multiple of 2 greater than or equal to capacity. ]*/       \
                uint32_t new_capacity = tarray->capacity;                                                                                                       \
                do{                                                                                                                                             \
                    new_capacity *= 2;                                                                                                                          \
                }while(new_capacity<capacity);                                                                                                                  \
                T* temp = realloc_2((void*)tarray->arr, new_capacity, sizeof(T));                                                                               \
                if (temp == NULL)                                                                                                                               \
                {                                                                                                                                               \
                    /*Codes_SRS_TARRAY_02_009: [ If there are any failures then TARRAY_ENSURE_CAPACITY(T) shall fail and return a non-zero value. ]*/           \
                    LogError("failure in realloc_2(tarray->arr=%p, new_capacity=%" PRIu32 ", sizeof(T)=%zu)",                                                   \
                        tarray->arr, new_capacity, sizeof(T));                                                                                                  \
                    result = MU_FAILURE;                                                                                                                        \
                }                                                                                                                                               \
                else                                                                                                                                            \
                {                                                                                                                                               \
                    /*Codes_SRS_TARRAY_02_008: [ TARRAY_ENSURE_CAPACITY(T) shall succeed, record the new computed capacity, and return 0. ]*/                   \
                    ((TARRAY_TYPEDEF_NAME(T)*)tarray)->capacity = new_capacity;                                                                                 \
                    ((TARRAY_TYPEDEF_NAME(T)*)tarray)->arr = temp;                                                                                              \
                    result = 0;                                                                                                                                 \
                }                                                                                                                                               \
            }                                                                                                                                                   \
        }                                                                                                                                                       \
    }                                                                                                                                                           \
    return result;                                                                                                                                              \
}