CLDS_SORTED_LIST_HANDLE clds_sorted_list_create()

in src/clds_sorted_list.c [638:695]


CLDS_SORTED_LIST_HANDLE clds_sorted_list_create(CLDS_HAZARD_POINTERS_HANDLE clds_hazard_pointers, SORTED_LIST_GET_ITEM_KEY_CB get_item_key_cb, void* get_item_key_cb_context, SORTED_LIST_KEY_COMPARE_CB key_compare_cb, void* key_compare_cb_context, volatile_atomic int64_t* start_sequence_number, SORTED_LIST_SKIPPED_SEQ_NO_CB skipped_seq_no_cb, void* skipped_seq_no_cb_context)
{
    CLDS_SORTED_LIST_HANDLE clds_sorted_list;

    /* Codes_SRS_CLDS_SORTED_LIST_01_049: [ get_item_key_cb_context shall be allowed to be NULL. ]*/
    /* Codes_SRS_CLDS_SORTED_LIST_01_050: [ key_compare_cb_context shall be allowed to be NULL. ]*/
    /* Codes_SRS_CLDS_SORTED_LIST_01_076: [ skipped_seq_no_cb shall be allowed to be NULL. ]*/
    /* Codes_SRS_CLDS_SORTED_LIST_01_077: [ skipped_seq_no_cb_context shall be allowed to be NULL. ]*/

    /* Codes_SRS_CLDS_SORTED_LIST_01_059: [ start_sequence_number shall be allowed to be NULL, in which case no order shall be provided for the operations. ]*/

    if (
        /* Codes_SRS_CLDS_SORTED_LIST_01_003: [ If clds_hazard_pointers is NULL, clds_sorted_list_create shall fail and return NULL. ]*/
        (clds_hazard_pointers == NULL) ||
        /* Codes_SRS_CLDS_SORTED_LIST_01_045: [ If get_item_key_cb is NULL, clds_sorted_list_create shall fail and return NULL. ]*/
        (get_item_key_cb == NULL) ||
        /* Codes_SRS_CLDS_SORTED_LIST_01_046: [ If key_compare_cb is NULL, clds_sorted_list_create shall fail and return NULL. ]*/
        (key_compare_cb == NULL) ||
        /* Codes_SRS_CLDS_SORTED_LIST_01_078: [ If start_sequence_number is NULL, then skipped_seq_no_cb must also be NULL, otherwise clds_sorted_list_create shall fail and return NULL. ]*/
        ((start_sequence_number == NULL) && (skipped_seq_no_cb != NULL))
        )
    {
        LogError("Invalid arguments: CLDS_HAZARD_POINTERS_HANDLE clds_hazard_pointers=%p, SORTED_LIST_GET_ITEM_KEY_CB get_item_key_cb=%p, void* get_item_key_cb_context=%p, SORTED_LIST_KEY_COMPARE_CB key_compare_cb=%p, void* key_compare_cb_context=%p, volatile_atomic int64_t* start_sequence_number=%p, SORTED_LIST_SKIPPED_SEQ_NO_CB skipped_seq_no_cb=%p, void* skipped_seq_no_cb_context=%p",
            clds_hazard_pointers, get_item_key_cb, get_item_key_cb_context, key_compare_cb, key_compare_cb_context, start_sequence_number, skipped_seq_no_cb, skipped_seq_no_cb_context);
        clds_sorted_list = NULL;
    }
    else
    {
        /* Codes_SRS_CLDS_SORTED_LIST_01_001: [ clds_sorted_list_create shall create a new sorted list object and on success it shall return a non-NULL handle to the newly created list. ]*/
        clds_sorted_list = malloc(sizeof(CLDS_SORTED_LIST));
        if (clds_sorted_list == NULL)
        {
            /* Codes_SRS_CLDS_SORTED_LIST_01_002: [ If any error happens, clds_sorted_list_create shall fail and return NULL. ]*/
            LogError("malloc failed");
        }
        else
        {
            // all ok
            clds_sorted_list->clds_hazard_pointers = clds_hazard_pointers;
            clds_sorted_list->get_item_key_cb = get_item_key_cb;
            clds_sorted_list->get_item_key_cb_context = get_item_key_cb_context;
            clds_sorted_list->key_compare_cb = key_compare_cb;
            clds_sorted_list->key_compare_cb_context = key_compare_cb_context;
            clds_sorted_list->skipped_seq_no_cb = skipped_seq_no_cb;
            clds_sorted_list->skipped_seq_no_cb_context = skipped_seq_no_cb_context;

            (void)interlocked_exchange(&clds_sorted_list->locked_for_write, 0);
            (void)interlocked_exchange(&clds_sorted_list->pending_write_operations, 0);

            /* Codes_SRS_CLDS_SORTED_LIST_01_058: [ start_sequence_number shall be used by the sorted list to compute the sequence number of each operation. ]*/
            clds_sorted_list->sequence_number = start_sequence_number;

            (void)interlocked_exchange_pointer((void* volatile_atomic*)&clds_sorted_list->head, NULL);
        }
    }

    return clds_sorted_list;
}