]>
Commit | Line | Data |
---|---|---|
997358a6 MW |
1 | /* |
2 | * Copyright (C) 1997-2001 Internet Software Consortium. | |
3 | * | |
4 | * Permission to use, copy, modify, and distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM | |
9 | * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
10 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL | |
11 | * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, | |
12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | |
13 | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | |
14 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | |
15 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | /* $Id: list.h,v 1.1 2004/03/15 20:35:25 as Exp $ */ | |
19 | ||
20 | #ifndef LWRES_LIST_H | |
21 | #define LWRES_LIST_H 1 | |
22 | ||
23 | #define LWRES_LIST(type) struct { type *head, *tail; } | |
24 | #define LWRES_LIST_INIT(list) \ | |
25 | do { (list).head = NULL; (list).tail = NULL; } while (0) | |
26 | ||
27 | #define LWRES_LINK(type) struct { type *prev, *next; } | |
28 | #define LWRES_LINK_INIT(elt, link) \ | |
29 | do { \ | |
30 | (elt)->link.prev = (void *)(-1); \ | |
31 | (elt)->link.next = (void *)(-1); \ | |
32 | } while (0) | |
33 | #define LWRES_LINK_LINKED(elt, link) \ | |
34 | ((void *)((elt)->link.prev) != (void *)(-1)) | |
35 | ||
36 | #define LWRES_LIST_HEAD(list) ((list).head) | |
37 | #define LWRES_LIST_TAIL(list) ((list).tail) | |
38 | #define LWRES_LIST_EMPTY(list) LWRES_TF((list).head == NULL) | |
39 | ||
40 | #define LWRES_LIST_PREPEND(list, elt, link) \ | |
41 | do { \ | |
42 | if ((list).head != NULL) \ | |
43 | (list).head->link.prev = (elt); \ | |
44 | else \ | |
45 | (list).tail = (elt); \ | |
46 | (elt)->link.prev = NULL; \ | |
47 | (elt)->link.next = (list).head; \ | |
48 | (list).head = (elt); \ | |
49 | } while (0) | |
50 | ||
51 | #define LWRES_LIST_APPEND(list, elt, link) \ | |
52 | do { \ | |
53 | if ((list).tail != NULL) \ | |
54 | (list).tail->link.next = (elt); \ | |
55 | else \ | |
56 | (list).head = (elt); \ | |
57 | (elt)->link.prev = (list).tail; \ | |
58 | (elt)->link.next = NULL; \ | |
59 | (list).tail = (elt); \ | |
60 | } while (0) | |
61 | ||
62 | #define LWRES_LIST_UNLINK(list, elt, link) \ | |
63 | do { \ | |
64 | if ((elt)->link.next != NULL) \ | |
65 | (elt)->link.next->link.prev = (elt)->link.prev; \ | |
66 | else \ | |
67 | (list).tail = (elt)->link.prev; \ | |
68 | if ((elt)->link.prev != NULL) \ | |
69 | (elt)->link.prev->link.next = (elt)->link.next; \ | |
70 | else \ | |
71 | (list).head = (elt)->link.next; \ | |
72 | (elt)->link.prev = (void *)(-1); \ | |
73 | (elt)->link.next = (void *)(-1); \ | |
74 | } while (0) | |
75 | ||
76 | #define LWRES_LIST_PREV(elt, link) ((elt)->link.prev) | |
77 | #define LWRES_LIST_NEXT(elt, link) ((elt)->link.next) | |
78 | ||
79 | #define LWRES_LIST_INSERTBEFORE(list, before, elt, link) \ | |
80 | do { \ | |
81 | if ((before)->link.prev == NULL) \ | |
82 | LWRES_LIST_PREPEND(list, elt, link); \ | |
83 | else { \ | |
84 | (elt)->link.prev = (before)->link.prev; \ | |
85 | (before)->link.prev = (elt); \ | |
86 | (elt)->link.prev->link.next = (elt); \ | |
87 | (elt)->link.next = (before); \ | |
88 | } \ | |
89 | } while (0) | |
90 | ||
91 | #define LWRES_LIST_INSERTAFTER(list, after, elt, link) \ | |
92 | do { \ | |
93 | if ((after)->link.next == NULL) \ | |
94 | LWRES_LIST_APPEND(list, elt, link); \ | |
95 | else { \ | |
96 | (elt)->link.next = (after)->link.next; \ | |
97 | (after)->link.next = (elt); \ | |
98 | (elt)->link.next->link.prev = (elt); \ | |
99 | (elt)->link.prev = (after); \ | |
100 | } \ | |
101 | } while (0) | |
102 | ||
103 | #define LWRES_LIST_APPENDLIST(list1, list2, link) \ | |
104 | do { \ | |
105 | if (LWRES_LIST_EMPTY(list1)) \ | |
106 | (list1) = (list2); \ | |
107 | else if (!LWRES_LIST_EMPTY(list2)) { \ | |
108 | (list1).tail->link.next = (list2).head; \ | |
109 | (list2).head->link.prev = (list1).tail; \ | |
110 | (list1).tail = (list2).tail; \ | |
111 | } \ | |
112 | (list2).head = NULL; \ | |
113 | (list2).tail = NULL; \ | |
114 | } while (0) | |
115 | ||
116 | #define LWRES_LIST_ENQUEUE(list, elt, link) LWRES_LIST_APPEND(list, elt, link) | |
117 | #define LWRES_LIST_DEQUEUE(list, elt, link) LWRES_LIST_UNLINK(list, elt, link) | |
118 | ||
119 | #endif /* LWRES_LIST_H */ |