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