Main Page   File List  

sll.h

00001 #ifndef __L4UTIL__SLL_H__
00002 #define __L4UTIL__SLL_H__
00003 
00004 #include <stdlib.h>
00005 
00006 #include <l4/sys/compiler.h>
00007 
00008 EXTERN_C_BEGIN
00009 
00010 /*
00011  * the linked list structure
00012  */
00013 
00014 typedef struct slist_t
00015 {
00016   struct slist_t *next;   /* pointer to next node */
00017   void *data;          /* void pointer for user data */
00018 } slist_t;
00019 
00020 /*
00021  * function prototypes
00022  */
00023 static inline slist_t*
00024 list_new_entry(void *data);
00025 
00026 static inline slist_t*
00027 list_append(slist_t *list, slist_t *new_node);
00028 
00029 static inline slist_t*
00030 list_remove(slist_t *list, slist_t *node);
00031 
00032 static inline void
00033 list_free_entry(slist_t **list);
00034 
00035 static inline unsigned char
00036 list_is_empty(slist_t *list);
00037 
00038 static inline slist_t*
00039 list_get_at(slist_t *list, int n);
00040 
00041 static inline slist_t*
00042 list_add(slist_t *list, slist_t *new_node);
00043 
00044 static inline void
00045 list_insert_after(slist_t *after, slist_t *new_node);
00046 
00047 static inline int
00048 list_elements(slist_t *head);
00049 
00050 /*
00051  *  allocateNode()
00052  *  allocate a new node.
00053  *
00054  *  Parameters:
00055  *  void    *data       a generic pointer to object data
00056  *
00057  *  Return Values:
00058  *  pointer to slist_t if succeeds
00059  *  NULL otherwise
00060  *
00061  */
00062 static inline slist_t*
00063 list_new_entry(void *data)
00064 {
00065   slist_t *sll;
00066 
00067   sll = malloc(sizeof(slist_t));
00068   if (!sll)
00069     return ((slist_t *) NULL);
00070 
00071   sll->data=data;
00072   sll->next=NULL;
00073 
00074   return (sll);
00075 }
00076 
00077 /*
00078  *  appendNode()
00079  *  appends a node to the end of a list
00080  *
00081  *  Parameters:
00082  *  slist_t *head      - modify the list
00083  *  slist_t *new       - appends this node
00084  *
00085  *  Return Values:
00086  *  the new list
00087  *
00088  */
00089 static inline slist_t*
00090 list_append(slist_t *head, slist_t *new_node)
00091 {
00092   slist_t *ret = head;
00093   if (!head)
00094     return new_node;
00095 
00096   while (head->next)
00097     head = head->next;
00098   head->next = new_node;
00099   return ret;
00100 }
00101 
00102 /*
00103  *  insertNode()
00104  *  insert a node at the beginning of a list
00105  *
00106  *  Parameters:
00107  *  slist_t *head      - modify this list
00108  *  slist_t *new       - appends this node
00109  *
00110  *  Return Values:
00111  *  the new list
00112  *
00113  */
00114 static inline slist_t*
00115 list_add(slist_t *head, slist_t *new_node)
00116 {
00117   if (!new_node)
00118     return head;
00119   new_node->next = head;
00120   return new_node;
00121 }
00122 
00123 /*
00124  *  insertNode()
00125  *  insert a node at the beginning of a list
00126  *
00127  *  Parameters:
00128  *  slist_t *head      - modify this list
00129  *  slist_t *new       - appends this node
00130  *
00131  *  Return Values:
00132  *  the new list
00133  *
00134  */
00135 static inline void
00136 list_insert_after(slist_t *after, slist_t *new_node)
00137 {
00138   if (!new_node)
00139     return;
00140   if (!after)
00141     return;
00142   new_node->next = after->next;
00143   after->next = new_node;
00144 }
00145 
00146 
00147 /*
00148  *  emptyList()
00149  *  check if a list variable is NULL
00150  *
00151  *  Parameters:
00152  *  slist_t *list      list
00153  *
00154  *  Return Values:
00155  *  TRUE    if empty
00156  *  FALSE   if not empty
00157  *
00158  */
00159 static inline unsigned char
00160 list_is_empty(slist_t *list)
00161 {
00162   return ((list) ? 0 : 1);
00163 }
00164 
00165 /*
00166  *  delNode()
00167  *  remove a node from a list
00168  *
00169  *  Parameters:
00170  *  slist_t  *head      - list to modify
00171  *  slist_t *node       - node to remove
00172  *
00173  *  Return Values:
00174  *  none
00175  *
00176  */
00177 static inline slist_t*
00178 list_remove(slist_t *head, slist_t *node)
00179 {
00180   slist_t *ret = head;
00181   if (list_is_empty(head))
00182     return ret;
00183   if (!node)
00184     return ret;
00185 
00186   if (head == node)
00187     {
00188       ret = head->next;
00189     }
00190   else
00191     {
00192       while (head && (head->next != node))
00193         head = head->next;
00194       if (!head)
00195         return ret;
00196       else
00197         head->next = node->next;
00198     }
00199   list_free_entry(&node);
00200   return ret;
00201 }
00202 
00203 /*
00204  *  freeNode()
00205  *  frees a node
00206  *
00207  *  Parameters:
00208  *  slist_t  *list  node to free
00209  *
00210  *  Return Values:
00211  *  none
00212  *
00213  */
00214 static inline void
00215 list_free_entry(slist_t **list)
00216 {
00217   if (*list)
00218     {
00219       free ((void *) (*list));
00220       (*list)=NULL;
00221     }
00222 }
00223 
00224 
00225 /*
00226  *  getNthNode()
00227  *  get nth node in a list
00228  *
00229  *  Parameters:
00230  *  slist_t *list       - the head list
00231  *  int n           - return the node
00232  *  Return Values:
00233  *  a pointer to the list at position n
00234  *  NULL if there's no such node at posion n
00235  *
00236  */
00237 static inline slist_t*
00238 list_get_at(slist_t *list, int n)
00239 {
00240   int j=0;
00241 
00242   while (list)
00243     {
00244       j++;
00245       if (j == n)
00246         return (list);
00247       list = list->next;
00248     }
00249 
00250   return ((slist_t *) NULL);
00251 }
00252 
00253 /*
00254  *  numNodes()
00255  *  returns number of nodes in the list
00256  *
00257  *  Parameters:
00258  *  slist_t  *head      - the head node of the list
00259  *
00260  *  Return Values:
00261  *  number of node/s
00262  *
00263  */
00264 static inline int
00265 list_elements(slist_t *head)
00266 {
00267   register int n;
00268   for (n=0; head; head=head->next) n++;
00269   return (n);
00270 }
00271 
00272 EXTERN_C_END
00273 
00274 #endif  /* __L4UTIL__SLL_H__ */

L4 Utilities, part of DROPS  © 2000-2003