void clds_singly_linked_list_destroy()

in src/clds_singly_linked_list.c [280:306]


void clds_singly_linked_list_destroy(CLDS_SINGLY_LINKED_LIST_HANDLE clds_singly_linked_list)
{
    if (clds_singly_linked_list == NULL)
    {
        /* Codes_SRS_CLDS_SINGLY_LINKED_LIST_01_005: [ If clds_singly_linked_list is NULL, clds_singly_linked_list_destroy shall return. ]*/
        LogError("Invalid arguments: CLDS_SINGLY_LINKED_LIST_HANDLE clds_singly_linked_list=%p", clds_singly_linked_list);
    }
    else
    {
        CLDS_SINGLY_LINKED_LIST_ITEM* current_item = interlocked_compare_exchange_pointer((void* volatile_atomic*)&clds_singly_linked_list->head, NULL, NULL);

        /* Codes_SRS_CLDS_SINGLY_LINKED_LIST_01_039: [ Any items still present in the list shall be freed. ]*/
        // go through all the items and free them
        while (current_item != NULL)
        {
            CLDS_SINGLY_LINKED_LIST_ITEM* next_item = interlocked_compare_exchange_pointer((void* volatile_atomic*)&current_item->next, NULL, NULL);

            /* Codes_SRS_CLDS_SINGLY_LINKED_LIST_01_040: [ For each item that is freed, the callback item_cleanup_callback passed to clds_singly_linked_list_node_create shall be called, while passing item_cleanup_callback_context and the freed item as arguments. ]*/
            /* Codes_SRS_CLDS_SINGLY_LINKED_LIST_01_041: [ If item_cleanup_callback is NULL, no user callback shall be triggered for the freed items. ]*/
            internal_node_destroy(current_item);
            current_item = next_item;
        } 

        /* Codes_SRS_CLDS_SINGLY_LINKED_LIST_01_004: [ clds_singly_linked_list_destroy shall free all resources associated with the singly linked list instance. ]*/
        free(clds_singly_linked_list);
    }
}