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>
27 #define LIST(type) struct { type *head, *tail; }
28 #define INIT_LIST(list) \
29 do { (list).head = NULL; (list).tail = NULL; } while (0)
31 #define LINK(type) struct { type *prev, *next; }
32 #define INIT_LINK_TYPE(elt, link, type) \
34 (elt)->link.prev = (type *)(-1); \
35 (elt)->link.next = (type *)(-1); \
37 #define INIT_LINK(elt, link) \
38 INIT_LINK_TYPE(elt, link, void)
39 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
41 #define HEAD(list) ((list).head)
42 #define TAIL(list) ((list).tail)
43 #define EMPTY(list) ((list).head == NULL)
45 #define PREPEND(list, elt, link) \
47 INSIST(!LINKED(elt, link));\
48 if ((list).head != NULL) \
49 (list).head->link.prev = (elt); \
51 (list).tail = (elt); \
52 (elt)->link.prev = NULL; \
53 (elt)->link.next = (list).head; \
54 (list).head = (elt); \
57 #define APPEND(list, elt, link) \
59 INSIST(!LINKED(elt, link));\
60 if ((list).tail != NULL) \
61 (list).tail->link.next = (elt); \
63 (list).head = (elt); \
64 (elt)->link.prev = (list).tail; \
65 (elt)->link.next = NULL; \
66 (list).tail = (elt); \
69 #define UNLINK_TYPE(list, elt, link, type) \
71 INSIST(LINKED(elt, link));\
72 if ((elt)->link.next != NULL) \
73 (elt)->link.next->link.prev = (elt)->link.prev; \
75 INSIST((list).tail == (elt)); \
76 (list).tail = (elt)->link.prev; \
78 if ((elt)->link.prev != NULL) \
79 (elt)->link.prev->link.next = (elt)->link.next; \
81 INSIST((list).head == (elt)); \
82 (list).head = (elt)->link.next; \
84 INIT_LINK_TYPE(elt, link, type); \
86 #define UNLINK(list, elt, link) \
87 UNLINK_TYPE(list, elt, link, void)
89 #define PREV(elt, link) ((elt)->link.prev)
90 #define NEXT(elt, link) ((elt)->link.next)
92 #define INSERT_BEFORE(list, before, elt, link) \
94 INSIST(!LINKED(elt, link));\
95 if ((before)->link.prev == NULL) \
96 PREPEND(list, elt, link); \
98 (elt)->link.prev = (before)->link.prev; \
99 (before)->link.prev = (elt); \
100 (elt)->link.prev->link.next = (elt); \
101 (elt)->link.next = (before); \
105 #define INSERT_AFTER(list, after, elt, link) \
107 INSIST(!LINKED(elt, link));\
108 if ((after)->link.next == NULL) \
109 APPEND(list, elt, link); \
111 (elt)->link.next = (after)->link.next; \
112 (after)->link.next = (elt); \
113 (elt)->link.next->link.prev = (elt); \
114 (elt)->link.prev = (after); \
118 #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
119 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)