QUARK  0.9.0
QUARK-QUeuingAndRuntimeforKernels
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
icl_list.c
Go to the documentation of this file.
1 
7 /* $Id: icl_list.c 1693 2010-10-26 18:29:56Z yarkhan $ */
8 /* $UTK_Copyright: $ */
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 
13 #include "icl_list.h"
14 
21 icl_list_t *
23 {
24  icl_list_t *node;
25 
26  node = (icl_list_t*)malloc(sizeof(icl_list_t));
27 
28  if(!node) return NULL;
29 
30  node->flink = NULL;
31  node->blink = node;
32  node->data = NULL;
33 
34  return(node);
35 }
36 
48 icl_list_t *
49 icl_list_insert(icl_list_t *head, icl_list_t *pos, void *data)
50 {
51  icl_list_t *node;
52 
53  if(!head || !pos) return NULL;
54 
55  node = (icl_list_t*)malloc(sizeof(icl_list_t));
56 
57  if(!node) return NULL;
58 
59  node->blink = pos;
60  node->flink = pos->flink;
61  node->data = data;
62 
63  if(pos->flink)
64  pos->flink->blink = node;
65  else
66  head->blink = node; /* node at end of list */
67 
68  pos->flink = node;
69 
70  return node;
71 }
72 
83 int
84 icl_list_delete(icl_list_t *head, icl_list_t *pos, void (*free_function)(void *))
85 {
86  if (!pos || !head) return -1;
87  if (pos == head) return -1;
88 
89  if(free_function && pos->data)
90  (*free_function)(pos->data);
91 
92  pos->blink->flink = pos->flink;
93 
94  if(pos->flink)
95  pos->flink->blink = pos->blink;
96  else
97  head->blink = pos->blink; /* pos at end of list */
98 
99  free(pos);
100 
101  return 0;
102 }
103 
114 icl_list_t *
115 icl_list_search(icl_list_t *head, void *data, int (*compare)(void*, void*))
116 {
117  icl_list_t *node;
118 
119  if (!head) return NULL;
120 
121  for (node=head->flink; node!=NULL; node=node->flink) {
122  if(!node->data)
123  continue;
124  if((compare && (*compare)(node->data, data)==0))
125  break;
126  else if (node->data==data)
127  break; /* compare == NULL, then direct comparison of pointers */
128  }
129 
130  return(node);
131 }
132 
142 int
143 icl_list_destroy(icl_list_t *head, void (*free_function)(void*))
144 {
145  icl_list_t *node, *tmpnode;
146 
147  if (!head) return -1;
148 
149  for(node=head; node!=NULL; ) {
150  tmpnode = node->flink;
151 
152  if(free_function!=NULL && node->data!=NULL)
153  (*free_function)(node->data);
154 
155  free(node);
156  node = tmpnode;
157  }
158 
159  return 0;
160 }
161 
170 int
172 {
173  int size=0;
174 
175  if(!head) return -1;
176 
177  while((head=head->flink))
178  size++;
179 
180  return size;
181 }
182 
191 icl_list_t *
193 {
194  if(head)
195  return head->flink;
196 
197  return NULL;
198 }
199 
208 icl_list_t *
210 {
211  icl_list_t *pos;
212  for ( pos=head; pos->flink!=NULL; pos=pos->flink ) {}
213  if ( pos->blink==pos ) return NULL;
214  else return pos;
215 }
216 
217 
227 icl_list_t *
229 {
230  if(pos)
231  return pos->flink;
232 
233  return NULL;
234 }
235 
245 icl_list_t *
247 {
248  if(pos && pos->blink!=NULL && pos!=head && pos->blink!=head && pos->blink!=pos )
249  return pos->blink;
250 
251  return NULL;
252 }
253 
264 icl_list_t *
266 {
267  if(!head1 || !head2 || !head1->blink || !head2->flink)
268  return NULL;
269 
270  head1->blink->flink = head2->flink;
271  head2->flink->blink = head1->blink;
272  head1->blink = head2->blink;
273 
274  free(head2);
275 
276  return(head1);
277 }
278 
288 icl_list_t *
289 icl_list_prepend(icl_list_t *head, void *data)
290 {
291  return(icl_list_insert(head, head, data));
292 }
293 
303 icl_list_t *
304 icl_list_append(icl_list_t *head, void *data)
305 {
306  return(icl_list_insert(head, head->blink, data));
307 }