static bool ProcessList()

in src/cdi/rx_reorder_packets.c [433:532]


static bool ProcessList(CdiProtocolHandle protocol_handle, CdiPoolHandle reorder_entries_pool_handle,
                        CdiPoolHandle payload_sgl_entry_pool_handle, int* num_bytes_added_ptr,
                        CdiReorderList** cur_reorder_list_ptr, const CdiSgList* new_sglist_ptr, int sequence_num,
                        int initial_offset)
{
    bool ret = true;
    CdiReorderList* new_reorder_list_ptr = NULL;
    CdiReorderList* tmp_reorder_list_ptr = *cur_reorder_list_ptr;
    CdiReorderList* tmp_reorder_list_ptr_prev = NULL;

    // Search to find where to attach list.
    while (tmp_reorder_list_ptr) {
#ifdef DEBUG_RX_REORDER_ERROR
        // This should never happen.
        if (sequence_num >= tmp_reorder_list_ptr->top_sequence_num &&
            sequence_num <= tmp_reorder_list_ptr->bot_sequence_num) {
            CDI_LOG_THREAD(kLogWarning, "Sequence number[%d] has already been received! Skipping.", sequence_num);
            ret = false;
            break;
        }
#endif
        if (sequence_num > tmp_reorder_list_ptr->bot_sequence_num) {
            tmp_reorder_list_ptr_prev = tmp_reorder_list_ptr;
            tmp_reorder_list_ptr = tmp_reorder_list_ptr->next_ptr;
        } else {
            break;
        }
    }
    // if the very first one we got is < than the bot_sequence_num, that means it either belongs
    // at the top of the list or to the left
    if (NULL == tmp_reorder_list_ptr_prev) {
        if (sequence_num == tmp_reorder_list_ptr->top_sequence_num-1) {
            if (AddSgListToRxReorderListTop(protocol_handle, payload_sgl_entry_pool_handle, tmp_reorder_list_ptr,
                                            new_sglist_ptr, sequence_num, initial_offset, num_bytes_added_ptr)) {
            } else {
                ret = false;
            }
        } else {
            // List didn't belong on top of existing list, insert to the left and set
            // the payload_state pointer to this new list
            if (CreateAndInsertRxReorderList(protocol_handle, reorder_entries_pool_handle,
                                             payload_sgl_entry_pool_handle, new_sglist_ptr, sequence_num,
                                             initial_offset, num_bytes_added_ptr, NULL, tmp_reorder_list_ptr,
                                             &new_reorder_list_ptr)) {
                *cur_reorder_list_ptr = new_reorder_list_ptr;
            } else {
                ret = false;
            }
        }
    } else {
        // Does this belong on the bottom of existing list?
        if (sequence_num == tmp_reorder_list_ptr_prev->bot_sequence_num+1) {
            if (AddSgListToRxReorderListBottom(protocol_handle, payload_sgl_entry_pool_handle,
                                               tmp_reorder_list_ptr_prev, new_sglist_ptr, sequence_num, initial_offset,
                                               num_bytes_added_ptr)) {
                // Check to see if next list needs attaching because something has been added to the end of this one.
                if (tmp_reorder_list_ptr) {
                    if (tmp_reorder_list_ptr->top_sequence_num == sequence_num+1) {
                        // Attach top of next list to bottom of this list.
                        AttachNextRxReorderList(reorder_entries_pool_handle, tmp_reorder_list_ptr_prev);
                    }
                }
            } else {
                ret = false;
            }
        } else {
            // List didn't belong on bottom of existing list, check to see if it should be attached to
            // top of next list.
            if (NULL != tmp_reorder_list_ptr) {
                if (sequence_num == tmp_reorder_list_ptr->top_sequence_num-1) {
                    if (AddSgListToRxReorderListTop(protocol_handle, payload_sgl_entry_pool_handle,
                                                    tmp_reorder_list_ptr, new_sglist_ptr, sequence_num, initial_offset,
                                                    num_bytes_added_ptr)) {
                    } else {
                        ret = false;
                    }
                } else {
                    // List didn't belong on top of existing list, insert between the two lists.
                    if (CreateAndInsertRxReorderList(protocol_handle, reorder_entries_pool_handle,
                                                     payload_sgl_entry_pool_handle, new_sglist_ptr, sequence_num,
                                                     initial_offset, num_bytes_added_ptr, tmp_reorder_list_ptr_prev,
                                                     tmp_reorder_list_ptr, &new_reorder_list_ptr)) {
                    } else {
                        ret = false;
                    }
                }
            } else {
                // No next list, so create a new list to the right.
                if (CreateAndInsertRxReorderList(protocol_handle, reorder_entries_pool_handle,
                                                 payload_sgl_entry_pool_handle, new_sglist_ptr, sequence_num,
                                                 initial_offset, num_bytes_added_ptr, tmp_reorder_list_ptr_prev, NULL,
                                                 &new_reorder_list_ptr)) {
                } else {
                    ret = false;
                }
            }
        }
    }
    return ret;
}