- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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

Go to the documentation of this file.

    00001
    00020 /*
    00021  * INCLUDE FILES
    00022  ****************************************************************************************
    00023  */
    00024 // for mem* functions
    00025 #include <string.h>
    00026
    00027 #include "co_list.h"
    00028
    00029 // for assertions
    00030 #include "dbg_assert.h"
    00031
    00032
    00033 /*
    00034  * FUNCTION DEFINITIONS
    00035  ****************************************************************************************
    00036  */
    00037 void co_list_init(struct co_list *list)
    00038 {
    00039     list->first = NULL;
    00040     list->last = NULL;
    00041 }
    00042
    00043 void co_list_pool_init(struct co_list *list,
    00044                        void *pool,
    00045                        size_t elmt_size,
    00046                        uint32_t elmt_cnt,
    00047                        void *default_value)
    00048 {
    00049     uint32_t i;
    00050
    00051     // initialize the free list relative to the pool
    00052     co_list_init(list);
    00053
    00054     // Add each element of the pool to this list, and init them one by one
    00055     for (i = 0; i < elmt_cnt; i++)
    00056     {
    00057         if (default_value)
    00058         {
    00059             memcpy(pool, default_value, elmt_size);
    00060         }
    00061         co_list_push_back(list, (struct co_list_hdr *) pool);
    00062
    00063         // move to the next pool element
    00064         pool = (void *)((uint8_t *)pool + (uint32_t)elmt_size);
    00065     }
    00066 }
    00067
    00068 void co_list_push_back(struct co_list *list,
    00069                        struct co_list_hdr *list_hdr)
    00070 {
    00071     // Sanity check
    00072     ASSERT_ERR(list_hdr != NULL);
    00073
    00074     // check if list is empty
    00075     if (co_list_is_empty(list))
    00076     {
    00077         // list empty => pushed element is also head
    00078         list->first = list_hdr;
    00079     }
    00080     else
    00081     {
    00082         // list not empty => update next of last
    00083         list->last->next = list_hdr;
    00084     }
    00085
    00086     // add element at the end of the list
    00087     list->last = list_hdr;
    00088     list_hdr->next = NULL;
    00089 }
    00090
    00091 void co_list_push_front(struct co_list *list,
    00092                         struct co_list_hdr *list_hdr)
    00093 {
    00094     // Sanity check
    00095     ASSERT_ERR(list_hdr != NULL);
    00096
    00097     // check if list is empty
    00098     if (co_list_is_empty(list))
    00099     {
    00100         // list empty => pushed element is also head
    00101         list->last = list_hdr;
    00102     }
    00103
    00104     // add element at the beginning of the list
    00105     list_hdr->next = list->first;
    00106     list->first = list_hdr;
    00107 }
    00108
    00109 struct co_list_hdr *co_list_pop_front(struct co_list *list)
    00110 {
    00111     struct co_list_hdr *element;
    00112
    00113     // check if list is empty
    00114     element = list->first;
    00115     if (element != NULL)
    00116     {
    00117         // The list isn't empty : extract the first element
    00118         list->first = list->first->next;
    00119         if (list->last == element)
    00120             list->last = NULL;
    00121     }
    00122
    00123     return element;
    00124 }
    00125
    00126 void co_list_extract(struct co_list *list,
    00127                      struct co_list_hdr *list_hdr)
    00128 {
    00129     struct co_list_hdr *scan_list;
    00130
    00131     // sanity check
    00132     ASSERT_ERR(list != NULL);
    00133
    00134     scan_list = list->first;
    00135
    00136     // Check if list is empty or not
    00137     if (scan_list == NULL)
    00138         return;
    00139
    00140     // check if searched element is first
    00141     if (scan_list == list_hdr)
    00142     {
    00143         // Extract first element
    00144         list->first = scan_list->next;
    00145         if (list->last == scan_list)
    00146             list->last = NULL;
    00147     }
    00148     else
    00149     {
    00150         // Look for the element in the list
    00151         while ((scan_list->next != NULL) && (scan_list->next != list_hdr))
    00152         {
    00153             scan_list = scan_list->next;
    00154         }
    00155
    00156         // Check if element was found in the list
    00157         if (scan_list->next != NULL)
    00158         {
    00159             // check if the removed element is the last in the list
    00160             if (list->last == list_hdr)
    00161             {
    00162                 // Last element will be extracted
    00163                 list->last = scan_list;
    00164             }
    00165             // Extract the element from the list
    00166             scan_list->next = list_hdr->next;
    00167         }
    00168     }
    00169 }
    00170
    00171 bool co_list_find(struct co_list *list,
    00172                   struct co_list_hdr *list_hdr)
    00173 {
    00174     struct co_list_hdr *tmp_list_hdr;
    00175
    00176     // Go through the list to find the element
    00177     tmp_list_hdr = list->first;
    00178     while((tmp_list_hdr != list_hdr) && (tmp_list_hdr != NULL))
    00179     {
    00180         tmp_list_hdr = tmp_list_hdr->next;
    00181     }
    00182
    00183     return (tmp_list_hdr == list_hdr);
    00184 }
    00185
    00186 uint32_t co_list_cnt(const struct co_list *const list)
    00187 {
    00188     uint32_t cnt = 0;
    00189     struct co_list_hdr *elt = co_list_pick(list);
    00190
    00191     // Go through the list to count the number of elements
    00192     while (elt != NULL)
    00193     {
    00194         cnt++;
    00195         elt = co_list_next(elt);
    00196     }
    00197
    00198     return(cnt);
    00199 }
    00200
    00216 void co_list_insert(struct co_list * const list,
    00217                     struct co_list_hdr * const element,
    00218                     bool (*cmp)(struct co_list_hdr const *elementA,
    00219                                 struct co_list_hdr const *elementB))
    00220 {
    00221     struct co_list_hdr *prev = NULL;
    00222     struct co_list_hdr *scan = list->first;
    00223
    00224     for(;;)
    00225     {
    00226         // scan the list until the end or cmp() returns true
    00227         if (scan)
    00228         {
    00229             if (cmp(element, scan))
    00230             {
    00231                 // insert now
    00232                 break;
    00233             }
    00234             prev = scan;
    00235             scan = scan->next;
    00236         }
    00237         else
    00238         {
    00239             // end of list
    00240             list->last = element;
    00241             break;
    00242         }
    00243     }
    00244
    00245     element->next = scan;
    00246
    00247     if (prev)
    00248     {
    00249         // second or more
    00250         prev->next = element;
    00251     }
    00252     else
    00253     {
    00254         // first message
    00255         list->first = element;
    00256     }
    00257 }
    00258
    00259 void co_list_insert_after(struct co_list * const list,
    00260                           struct co_list_hdr * const prev_element,
    00261                           struct co_list_hdr * const element)
    00262 {
    00263     struct co_list_hdr *scan = list->first;
    00264
    00265     if (prev_element == NULL)
    00266     {
    00267         // Insert the element in front on the list
    00268         co_list_push_front(list, element);
    00269     }
    00270     else
    00271     {
    00272         // Look for prev_element in the list
    00273         while (scan)
    00274         {
    00275             if (scan == prev_element)
    00276             {
    00277                 break;
    00278             }
    00279
    00280             // Get next element
    00281             scan = scan->next;
    00282         }
    00283
    00284         // If prev_element has been found, insert element
    00285         if (scan)
    00286         {
    00287             element->next = prev_element->next;
    00288             prev_element->next = element;
    00289
    00290             if (list->last == prev_element)
    00291             {
    00292                 list->last = element;
    00293             }
    00294         }
    00295     }
    00296 }
    00297
    00298 void co_list_insert_before(struct co_list * const list,
    00299                            struct co_list_hdr * const next_element,
    00300                            struct co_list_hdr * const element)
    00301 {
    00302     if (next_element == NULL)
    00303     {
    00304         // Insert the element at the end of the list
    00305         co_list_push_back(list, element);
    00306     }
    00307     else if (next_element == list->first)
    00308     {
    00309         // Insert the element in front of the list
    00310         co_list_push_front(list, element);
    00311     }
    00312     else
    00313     {
    00314         struct co_list_hdr *scan = list->first;
    00315
    00316         // Look for next_element in the list
    00317         while (scan)
    00318         {
    00319             if (scan->next == next_element)
    00320             {
    00321                 break;
    00322             }
    00323
    00324             // Get next element
    00325             scan = scan->next;
    00326         }
    00327
    00328         // Insert element after scan
    00329         if (scan)
    00330         {
    00331             element->next = next_element;
    00332             scan->next = element;
    00333         }
    00334     }
    00335 }
    00336
    00337 void co_list_concat(struct co_list *list1, struct co_list *list2)
    00338 {
    00339     // If list2 is empty, don't do anything
    00340     if (list2->first != NULL)
    00341     {
    00342         // Check if list1 is empty
    00343         if (list1->first == NULL)
    00344         {
    00345             // If list1 is empty, list1 becomes list2
    00346             *list1 = *list2;
    00347         }
    00348         else
    00349         {
    00350             // Otherwise, append list2 to list1
    00351             list1->last->next = list2->first;
    00352             list1->last = list2->last;
    00353         }
    00354         // Clear list2
    00355         list2->first = NULL;
    00356     }
    00357 }
    00358
    00359
    00360 void co_list_remove(struct co_list *list,
    00361                     struct co_list_hdr *prev_element,
    00362                     struct co_list_hdr *element)
    00363 {
    00364     // sanity check
    00365     ASSERT_ERR(list != NULL);
    00366     ASSERT_ERR((prev_element == NULL) || (prev_element->next == element));
    00367     ASSERT_ERR(element != NULL);
    00368
    00369
    00370     if (prev_element == NULL)
    00371     {
    00372         list->first = element->next;
    00373     }
    00374     else
    00375     {
    00376         prev_element->next = element->next;
    00377     }
    00378     if (list->last == element)
    00379         list->last = prev_element;
    00380
    00381     element->next = NULL;
    00382 }
    00383