2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1997,1999 by Internet Software Consortium.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #define INSIST(cond) assert(cond)
28 #include <isc/assertions.h>
31 #define LIST(type) struct { type *head, *tail; }
32 #define INIT_LIST(list) \
33 do { (list).head = NULL; (list).tail = NULL; } while (0)
35 #define LINK(type) struct { type *prev, *next; }
36 #define INIT_LINK_TYPE(elt, link, type) \
38 (elt)->link.prev = (type *)(-1); \
39 (elt)->link.next = (type *)(-1); \
41 #define INIT_LINK(elt, link) \
42 INIT_LINK_TYPE(elt, link, void)
43 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1) && \
44 (void *)((elt)->link.next) != (void *)(-1))
46 #define HEAD(list) ((list).head)
47 #define TAIL(list) ((list).tail)
48 #define EMPTY(list) ((list).head == NULL)
50 #define PREPEND(list, elt, link) \
52 INSIST(!LINKED(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 APPEND(list, elt, link) \
64 INSIST(!LINKED(elt, link));\
65 if ((list).tail != NULL) \
66 (list).tail->link.next = (elt); \
68 (list).head = (elt); \
69 (elt)->link.prev = (list).tail; \
70 (elt)->link.next = NULL; \
71 (list).tail = (elt); \
74 #define UNLINK_TYPE(list, elt, link, type) \
76 INSIST(LINKED(elt, link));\
77 if ((elt)->link.next != NULL) \
78 (elt)->link.next->link.prev = (elt)->link.prev; \
80 INSIST((list).tail == (elt)); \
81 (list).tail = (elt)->link.prev; \
83 if ((elt)->link.prev != NULL) \
84 (elt)->link.prev->link.next = (elt)->link.next; \
86 INSIST((list).head == (elt)); \
87 (list).head = (elt)->link.next; \
89 INIT_LINK_TYPE(elt, link, type); \
91 #define UNLINK(list, elt, link) \
92 UNLINK_TYPE(list, elt, link, void)
94 #define PREV(elt, link) ((elt)->link.prev)
95 #define NEXT(elt, link) ((elt)->link.next)
97 #define INSERT_BEFORE(list, before, elt, link) \
99 INSIST(!LINKED(elt, link));\
100 if ((before)->link.prev == NULL) \
101 PREPEND(list, elt, link); \
103 (elt)->link.prev = (before)->link.prev; \
104 (before)->link.prev = (elt); \
105 (elt)->link.prev->link.next = (elt); \
106 (elt)->link.next = (before); \
110 #define INSERT_AFTER(list, after, elt, link) \
112 INSIST(!LINKED(elt, link));\
113 if ((after)->link.next == NULL) \
114 APPEND(list, elt, link); \
116 (elt)->link.next = (after)->link.next; \
117 (after)->link.next = (elt); \
118 (elt)->link.next->link.prev = (elt); \
119 (elt)->link.prev = (after); \
123 #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
124 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)