2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1997,1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #define INSIST(cond) assert(cond)
24 #include <isc/assertions.h>
26 #define LIST(type) struct { type *head, *tail; }
27 #define INIT_LIST(list) \
28 do { (list).head = NULL; (list).tail = NULL; } while (0)
30 #define LINK(type) struct { type *prev, *next; }
31 #define INIT_LINK_TYPE(elt, link, type) \
33 (elt)->link.prev = (type *)(-1); \
34 (elt)->link.next = (type *)(-1); \
36 #define INIT_LINK(elt, link) \
37 INIT_LINK_TYPE(elt, link, void)
38 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
40 #define HEAD(list) ((list).head)
41 #define TAIL(list) ((list).tail)
42 #define EMPTY(list) ((list).head == NULL)
44 #define PREPEND(list, elt, link) \
46 INSIST(!LINKED(elt, link));\
47 if ((list).head != NULL) \
48 (list).head->link.prev = (elt); \
50 (list).tail = (elt); \
51 (elt)->link.prev = NULL; \
52 (elt)->link.next = (list).head; \
53 (list).head = (elt); \
56 #define APPEND(list, elt, link) \
58 INSIST(!LINKED(elt, link));\
59 if ((list).tail != NULL) \
60 (list).tail->link.next = (elt); \
62 (list).head = (elt); \
63 (elt)->link.prev = (list).tail; \
64 (elt)->link.next = NULL; \
65 (list).tail = (elt); \
68 #define UNLINK_TYPE(list, elt, link, type) \
70 INSIST(LINKED(elt, link));\
71 if ((elt)->link.next != NULL) \
72 (elt)->link.next->link.prev = (elt)->link.prev; \
74 INSIST((list).tail == (elt)); \
75 (list).tail = (elt)->link.prev; \
77 if ((elt)->link.prev != NULL) \
78 (elt)->link.prev->link.next = (elt)->link.next; \
80 INSIST((list).head == (elt)); \
81 (list).head = (elt)->link.next; \
83 INIT_LINK_TYPE(elt, link, type); \
85 #define UNLINK(list, elt, link) \
86 UNLINK_TYPE(list, elt, link, void)
88 #define PREV(elt, link) ((elt)->link.prev)
89 #define NEXT(elt, link) ((elt)->link.next)
91 #define INSERT_BEFORE(list, before, elt, link) \
93 INSIST(!LINKED(elt, link));\
94 if ((before)->link.prev == NULL) \
95 PREPEND(list, elt, link); \
97 (elt)->link.prev = (before)->link.prev; \
98 (before)->link.prev = (elt); \
99 (elt)->link.prev->link.next = (elt); \
100 (elt)->link.next = (before); \
104 #define INSERT_AFTER(list, after, elt, link) \
106 INSIST(!LINKED(elt, link));\
107 if ((after)->link.next == NULL) \
108 APPEND(list, elt, link); \
110 (elt)->link.next = (after)->link.next; \
111 (after)->link.next = (elt); \
112 (elt)->link.next->link.prev = (elt); \
113 (elt)->link.prev = (after); \
117 #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
118 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)