ca5dc60afe10f28982d1ecc8159accaf9a299614
[strongswan.git] / Source / charon / utils / linked_list.c
1 /**
2 * @file linked_list.c
3 *
4 * @brief Implementation of linked_list_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <stdlib.h>
24
25 #include "linked_list.h"
26
27 #include <utils/allocator.h>
28
29
30 typedef struct linked_list_element_t linked_list_element_t;
31
32 /**
33 * @brief Element of the linked_list.
34 *
35 * This element holds a pointer to the value of the list item itself.
36 */
37 struct linked_list_element_t {
38 /**
39 * Value of a list item.
40 */
41 void *value;
42
43 /**
44 * previous list element
45 * NULL if first element in list
46 */
47 linked_list_element_t *previous;
48
49 /**
50 * next list element
51 * NULL if last element in list
52 */
53 linked_list_element_t *next;
54
55 /**
56 * Destroys a linked_list_element object.
57 *
58 * @param linked_list_element_t calling object
59 */
60 void (*destroy) (linked_list_element_t *this);
61 };
62
63 /**
64 * Implementation of linked_list_element_t.destroy.
65 */
66 static void linked_list_element_destroy(linked_list_element_t *this)
67 {
68 allocator_free(this);
69 }
70
71 /**
72 * @brief Creates an empty linked list object.
73 *
74 * @warning Only the pointer to the value is stored.
75 *
76 * @param[in] value value of item to be set
77 * @return linked_list_element_t object
78 */
79
80 linked_list_element_t *linked_list_element_create(void *value)
81 {
82 linked_list_element_t *this = allocator_alloc_thing(linked_list_element_t);
83
84 this->destroy = linked_list_element_destroy;
85
86 this->previous=NULL;
87 this->next=NULL;
88 this->value = value;
89
90 return (this);
91 }
92
93
94 typedef struct private_linked_list_t private_linked_list_t;
95 /**
96 * Private variables and functions of linked list.
97 *
98 */
99 struct private_linked_list_t {
100 /**
101 * Public part of linked list.
102 */
103 linked_list_t public;
104
105 /**
106 * Number of items in the list.
107 */
108 int count;
109
110 /**
111 * First element in list.
112 * NULL if no elements in list.
113 */
114 linked_list_element_t *first;
115
116 /**
117 * Last element in list.
118 * NULL if no elements in list.
119 */
120 linked_list_element_t *last;
121 };
122
123
124 typedef struct private_iterator_t private_iterator_t;
125
126 /**
127 * Private variables and functions of linked list iterator.
128 */
129 struct private_iterator_t {
130 /**
131 * Public part of linked list iterator.
132 */
133 iterator_t public;
134
135 /**
136 * Associated linked list.
137 */
138 private_linked_list_t * list;
139
140 /**
141 * Current element of the iterator.
142 */
143 linked_list_element_t *current;
144
145 /**
146 * Direction of iterator.
147 */
148 bool forward;
149 };
150
151 /**
152 * Implementation of iterator_t.has_next.
153 */
154 bool iterator_has_next(private_iterator_t *this)
155 {
156 if (this->list->count == 0)
157 {
158 return FALSE;
159 }
160 if (this->current == NULL)
161 {
162 this->current = (this->forward) ? this->list->first : this->list->last;
163 return TRUE;
164 }
165 if (this->forward)
166 {
167 if (this->current->next == NULL)
168 {
169 return FALSE;
170 }
171 this->current = this->current->next;
172 return TRUE;
173 }
174 /* backward */
175 if (this->current->previous == NULL)
176 {
177 return FALSE;
178 }
179 this->current = this->current->previous;
180 return TRUE;
181 }
182
183 /**
184 * Implementation of iterator_t.current.
185 */
186 static status_t iterator_current(private_iterator_t *this, void **value)
187 {
188 if (this->current == NULL)
189 {
190 return NOT_FOUND;
191 }
192 *value = this->current->value;
193 return SUCCESS;
194 }
195
196 /**
197 * Implementation of iterator_t.reset.
198 */
199 static void iterator_reset(private_iterator_t *this)
200 {
201 this->current = NULL;
202 }
203
204 /**
205 * Implementation of iterator_t.remove.
206 */
207 static status_t remove(private_iterator_t *this)
208 {
209 linked_list_element_t *new_current;
210
211 if (this->current == NULL)
212 {
213 return NOT_FOUND;
214 }
215
216 if (this->list->count == 0)
217 {
218 return NOT_FOUND;
219 }
220 /* find out the new iterator position */
221 if (this ->current->previous != NULL)
222 {
223 new_current = this->current->previous;
224 }
225 else if (this->current->next != NULL)
226 {
227 new_current = this->current->next;
228 }
229 else
230 {
231 new_current = NULL;
232 }
233
234 /* now delete the entry :-) */
235 if (this->current->previous == NULL)
236 {
237 if (this->current->next == NULL)
238 {
239 this->list->first = NULL;
240 this->list->last = NULL;
241 }
242 else
243 {
244 this->current->next->previous = NULL;
245 this->list->first = this->current->next;
246 }
247 }
248 else if (this->current->next == NULL)
249 {
250 this->current->previous->next = NULL;
251 this->list->last = this->current->previous;
252 }
253 else
254 {
255 this->current->previous->next = this->current->next;
256 this->current->next->previous = this->current->previous;
257 }
258
259 this->list->count--;
260 this->current->destroy(this->current);
261 /* set the new iterator position */
262 this->current = new_current;
263 return SUCCESS;
264 }
265
266 /**
267 * Implementation of iterator_t.insert_before.
268 */
269 static void insert_before(private_iterator_t * iterator, void *item)
270 {
271 if (iterator->current == NULL)
272 {
273 iterator->list->public.insert_first(&(iterator->list->public), item);
274 }
275
276 linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
277
278 if (iterator->current->previous == NULL)
279 {
280 iterator->current->previous = element;
281 element->next = iterator->current;
282 iterator->list->first = element;
283 }
284 else
285 {
286 iterator->current->previous->next = element;
287 element->previous = iterator->current->previous;
288 iterator->current->previous = element;
289 element->next = iterator->current;
290 }
291
292 iterator->list->count++;
293 }
294
295 /**
296 * Implementation of iterator_t.insert_after.
297 */
298 static void insert_after(private_iterator_t * iterator, void *item)
299 {
300 if (iterator->current == NULL)
301 {
302 iterator->list->public.insert_first(&(iterator->list->public),item);
303 return;
304 }
305
306 linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
307
308 if (iterator->current->next == NULL)
309 {
310 iterator->current->next = element;
311 element->previous = iterator->current;
312 iterator->list->last = element;
313 }
314 else
315 {
316 iterator->current->next->previous = element;
317 element->next = iterator->current->next;
318 iterator->current->next = element;
319 element->previous = iterator->current;
320 }
321 iterator->list->count++;
322 }
323
324 /**
325 * Implementation of iterator_t.destroy.
326 */
327 static void iterator_destroy(private_iterator_t *this)
328 {
329 allocator_free(this);
330 }
331
332 /**
333 * Implementation of linked_list_t.get_count.
334 */
335 static int get_count(private_linked_list_t *this)
336 {
337 return this->count;
338 }
339
340
341 /**
342 * Implementation of linked_list_t.insert_first.
343 */
344 static void insert_first(private_linked_list_t *this, void *item)
345 {
346 linked_list_element_t *element;
347
348 element =(linked_list_element_t *) linked_list_element_create(item);
349
350 if (this->count == 0)
351 {
352 /* first entry in list */
353 this->first = element;
354 this->last = element;
355 element->previous = NULL;
356 element->next = NULL;
357 }
358 else
359 {
360 linked_list_element_t *old_first_element = this->first;
361 element->next = old_first_element;
362 element->previous = NULL;
363 old_first_element->previous = element;
364 this->first = element;
365 }
366
367 this->count++;
368 }
369
370 /**
371 * Implementation of linked_list_t.remove_first.
372 */
373 static status_t remove_first(private_linked_list_t *this, void **item)
374 {
375 if (this->count == 0)
376 {
377 return NOT_FOUND;
378 }
379
380 linked_list_element_t *element = this->first;
381
382 if (element->next != NULL)
383 {
384 element->next->previous = NULL;
385 }
386 this->first = element->next;
387
388 *item = element->value;
389
390 this->count--;
391
392 element->destroy(element);
393
394 return SUCCESS;
395 }
396
397 /**
398 * Implementation of linked_list_t.get_first.
399 */
400 static status_t get_first(private_linked_list_t *this, void **item)
401 {
402 if (this->count == 0)
403 {
404 return NOT_FOUND;
405 }
406
407 *item = this->first->value;
408
409 return SUCCESS;
410 }
411
412 /**
413 * Implementation of linked_list_t.insert_last.
414 */
415 static void insert_last(private_linked_list_t *this, void *item)
416 {
417 linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item);
418
419 if (this->count == 0)
420 {
421 /* first entry in list */
422 this->first = element;
423 this->last = element;
424 element->previous = NULL;
425 element->next = NULL;
426 }
427 else
428 {
429
430 linked_list_element_t *old_last_element = this->last;
431 element->previous = old_last_element;
432 element->next = NULL;
433 old_last_element->next = element;
434 this->last = element;
435 }
436
437 this->count++;
438 }
439
440 /**
441 * Implementation of linked_list_t.remove_last.
442 */
443 static status_t remove_last(private_linked_list_t *this, void **item)
444 {
445 if (this->count == 0)
446 {
447 return NOT_FOUND;
448 }
449
450 linked_list_element_t *element = this->last;
451
452 if (element->previous != NULL)
453 {
454 element->previous->next = NULL;
455 }
456 this->last = element->previous;
457
458 *item = element->value;
459
460 this->count--;
461
462 element->destroy(element);
463
464 return SUCCESS;
465 }
466
467 /**
468 * Implementation of linked_list_t.get_last.
469 */
470 static status_t get_last(private_linked_list_t *this, void **item)
471 {
472 if (this->count == 0)
473 {
474 return NOT_FOUND;
475 }
476
477 *item = this->last->value;
478
479 return SUCCESS;
480 }
481
482 /**
483 * Implementation of linked_list_t.create_iterator.
484 */
485 static void create_iterator (private_linked_list_t *linked_list, iterator_t **iterator,bool forward)
486 {
487 private_iterator_t *this = allocator_alloc_thing(private_iterator_t);
488
489 this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
490 this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current;
491 this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before;
492 this->public.insert_after = (void (*) (iterator_t *this, void *item)) insert_after;
493 this->public.remove = (status_t (*) (iterator_t *this)) remove;
494 this->public.reset = (void (*) (iterator_t *this)) iterator_reset;
495 this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy;
496
497 this->forward = forward;
498 this->current = NULL;
499 this->list = linked_list;
500
501 *iterator = &(this->public);
502 }
503
504 /**
505 * Implementation of linked_list_t.destroy.
506 */
507 static void linked_list_destroy(private_linked_list_t *this)
508 {
509 void * value;
510 /* Remove all list items before destroying list */
511
512 while (this->public.remove_first(&(this->public),&value) != NOT_FOUND)
513 {
514 /* values are not destroyed so memory leaks are possible
515 * if list is not empty when deleting */
516 }
517 allocator_free(this);
518 }
519
520 /*
521 * Described in header
522 */
523 linked_list_t *linked_list_create()
524 {
525 private_linked_list_t *this = allocator_alloc_thing(private_linked_list_t);
526
527 this->public.get_count = (int (*) (linked_list_t *linked_list)) get_count;
528 this->public.create_iterator = (void (*) (linked_list_t *linked_list, iterator_t **iterator,bool forward)) create_iterator;
529 this->public.get_first = (status_t (*) (linked_list_t *linked_list, void **item)) get_first;
530 this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last;
531 this->public.insert_first = (void (*) (linked_list_t *linked_list, void *item)) insert_first;
532 this->public.insert_last = (void (*) (linked_list_t *linked_list, void *item)) insert_last;
533 this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first;
534 this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last;
535 this->public.destroy = (void (*) (linked_list_t *linked_list)) linked_list_destroy;
536
537 this->count = 0;
538 this->first = NULL;
539 this->last = NULL;
540
541 return (&(this->public));
542 }