- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# /space/home/slher/rel/Beken/v6_4_5/macsw/modules/common/src/co_dlist.c

Go to the documentation of this file.

    00001
    00020 /*
    00021  * INCLUDE FILES
    00022  ****************************************************************************************
    00023  */
    00024 #include "co_dlist.h"
    00025
    00026 // for assertions
    00027 #include "dbg_assert.h"
    00028
    00029
    00030 /*
    00031  * FUNCTION DEFINITIONS
    00032  ****************************************************************************************
    00033  */
    00034 void co_dlist_init(struct co_dlist *list)
    00035 {
    00036     list->first = NULL;
    00037     list->last = NULL;
    00038     list->cnt = 0;
    00039
    00040     #if NX_DEBUG
    00041     list->maxcnt = 0;
    00042     list->mincnt = 0xFFFFFFFF;
    00043     #endif // NX_DEBUG
    00044 }
    00045
    00046 void co_dlist_push_back(struct co_dlist *list, struct co_dlist_hdr *list_hdr)
    00047 {
    00048
    00049     // check if the list is empty
    00050     if (co_dlist_is_empty(list))
    00051     {
    00052         // list is empty => update the first because the added one is also the first
    00053         list->first = list_hdr;
    00054     }
    00055     else
    00056     {
    00057         // list not empty => just update the last
    00058         list->last->next = list_hdr;
    00059     }
    00060
    00061     // add the element at the back of the list
    00062     list_hdr->prev = list->last;
    00063     list->last = list_hdr;
    00064     list_hdr->next = NULL;
    00065     list->cnt++;
    00066
    00067     #if NX_DEBUG
    00068     if(list->maxcnt < list->cnt)
    00069     {
    00070         list->maxcnt = list->cnt;
    00071     }
    00072     #endif // NX_DEBUG
    00073 }
    00074
    00075 void co_dlist_push_front(struct co_dlist *list,struct co_dlist_hdr *list_hdr)
    00076 {
    00077     // check if list is empty
    00078     if (co_dlist_is_empty(list))
    00079     {
    00080         // list is empty => update the last because the added one is also the last
    00081         list->last = list_hdr;
    00082     }
    00083     else
    00084     {
    00085         // list is empty => just update the first
    00086         list->first->prev = list_hdr;
    00087     }
    00088
    00089     // add the element at the front of the list
    00090     list_hdr->next = list->first;
    00091     list->first = list_hdr;
    00092     list_hdr->prev = NULL;
    00093     list->cnt++;
    00094
    00095     #if NX_DEBUG
    00096     if(list->maxcnt < list->cnt)
    00097     {
    00098         list->maxcnt = list->cnt;
    00099     }
    00100     #endif // NX_DEBUG
    00101 }
    00102
    00103 struct co_dlist_hdr *co_dlist_pop_front(struct co_dlist *list)
    00104 {
    00105     struct co_dlist_hdr *element;
    00106
    00107     element = list->first;
    00108     if ( element != NULL )
    00109     {
    00110         // The list isn't empty : extract the first element
    00111         list->cnt--;
    00112         list->first = list->first->next;
    00113
    00114         // check if list is empty after popping
    00115         if (!co_dlist_is_empty(list))
    00116         {
    00117             list->first->prev = NULL;
    00118         }
    00119         else
    00120         {
    00121             // since list is empty, clear the last
    00122             list->last = NULL;
    00123         }
    00124
    00125         #if NX_DEBUG
    00126         if(list->mincnt > list->cnt)
    00127         {
    00128             list->mincnt = list->cnt;
    00129         }
    00130         #endif // NX_DEBUG
    00131     }
    00132     return element;
    00133 }
    00134
    00135 void co_dlist_extract(struct co_dlist *list, struct co_dlist_hdr const *list_hdr)
    00136 {
    00137     struct co_dlist_hdr *first;
    00138
    00139     // retrieve the first pointer
    00140     first = list->first;
    00141
    00142     // sanity check: should never call extract on empty list
    00143     ASSERT_ERR(first != NULL);
    00144
    00145     // check if first is the searched element
    00146     if (first == list_hdr)
    00147     {
    00148         // Extract first element
    00149         list->first = first->next;
    00150
    00151         // check if list is empty after extracting
    00152         if (!co_dlist_is_empty(list))
    00153         {
    00154             list->first->prev = NULL;
    00155         }
    00156         else
    00157         {
    00158             // since list is empty, clear the last
    00159             list->last = NULL;
    00160         }
    00161     }
    00162     else
    00163     {
    00164         #if NX_DEBUG
    00165         struct co_dlist_hdr *scan_list;
    00166         // verify that the element is in the list (for debugging only)
    00167         // Look for the element in the list
    00168         scan_list = list->first;
    00169         while (scan_list->next != list_hdr)    // Element not found
    00170         {
    00171             scan_list = scan_list->next;
    00172             ASSERT_ERR(scan_list != NULL);  // element is not in the list !
    00173         }
    00174         // end of : verify that the element is in the list (for debugging only)
    00175         #endif // NX_DEBUG
    00176
    00177         // re move the element by linking its next and previous elements
    00178         list_hdr->prev->next = list_hdr->next;
    00179
    00180         // check if the searched element is the last
    00181         if(list->last == list_hdr)
    00182         {
    00183             // the searched is the last element => update the last element with the previous
    00184             list->last = list_hdr->prev;
    00185         }
    00186         else
    00187         {
    00188             // the searched is not the last element => update the next's previous
    00189             list_hdr->next->prev = list_hdr->prev;
    00190         }
    00191     }
    00192
    00193     list->cnt--;
    00194
    00195     #if NX_DEBUG
    00196     if(list->mincnt > list->cnt)
    00197     {
    00198         list->mincnt = list->cnt;
    00199     }
    00200     #endif // NX_DEBUG
    00201 }
    00202