Main Page | Modules | Data Structures | File List | Data Fields

list.c

00001 /* 
00002  * Copyright (c) 2005-2008 by KoanLogic s.r.l. - All rights reserved.  
00003  */
00004 
00005 static const char rcsid[] =
00006     "$Id: list.c,v 1.3 2008/04/25 19:29:18 tat Exp $";
00007 
00008 #include <u/libu_conf.h>
00009 #include <u/libu.h>
00010 #include <toolbox/list.h>
00011 
00012 typedef struct u_list_item_s
00013 {
00014     TAILQ_ENTRY(u_list_item_s) np;
00015     void *ptr;
00016 } u_list_item_t;
00017 
00018 struct u_list_s
00019 {
00020     TAILQ_HEAD(u_list_head_s, u_list_item_s) head;
00021     size_t count;
00022 };
00023 
00036 int u_list_create(u_list_t **plist)
00037 {
00038     u_list_t *list = NULL;
00039 
00040     list = u_zalloc(sizeof(u_list_t));
00041     dbg_err_if(list == NULL); 
00042 
00043     TAILQ_INIT(&list->head);
00044 
00045     *plist = list;
00046 
00047     return 0;
00048 err:
00049     if(list)
00050         u_free(list);
00051     return ~0;
00052 }
00053 
00062 void u_list_free(u_list_t *list)
00063 {
00064     u_list_item_t *item = NULL;
00065 
00066     dbg_return_if(list == NULL, );
00067 
00068     while((item = TAILQ_FIRST(&list->head)) != NULL)
00069     {
00070         TAILQ_REMOVE(&list->head, item, np);
00071 
00072         u_free(item);
00073     }
00074 
00075     u_free(list);
00076 
00077     return;
00078 }
00079 
00088 int u_list_add(u_list_t *list, void *ptr)
00089 {
00090     u_list_item_t *item = NULL;
00091 
00092     dbg_return_if (list == NULL, ~0);
00093     dbg_return_if (ptr == NULL, ~0);
00094 
00095     item = (u_list_item_t*)u_zalloc(sizeof(u_list_item_t));
00096     dbg_err_if(item == NULL);
00097 
00098     item->ptr = ptr;
00099         
00100     TAILQ_INSERT_TAIL(&list->head, item, np);
00101     list->count++;
00102 
00103     return 0;
00104 err:
00105     if(item)
00106         u_free(item);
00107     return ~0;
00108 }
00109 
00118 int u_list_del(u_list_t *list, void *ptr)
00119 {
00120     u_list_item_t *item = NULL;
00121 
00122     TAILQ_FOREACH(item, &list->head, np)
00123     {
00124         if(item->ptr == ptr)
00125         {
00126             TAILQ_REMOVE(&list->head, item, np);
00127             list->count--;
00128             u_free(item);
00129             return 0; /* removed */
00130         }
00131     }
00132 
00133     return ~0; /* not found */
00134 }
00135 
00143 size_t u_list_count(u_list_t *list)
00144 {
00145     /* a SIGBUS is better than returning 0 if list == NULL */
00146     return list->count;
00147 }
00148 
00158 void* u_list_get_n(u_list_t *list, size_t n)
00159 {
00160     u_list_item_t *item = NULL;
00161 
00162     dbg_return_if (list == NULL, NULL);
00163     dbg_return_if (n > list->count, NULL);
00164 
00165     TAILQ_FOREACH(item, &list->head, np)
00166     {
00167         if(n-- == 0)
00168             return item->ptr;
00169     }
00170 
00171     return NULL;
00172 }
00173 

←Products
© 2005-2008 - KoanLogic S.r.l. - All rights reserved