int constbuffer_array_ptr_copy()

in src/async_type_helper.c [153:199]


int constbuffer_array_ptr_copy(constbuffer_array_ptr* dest, const constbuffer_array_ptr src, uint32_t item_count)
{
    int result;

    if (
        /*Codes_SRS_ASYNC_TYPE_HELPER_28_001: [ If dest is NULL then the copy handler will fail and return a non-zero value. ]*/
        dest == NULL ||
        /*Codes_SRS_ASYNC_TYPE_HELPER_28_011: [ If item_count is 0, the copy handler will fail and return a non-zero value. ]*/
        item_count == 0)
    {
        LogError("Invalid argument: CONSTBUFFER_ARRAY_HANDLE** dest=%p, const CONSTBUFFER_ARRAY_HANDLE* src=%p, uin32_t item_count=%" PRIu32 "",
            dest, src, item_count);
        result = MU_FAILURE;
    }
    else if (src == NULL)
    {
        /*Codes_SRS_ASYNC_TYPE_HELPER_28_002: [ If src is NULL then the copy handler will set the dest to NULL and return 0. ]*/
        *dest = NULL;
        result = 0;
    }
    else
    {
        /*Codes_SRS_ASYNC_TYPE_HELPER_28_003: [ The copy handler shall allocate an array to store all the constbuffer_array in the src. ]*/
        *dest = malloc_2(item_count, sizeof(CONSTBUFFER_ARRAY_HANDLE));

        if (*dest == NULL)
        {
            /*Codes_SRS_ASYNC_TYPE_HELPER_28_004: [ If there are any failures then the copy handler shall fail and return a non-zero value. ]*/
            LogError("malloc_2(item_count=%" PRIu32 ", sizeof(CONSTBUFFER_ARRAY_HANDLE)=%zu) failed", item_count, sizeof(CONSTBUFFER_ARRAY_HANDLE));
            result = MU_FAILURE;
        }
        else
        {
            for (uint32_t i = 0; i < item_count; i++)
            {
                /*Codes_SRS_ASYNC_TYPE_HELPER_28_005: [ The copy handler shall call constbuffer_array_inc_ref on each constbuffer array in src. ]*/
                constbuffer_array_inc_ref(src[i]);
                /*Codes_SRS_ASYNC_TYPE_HELPER_28_006: [ The copy handler shall copy all the constbuffer_arrays from the src to the dest. ]*/
                (*dest)[i] = src[i];
            }
            /*Codes_SRS_ASYNC_TYPE_HELPER_28_007: [ The copy handler shall succeed and return 0. ]*/
            result = 0;
        }
    }

    return result;
}