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
00012
00013
00014 typedef struct slist_t
00015 {
00016 struct slist_t *next;
00017 void *data;
00018 } slist_t;
00019
00020
00021
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
00052
00053
00054
00055
00056
00057
00058
00059
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
00079
00080
00081
00082
00083
00084
00085
00086
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
00104
00105
00106
00107
00108
00109
00110
00111
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
00125
00126
00127
00128
00129
00130
00131
00132
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
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 static inline unsigned char
00160 list_is_empty(slist_t *list)
00161 {
00162 return ((list) ? 0 : 1);
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
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
00205
00206
00207
00208
00209
00210
00211
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
00227
00228
00229
00230
00231
00232
00233
00234
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
00255
00256
00257
00258
00259
00260
00261
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