4 * Copyright (C) 1997-2001 Internet Software Consortium.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
11 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Id: list.h,v 1.19 2002/05/09 07:09:30 marka Exp */
24 #include <isc/boolean.h>
25 #include <isc/assertions.h>
27 #ifdef ISC_LIST_CHECKINIT
28 #define ISC_LINK_INSIST(x) ISC_INSIST(x)
30 #define ISC_LINK_INSIST(x)
33 #define ISC_LIST(type) struct { type *head, *tail; }
34 #define ISC_LIST_INIT(list) \
35 do { (list).head = NULL; (list).tail = NULL; } while (0)
37 #define ISC_LINK(type) struct { type *prev, *next; }
38 #define ISC_LINK_INIT_TYPE(elt, link, type) \
40 (elt)->link.prev = (type *)(-1); \
41 (elt)->link.next = (type *)(-1); \
43 #define ISC_LINK_INIT(elt, link) \
44 ISC_LINK_INIT_TYPE(elt, link, void)
45 #define ISC_LINK_LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
47 #define ISC_LIST_HEAD(list) ((list).head)
48 #define ISC_LIST_TAIL(list) ((list).tail)
49 #define ISC_LIST_EMPTY(list) ISC_TF((list).head == NULL)
51 #define __ISC_LIST_PREPENDUNSAFE(list, elt, link) \
53 if ((list).head != NULL) \
54 (list).head->link.prev = (elt); \
56 (list).tail = (elt); \
57 (elt)->link.prev = NULL; \
58 (elt)->link.next = (list).head; \
59 (list).head = (elt); \
62 #define ISC_LIST_PREPEND(list, elt, link) \
64 ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
65 __ISC_LIST_PREPENDUNSAFE(list, elt, link); \
68 #define ISC_LIST_INITANDPREPEND(list, elt, link) \
69 __ISC_LIST_PREPENDUNSAFE(list, elt, link)
71 #define __ISC_LIST_APPENDUNSAFE(list, elt, link) \
73 if ((list).tail != NULL) \
74 (list).tail->link.next = (elt); \
76 (list).head = (elt); \
77 (elt)->link.prev = (list).tail; \
78 (elt)->link.next = NULL; \
79 (list).tail = (elt); \
82 #define ISC_LIST_APPEND(list, elt, link) \
84 ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
85 __ISC_LIST_APPENDUNSAFE(list, elt, link); \
88 #define ISC_LIST_INITANDAPPEND(list, elt, link) \
89 __ISC_LIST_APPENDUNSAFE(list, elt, link)
91 #define __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type) \
93 if ((elt)->link.next != NULL) \
94 (elt)->link.next->link.prev = (elt)->link.prev; \
96 (list).tail = (elt)->link.prev; \
97 if ((elt)->link.prev != NULL) \
98 (elt)->link.prev->link.next = (elt)->link.next; \
100 (list).head = (elt)->link.next; \
101 (elt)->link.prev = (type *)(-1); \
102 (elt)->link.next = (type *)(-1); \
105 #define __ISC_LIST_UNLINKUNSAFE(list, elt, link) \
106 __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void)
108 #define ISC_LIST_UNLINK_TYPE(list, elt, link, type) \
110 ISC_LINK_INSIST(ISC_LINK_LINKED(elt, link)); \
111 __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type); \
113 #define ISC_LIST_UNLINK(list, elt, link) \
114 ISC_LIST_UNLINK_TYPE(list, elt, link, void)
116 #define ISC_LIST_PREV(elt, link) ((elt)->link.prev)
117 #define ISC_LIST_NEXT(elt, link) ((elt)->link.next)
119 #define __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link) \
121 if ((before)->link.prev == NULL) \
122 ISC_LIST_PREPEND(list, elt, link); \
124 (elt)->link.prev = (before)->link.prev; \
125 (before)->link.prev = (elt); \
126 (elt)->link.prev->link.next = (elt); \
127 (elt)->link.next = (before); \
131 #define ISC_LIST_INSERTBEFORE(list, before, elt, link) \
133 ISC_LINK_INSIST(ISC_LINK_LINKED(before, link)); \
134 ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
135 __ISC_LIST_INSERTBEFOREUNSAFE(list, before, elt, link); \
138 #define __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link) \
140 if ((after)->link.next == NULL) \
141 ISC_LIST_APPEND(list, elt, link); \
143 (elt)->link.next = (after)->link.next; \
144 (after)->link.next = (elt); \
145 (elt)->link.next->link.prev = (elt); \
146 (elt)->link.prev = (after); \
150 #define ISC_LIST_INSERTAFTER(list, after, elt, link) \
152 ISC_LINK_INSIST(ISC_LINK_LINKED(after, link)); \
153 ISC_LINK_INSIST(!ISC_LINK_LINKED(elt, link)); \
154 __ISC_LIST_INSERTAFTERUNSAFE(list, after, elt, link); \
157 #define ISC_LIST_APPENDLIST(list1, list2, link) \
159 if (ISC_LIST_EMPTY(list1)) \
161 else if (!ISC_LIST_EMPTY(list2)) { \
162 (list1).tail->link.next = (list2).head; \
163 (list2).head->link.prev = (list1).tail; \
164 (list1).tail = (list2).tail; \
166 (list2).head = NULL; \
167 (list2).tail = NULL; \
170 #define ISC_LIST_ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
171 #define __ISC_LIST_ENQUEUEUNSAFE(list, elt, link) \
172 __ISC_LIST_APPENDUNSAFE(list, elt, link)
173 #define ISC_LIST_DEQUEUE(list, elt, link) \
174 ISC_LIST_UNLINK_TYPE(list, elt, link, void)
175 #define ISC_LIST_DEQUEUE_TYPE(list, elt, link, type) \
176 ISC_LIST_UNLINK_TYPE(list, elt, link, type)
177 #define __ISC_LIST_DEQUEUEUNSAFE(list, elt, link) \
178 __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, void)
179 #define __ISC_LIST_DEQUEUEUNSAFE_TYPE(list, elt, link, type) \
180 __ISC_LIST_UNLINKUNSAFE_TYPE(list, elt, link, type)
182 #endif /* ISC_LIST_H */