Debian Codegraph @

liblsd/list.c

raw
1 /*****************************************************************************
2 * $Id: list.c 3709 2006-11-29 00:51:22Z dun $
3 *****************************************************************************
4 * Copyright (C) 2001-2002 The Regents of the University of California.
5 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6 * Written by Chris Dunlap <[email protected]>.
7 *
8 * This file is from LSD-Tools, the LLNL Software Development Toolbox.
9 *
10 * LSD-Tools is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * LSD-Tools is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with LSD-Tools; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *****************************************************************************
24 * Refer to "list.h" for documentation on public functions.
25 *****************************************************************************/
26
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
31
32 #ifdef WITH_PTHREADS
33 # include <pthread.h>
34 #endif /* WITH_PTHREADS */
35
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "list.h"
41
42
43 /*********************
44 * lsd_fatal_error *
45 *********************/
46
47 #ifdef WITH_LSD_FATAL_ERROR_FUNC
48 # undef lsd_fatal_error
49 extern void lsd_fatal_error(char *file, int line, char *mesg);
50 #else /* !WITH_LSD_FATAL_ERROR_FUNC */
51 # ifndef lsd_fatal_error
52 # include <errno.h>
53 # include <stdio.h>
54 # include <string.h>
55 # define lsd_fatal_error(file, line, mesg) \
56 do { \
57 fprintf(stderr, "ERROR: [%s:%d] %s: %s\n", \
58 file, line, mesg, strerror(errno)); \
59 } while (0)
60 # endif /* !lsd_fatal_error */
61 #endif /* !WITH_LSD_FATAL_ERROR_FUNC */
62
63
64 /*********************
65 * lsd_nomem_error *
66 *********************/
67
68 #ifdef WITH_LSD_NOMEM_ERROR_FUNC
69 # undef lsd_nomem_error
70 extern void * lsd_nomem_error(char *file, int line, char *mesg);
71 #else /* !WITH_LSD_NOMEM_ERROR_FUNC */
72 # ifndef lsd_nomem_error
73 # define lsd_nomem_error(file, line, mesg) (NULL)
74 # endif /* !lsd_nomem_error */
75 #endif /* !WITH_LSD_NOMEM_ERROR_FUNC */
76
77
78 /***************
79 * Constants *
80 ***************/
81
82 #define LIST_ALLOC 32
83 #define LIST_MAGIC 0xDEADBEEF
84
85 #define USE_INTERNAL_ALLOC 0
86
87
88 /****************
89 * Data Types *
90 ****************/
91
92 struct listNode {
93 void *data; /* node's data */
94 struct listNode *next; /* next node in list */
95 };
96
97 struct listIterator {
98 struct list *list; /* the list being iterated */
99 struct listNode *pos; /* the next node to be iterated */
100 struct listNode **prev; /* addr of 'next' ptr to prv It node */
101 struct listIterator *iNext; /* iterator chain for list_destroy() */
102 #ifndef NDEBUG
103 unsigned int magic; /* sentinel for asserting validity */
104 #endif /* !NDEBUG */
105 };
106
107 struct list {
108 struct listNode *head; /* head of the list */
109 struct listNode **tail; /* addr of last node's 'next' ptr */
110 struct listIterator *iNext; /* iterator chain for list_destroy() */
111 ListDelF fDel; /* function to delete node data */
112 int count; /* number of nodes in list */
113 #ifdef WITH_PTHREADS
114 pthread_mutex_t mutex; /* mutex to protect access to list */
115 #endif /* WITH_PTHREADS */
116 #ifndef NDEBUG
117 unsigned int magic; /* sentinel for asserting validity */
118 #endif /* !NDEBUG */
119 };
120
121 typedef struct listNode * ListNode;
122
123
124 /****************
125 * Prototypes *
126 ****************/
127
128 static void * list_node_create (List l, ListNode *pp, void *x);
129 static void * list_node_destroy (List l, ListNode *pp);
130 static List list_alloc (void);
131 static void list_free (List l);
132 static ListNode list_node_alloc (void);
133 static void list_node_free (ListNode p);
134 static ListIterator list_iterator_alloc (void);
135 static void list_iterator_free (ListIterator i);
136 static void * list_alloc_aux (int size, void *pfreelist);
137 static void list_free_aux (void *x, void *pfreelist);
138
139
140 /***************
141 * Variables *
142 ***************/
143
144 static List list_free_lists = NULL;
145 static ListNode list_free_nodes = NULL;
146 static ListIterator list_free_iterators = NULL;
147
148 #ifdef WITH_PTHREADS
149 #if USE_INTERNAL_ALLOC
150 static pthread_mutex_t list_free_lock = PTHREAD_MUTEX_INITIALIZER;
151 #endif
152 #endif /* WITH_PTHREADS */
153
154
155 /************
156 * Macros *
157 ************/
158
159 #ifdef WITH_PTHREADS
160
161 # define list_mutex_init(mutex) \
162 do { \
163 int e = pthread_mutex_init(mutex, NULL); \
164 if (e != 0) { \
165 errno = e; \
166 lsd_fatal_error(__FILE__, __LINE__, "list mutex init"); \
167 abort(); \
168 } \
169 } while (0)
170
171 # define list_mutex_lock(mutex) \
172 do { \
173 int e = pthread_mutex_lock(mutex); \
174 if (e != 0) { \
175 errno = e; \
176 lsd_fatal_error(__FILE__, __LINE__, "list mutex lock"); \
177 abort(); \
178 } \
179 } while (0)
180
181 # define list_mutex_unlock(mutex) \
182 do { \
183 int e = pthread_mutex_unlock(mutex); \
184 if (e != 0) { \
185 errno = e; \
186 lsd_fatal_error(__FILE__, __LINE__, "list mutex unlock"); \
187 abort(); \
188 } \
189 } while (0)
190
191 # define list_mutex_destroy(mutex) \
192 do { \
193 int e = pthread_mutex_destroy(mutex); \
194 if (e != 0) { \
195 errno = e; \
196 lsd_fatal_error(__FILE__, __LINE__, "list mutex destroy"); \
197 abort(); \
198 } \
199 } while (0)
200
201 # ifndef NDEBUG
202 static int list_mutex_is_locked (pthread_mutex_t *mutex);
203 # endif /* !NDEBUG */
204
205 #else /* !WITH_PTHREADS */
206
207 # define list_mutex_init(mutex)
208 # define list_mutex_lock(mutex)
209 # define list_mutex_unlock(mutex)
210 # define list_mutex_destroy(mutex)
211 # define list_mutex_is_locked(mutex) (1)
212
213 #endif /* !WITH_PTHREADS */
214
215
216 /***************
217 * Functions *
218 ***************/
219
220 List
221 list_create (ListDelF f)
222 {
223 List l;
224
225 if (!(l = list_alloc()))
226 return(lsd_nomem_error(__FILE__, __LINE__, "list create"));
227 l->head = NULL;
228 l->tail = &l->head;
229 l->iNext = NULL;
230 l->fDel = f;
231 l->count = 0;
232 list_mutex_init(&l->mutex);
233 assert(l->magic = LIST_MAGIC); /* set magic via assert abuse */
234 return(l);
235 }
236
237
238 void
239 list_destroy (List l)
240 {
241 ListIterator i, iTmp;
242 ListNode p, pTmp;
243
244 assert(l != NULL);
245 list_mutex_lock(&l->mutex);
246 assert(l->magic == LIST_MAGIC);
247 i = l->iNext;
248 while (i) {
249 assert(i->magic == LIST_MAGIC);
250 iTmp = i->iNext;
251 assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
252 list_iterator_free(i);
253 i = iTmp;
254 }
255 p = l->head;
256 while (p) {
257 pTmp = p->next;
258 if (p->data && l->fDel)
259 l->fDel(p->data);
260 list_node_free(p);
261 p = pTmp;
262 }
263 assert(l->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
264 list_mutex_unlock(&l->mutex);
265 list_mutex_destroy(&l->mutex);
266 list_free(l);
267 return;
268 }
269
270
271 int
272 list_is_empty (List l)
273 {
274 int n;
275
276 assert(l != NULL);
277 list_mutex_lock(&l->mutex);
278 assert(l->magic == LIST_MAGIC);
279 n = l->count;
280 list_mutex_unlock(&l->mutex);
281 return(n == 0);
282 }
283
284
285 int
286 list_count (List l)
287 {
288 int n;
289
290 assert(l != NULL);
291 list_mutex_lock(&l->mutex);
292 assert(l->magic == LIST_MAGIC);
293 n = l->count;
294 list_mutex_unlock(&l->mutex);
295 return(n);
296 }
297
298
299 void *
300 list_append (List l, void *x)
301 {
302 void *v;
303
304 assert(l != NULL);
305 assert(x != NULL);
306 list_mutex_lock(&l->mutex);
307 assert(l->magic == LIST_MAGIC);
308 v = list_node_create(l, l->tail, x);
309 list_mutex_unlock(&l->mutex);
310 return(v);
311 }
312
313
314 void *
315 list_prepend (List l, void *x)
316 {
317 void *v;
318
319 assert(l != NULL);
320 assert(x != NULL);
321 list_mutex_lock(&l->mutex);
322 assert(l->magic == LIST_MAGIC);
323 v = list_node_create(l, &l->head, x);
324 list_mutex_unlock(&l->mutex);
325 return(v);
326 }
327
328
329 void *
330 list_find_first (List l, ListFindF f, void *key)
331 {
332 ListNode p;
333 void *v = NULL;
334
335 assert(l != NULL);
336 assert(f != NULL);
337 list_mutex_lock(&l->mutex);
338 assert(l->magic == LIST_MAGIC);
339 for (p=l->head; p; p=p->next) {
340 if (f(p->data, key)) {
341 v = p->data;
342 break;
343 }
344 }
345 list_mutex_unlock(&l->mutex);
346 return(v);
347 }
348
349
350 int
351 list_delete_all (List l, ListFindF f, void *key)
352 {
353 ListNode *pp;
354 void *v;
355 int n = 0;
356
357 assert(l != NULL);
358 assert(f != NULL);
359 list_mutex_lock(&l->mutex);
360 assert(l->magic == LIST_MAGIC);
361 pp = &l->head;
362 while (*pp) {
363 if (f((*pp)->data, key)) {
364 if ((v = list_node_destroy(l, pp))) {
365 if (l->fDel)
366 l->fDel(v);
367 n++;
368 }
369 }
370 else {
371 pp = &(*pp)->next;
372 }
373 }
374 list_mutex_unlock(&l->mutex);
375 return(n);
376 }
377
378
379 int
380 list_for_each (List l, ListForF f, void *arg)
381 {
382 ListNode p;
383 int n = 0;
384
385 assert(l != NULL);
386 assert(f != NULL);
387 list_mutex_lock(&l->mutex);
388 assert(l->magic == LIST_MAGIC);
389 for (p=l->head; p; p=p->next) {
390 n++;
391 if (f(p->data, arg) < 0) {
392 n = -n;
393 break;
394 }
395 }
396 list_mutex_unlock(&l->mutex);
397 return(n);
398 }
399
400
401 void
402 list_sort (List l, ListCmpF f)
403 {
404 /* Note: Time complexity O(n^2).
405 */
406 ListNode *pp, *ppPrev, *ppPos, pTmp;
407 ListIterator i;
408
409 assert(l != NULL);
410 assert(f != NULL);
411 list_mutex_lock(&l->mutex);
412 assert(l->magic == LIST_MAGIC);
413 if (l->count > 1) {
414 ppPrev = &l->head;
415 pp = &(*ppPrev)->next;
416 while (*pp) {
417 if (f((*pp)->data, (*ppPrev)->data) < 0) {
418 ppPos = &l->head;
419 while (f((*pp)->data, (*ppPos)->data) >= 0)
420 ppPos = &(*ppPos)->next;
421 pTmp = (*pp)->next;
422 (*pp)->next = *ppPos;
423 *ppPos = *pp;
424 *pp = pTmp;
425 if (ppPrev == ppPos)
426 ppPrev = &(*ppPrev)->next;
427 }
428 else {
429 ppPrev = pp;
430 pp = &(*pp)->next;
431 }
432 }
433 l->tail = pp;
434
435 for (i=l->iNext; i; i=i->iNext) {
436 assert(i->magic == LIST_MAGIC);
437 i->pos = i->list->head;
438 i->prev = &i->list->head;
439 }
440 }
441 list_mutex_unlock(&l->mutex);
442 return;
443 }
444
445
446 void *
447 list_push (List l, void *x)
448 {
449 void *v;
450
451 assert(l != NULL);
452 assert(x != NULL);
453 list_mutex_lock(&l->mutex);
454 assert(l->magic == LIST_MAGIC);
455 v = list_node_create(l, &l->head, x);
456 list_mutex_unlock(&l->mutex);
457 return(v);
458 }
459
460
461 void *
462 list_pop (List l)
463 {
464 void *v;
465
466 assert(l != NULL);
467 list_mutex_lock(&l->mutex);
468 assert(l->magic == LIST_MAGIC);
469 v = list_node_destroy(l, &l->head);
470 list_mutex_unlock(&l->mutex);
471 return(v);
472 }
473
474
475 void *
476 list_peek (List l)
477 {
478 void *v;
479
480 assert(l != NULL);
481 list_mutex_lock(&l->mutex);
482 assert(l->magic == LIST_MAGIC);
483 v = (l->head) ? l->head->data : NULL;
484 list_mutex_unlock(&l->mutex);
485 return(v);
486 }
487
488
489 void *
490 list_enqueue (List l, void *x)
491 {
492 void *v;
493
494 assert(l != NULL);
495 assert(x != NULL);
496 list_mutex_lock(&l->mutex);
497 assert(l->magic == LIST_MAGIC);
498 v = list_node_create(l, l->tail, x);
499 list_mutex_unlock(&l->mutex);
500 return(v);
501 }
502
503
504 void *
505 list_dequeue (List l)
506 {
507 void *v;
508
509 assert(l != NULL);
510 list_mutex_lock(&l->mutex);
511 assert(l->magic == LIST_MAGIC);
512 v = list_node_destroy(l, &l->head);
513 list_mutex_unlock(&l->mutex);
514 return(v);
515 }
516
517
518 ListIterator
519 list_iterator_create (List l)
520 {
521 ListIterator i;
522
523 assert(l != NULL);
524 if (!(i = list_iterator_alloc()))
525 return(lsd_nomem_error(__FILE__, __LINE__, "list iterator create"));
526 i->list = l;
527 list_mutex_lock(&l->mutex);
528 assert(l->magic == LIST_MAGIC);
529 i->pos = l->head;
530 i->prev = &l->head;
531 i->iNext = l->iNext;
532 l->iNext = i;
533 assert(i->magic = LIST_MAGIC); /* set magic via assert abuse */
534 list_mutex_unlock(&l->mutex);
535 return(i);
536 }
537
538
539 void
540 list_iterator_reset (ListIterator i)
541 {
542 assert(i != NULL);
543 assert(i->magic == LIST_MAGIC);
544 list_mutex_lock(&i->list->mutex);
545 assert(i->list->magic == LIST_MAGIC);
546 i->pos = i->list->head;
547 i->prev = &i->list->head;
548 list_mutex_unlock(&i->list->mutex);
549 return;
550 }
551
552
553 void
554 list_iterator_destroy (ListIterator i)
555 {
556 ListIterator *pi;
557
558 assert(i != NULL);
559 assert(i->magic == LIST_MAGIC);
560 list_mutex_lock(&i->list->mutex);
561 assert(i->list->magic == LIST_MAGIC);
562 for (pi=&i->list->iNext; *pi; pi=&(*pi)->iNext) {
563 assert((*pi)->magic == LIST_MAGIC);
564 if (*pi == i) {
565 *pi = (*pi)->iNext;
566 break;
567 }
568 }
569 list_mutex_unlock(&i->list->mutex);
570 assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
571 list_iterator_free(i);
572 return;
573 }
574
575
576 void *
577 list_next (ListIterator i)
578 {
579 ListNode p;
580
581 assert(i != NULL);
582 assert(i->magic == LIST_MAGIC);
583 list_mutex_lock(&i->list->mutex);
584 assert(i->list->magic == LIST_MAGIC);
585 if ((p = i->pos))
586 i->pos = p->next;
587 if (*i->prev != p)
588 i->prev = &(*i->prev)->next;
589 list_mutex_unlock(&i->list->mutex);
590 return(p ? p->data : NULL);
591 }
592
593
594 void *
595 list_insert (ListIterator i, void *x)
596 {
597 void *v;
598
599 assert(i != NULL);
600 assert(x != NULL);
601 assert(i->magic == LIST_MAGIC);
602 list_mutex_lock(&i->list->mutex);
603 assert(i->list->magic == LIST_MAGIC);
604 v = list_node_create(i->list, i->prev, x);
605 list_mutex_unlock(&i->list->mutex);
606 return(v);
607 }
608
609
610 void *
611 list_find (ListIterator i, ListFindF f, void *key)
612 {
613 void *v;
614
615 assert(i != NULL);
616 assert(f != NULL);
617 assert(i->magic == LIST_MAGIC);
618 while ((v=list_next(i)) && !f(v,key)) {;}
619 return(v);
620 }
621
622
623 void *
624 list_remove (ListIterator i)
625 {
626 void *v = NULL;
627
628 assert(i != NULL);
629 assert(i->magic == LIST_MAGIC);
630 list_mutex_lock(&i->list->mutex);
631 assert(i->list->magic == LIST_MAGIC);
632 if (*i->prev != i->pos)
633 v = list_node_destroy(i->list, i->prev);
634 list_mutex_unlock(&i->list->mutex);
635 return(v);
636 }
637
638
639 int
640 list_delete (ListIterator i)
641 {
642 void *v;
643
644 assert(i != NULL);
645 assert(i->magic == LIST_MAGIC);
646 if ((v = list_remove(i))) {
647 if (i->list->fDel)
648 i->list->fDel(v);
649 return(1);
650 }
651 return(0);
652 }
653
654
655 static void *
656 list_node_create (List l, ListNode *pp, void *x)
657 {
658 /* Inserts data pointed to by [x] into list [l] after [pp],
659 * the address of the previous node's "next" ptr.
660 * Returns a ptr to data [x], or NULL if insertion fails.
661 * This routine assumes the list is already locked upon entry.
662 */
663 ListNode p;
664 ListIterator i;
665
666 assert(l != NULL);
667 assert(l->magic == LIST_MAGIC);
668 assert(list_mutex_is_locked(&l->mutex));
669 assert(pp != NULL);
670 assert(x != NULL);
671 if (!(p = list_node_alloc()))
672 return(lsd_nomem_error(__FILE__, __LINE__, "list node create"));
673 p->data = x;
674 if (!(p->next = *pp))
675 l->tail = &p->next;
676 *pp = p;
677 l->count++;
678 for (i=l->iNext; i; i=i->iNext) {
679 assert(i->magic == LIST_MAGIC);
680 if (i->prev == pp)
681 i->prev = &p->next;
682 else if (i->pos == p->next)
683 i->pos = p;
684 //assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next));
685 }
686 return(x);
687 }
688
689
690 static void *
691 list_node_destroy (List l, ListNode *pp)
692 {
693 /* Removes the node pointed to by [*pp] from from list [l],
694 * where [pp] is the address of the previous node's "next" ptr.
695 * Returns the data ptr associated with list item being removed,
696 * or NULL if [*pp] points to the NULL element.
697 * This routine assumes the list is already locked upon entry.
698 */
699 void *v;
700 ListNode p;
701 ListIterator i;
702
703 assert(l != NULL);
704 assert(l->magic == LIST_MAGIC);
705 assert(list_mutex_is_locked(&l->mutex));
706 assert(pp != NULL);
707 if (!(p = *pp))
708 return(NULL);
709 v = p->data;
710 if (!(*pp = p->next))
711 l->tail = pp;
712 l->count--;
713 for (i=l->iNext; i; i=i->iNext) {
714 assert(i->magic == LIST_MAGIC);
715 if (i->pos == p)
716 i->pos = p->next, i->prev = pp;
717 else if (i->prev == &p->next)
718 i->prev = pp;
719 assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next));
720 }
721 list_node_free(p);
722 return(v);
723 }
724
725
726 static List
727 list_alloc (void)
728 {
729 return(list_alloc_aux(sizeof(struct list), &list_free_lists));
730 }
731
732
733 static void
734 list_free (List l)
735 {
736 list_free_aux(l, &list_free_lists);
737 return;
738 }
739
740
741 static ListNode
742 list_node_alloc (void)
743 {
744 return(list_alloc_aux(sizeof(struct listNode), &list_free_nodes));
745 }
746
747
748 static void
749 list_node_free (ListNode p)
750 {
751 list_free_aux(p, &list_free_nodes);
752 return;
753 }
754
755
756 static ListIterator
757 list_iterator_alloc (void)
758 {
759 return(list_alloc_aux(sizeof(struct listIterator), &list_free_iterators));
760 }
761
762
763 static void
764 list_iterator_free (ListIterator i)
765 {
766 list_free_aux(i, &list_free_iterators);
767 return;
768 }
769
770 #if USE_INTERNAL_ALLOC
771 static void *
772 list_alloc_aux (int size, void *pfreelist)
773 {
774 /* Allocates an object of [size] bytes from the freelist [*pfreelist].
775 * Memory is added to the freelist in chunks of size LIST_ALLOC.
776 * Returns a ptr to the object, or NULL if the memory request fails.
777 */
778 void **px;
779 void **pfree = pfreelist;
780 void **plast;
781
782 assert(sizeof(char) == 1);
783 assert(size >= sizeof(void *));
784 assert(pfreelist != NULL);
785 assert(LIST_ALLOC > 0);
786 list_mutex_lock(&list_free_lock);
787 if (!*pfree) {
788 if ((*pfree = malloc(LIST_ALLOC * size))) {
789 px = *pfree;
790 plast = (void **) ((char *) *pfree + ((LIST_ALLOC - 1) * size));
791 while (px < plast)
792 *px = (char *) px + size, px = *px;
793 *plast = NULL;
794 }
795 }
796 if ((px = *pfree))
797 *pfree = *px;
798 else
799 errno = ENOMEM;
800 list_mutex_unlock(&list_free_lock);
801 return(px);
802 }
803
804
805 static void
806 list_free_aux (void *x, void *pfreelist)
807 {
808 /* Frees the object [x], returning it to the freelist [*pfreelist].
809 */
810 void **px = x;
811 void **pfree = pfreelist;
812
813 assert(x != NULL);
814 assert(pfreelist != NULL);
815 list_mutex_lock(&list_free_lock);
816 *px = *pfree;
817 *pfree = px;
818 list_mutex_unlock(&list_free_lock);
819 return;
820 }
821 #else
822 static void *
823 list_alloc_aux (int size, void *pfreelist)
824 {
825 return malloc (size);
826 }
827
828 static void
829 list_free_aux (void *x, void *pfreelist)
830 {
831 free (x);
832 }
833 #endif
834
835
836 #ifndef NDEBUG
837 #ifdef WITH_PTHREADS
838 static int
839 list_mutex_is_locked (pthread_mutex_t *mutex)
840 {
841 /* Returns true if the mutex is locked; o/w, returns false.
842 */
843 int rc;
844
845 assert(mutex != NULL);
846 rc = pthread_mutex_trylock(mutex);
847 return(rc == EBUSY ? 1 : 0);
848 }
849 #endif /* WITH_PTHREADS */
850 #endif /* !NDEBUG */