int umockcallpairs_track_create_paired_call()

in src/umockcallpairs.c [14:106]


int umockcallpairs_track_create_paired_call(PAIRED_HANDLES* paired_handles, const void* handle, const char* handle_type, size_t handle_type_size)
{
    PAIRED_HANDLE* new_paired_handles;
    int result;

    if ((paired_handles == NULL) ||
        (handle == NULL) ||
        (handle_type == NULL))
    {
        /* Codes_SRS_UMOCKCALLPAIRS_01_004: [ If any of the arguments paired_handles, handle or handle_type is NULL, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
        result = __LINE__;
        UMOCK_LOG("umock_track_create_destroy_paired_calls_malloc: NULL paired_handles");
    }
    else
    {
        /* Codes_SRS_UMOCKCALLPAIRS_01_001: [ umockcallpairs_track_create_paired_call shall add a new entry to the PAIRED_HANDLES array and on success it shall return 0. ]*/
        new_paired_handles = (PAIRED_HANDLE*)umockalloc_realloc(paired_handles->paired_handles, sizeof(PAIRED_HANDLE) * (paired_handles->paired_handle_count + 1));
        if (new_paired_handles == NULL)
        {
            result = __LINE__;
            UMOCK_LOG("umock_track_create_destroy_paired_calls_malloc: Allocation failed");
        }
        else
        {
            paired_handles->paired_handle_count++;
            paired_handles->paired_handles = new_paired_handles;

            /* Codes_SRS_UMOCKCALLPAIRS_01_003: [ umockcallpairs_track_create_paired_call shall allocate a memory block and store a pointer to it in the memory field of the new entry. ]*/
            paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_value = umockalloc_malloc(handle_type_size);
            if (paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_value == NULL)
            {
                /* Codes_SRS_UMOCKCALLPAIRS_01_005: [ If allocating memory fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
                paired_handles->paired_handle_count--;
                if (paired_handles->paired_handle_count == 0)
                {
                    umockalloc_free(paired_handles->paired_handles);
                    paired_handles->paired_handles = NULL;
                }

                result = __LINE__;
                UMOCK_LOG("umock_track_create_destroy_paired_calls_malloc: Failed allocating memory for handle value for create");
            }
            else
            {
                size_t handle_type_length = strlen(handle_type);

                paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_type = (char*)umockalloc_malloc(handle_type_length + 1);
                if (paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_type == NULL)
                {
                    /* Codes_SRS_UMOCKCALLPAIRS_01_005: [ If allocating memory fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
                    umockalloc_free(paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_value);
                    paired_handles->paired_handle_count--;
                    if (paired_handles->paired_handle_count == 0)
                    {
                        umockalloc_free(paired_handles->paired_handles);
                        paired_handles->paired_handles = NULL;
                    }

                    result = __LINE__;
                    UMOCK_LOG("umock_track_create_destroy_paired_calls_malloc: Failed allocating memory for handle type for create");
                }
                else
                {
                    (void)memcpy(paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_type, handle_type, handle_type_length + 1);

                    /* Codes_SRS_UMOCKCALLPAIRS_01_002: [ umockcallpairs_track_create_paired_call shall copy the handle to the handle_value member of the new entry. ] */
                    /* Codes_SRS_UMOCKCALLPAIRS_01_006: [ The handle value shall be copied by using umocktypes_copy. ]*/
                    if (umocktypes_copy(handle_type, paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_value, handle) != 0)
                    {
                        /* Codes_SRS_UMOCKCALLPAIRS_01_007: [ If umocktypes_copy fails, umockcallpairs_track_create_paired_call shall fail and return a non-zero value. ]*/
                        umockalloc_free(paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_type);
                        umockalloc_free(paired_handles->paired_handles[paired_handles->paired_handle_count - 1].handle_value);
                        paired_handles->paired_handle_count--;
                        if (paired_handles->paired_handle_count == 0)
                        {
                            umockalloc_free(paired_handles->paired_handles);
                            paired_handles->paired_handles = NULL;
                        }

                        result = __LINE__;
                        UMOCK_LOG("umock_track_create_destroy_paired_calls_malloc: Failed copying handle");
                    }
                    else
                    {
                        result = 0;
                    }
                }
            }
        }
    }

    return result;
}