Merge branch 'vendor/GCC'
[dragonfly.git] / contrib / bind-9.3 / lib / bind / include / isc / list.h
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1997,1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 #ifndef LIST_H
19 #define LIST_H 1
20 #ifdef _LIBC
21 #include <assert.h>
22 #define INSIST(cond)    assert(cond)
23 #else
24 #include <isc/assertions.h>
25 #endif
26
27 #define LIST(type) struct { type *head, *tail; }
28 #define INIT_LIST(list) \
29         do { (list).head = NULL; (list).tail = NULL; } while (0)
30
31 #define LINK(type) struct { type *prev, *next; }
32 #define INIT_LINK_TYPE(elt, link, type) \
33         do { \
34                 (elt)->link.prev = (type *)(-1); \
35                 (elt)->link.next = (type *)(-1); \
36         } while (0)
37 #define INIT_LINK(elt, link) \
38         INIT_LINK_TYPE(elt, link, void)
39 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
40
41 #define HEAD(list) ((list).head)
42 #define TAIL(list) ((list).tail)
43 #define EMPTY(list) ((list).head == NULL)
44
45 #define PREPEND(list, elt, link) \
46         do { \
47                 INSIST(!LINKED(elt, link));\
48                 if ((list).head != NULL) \
49                         (list).head->link.prev = (elt); \
50                 else \
51                         (list).tail = (elt); \
52                 (elt)->link.prev = NULL; \
53                 (elt)->link.next = (list).head; \
54                 (list).head = (elt); \
55         } while (0)
56
57 #define APPEND(list, elt, link) \
58         do { \
59                 INSIST(!LINKED(elt, link));\
60                 if ((list).tail != NULL) \
61                         (list).tail->link.next = (elt); \
62                 else \
63                         (list).head = (elt); \
64                 (elt)->link.prev = (list).tail; \
65                 (elt)->link.next = NULL; \
66                 (list).tail = (elt); \
67         } while (0)
68
69 #define UNLINK_TYPE(list, elt, link, type) \
70         do { \
71                 INSIST(LINKED(elt, link));\
72                 if ((elt)->link.next != NULL) \
73                         (elt)->link.next->link.prev = (elt)->link.prev; \
74                 else { \
75                         INSIST((list).tail == (elt)); \
76                         (list).tail = (elt)->link.prev; \
77                 } \
78                 if ((elt)->link.prev != NULL) \
79                         (elt)->link.prev->link.next = (elt)->link.next; \
80                 else { \
81                         INSIST((list).head == (elt)); \
82                         (list).head = (elt)->link.next; \
83                 } \
84                 INIT_LINK_TYPE(elt, link, type); \
85         } while (0)
86 #define UNLINK(list, elt, link) \
87         UNLINK_TYPE(list, elt, link, void)
88
89 #define PREV(elt, link) ((elt)->link.prev)
90 #define NEXT(elt, link) ((elt)->link.next)
91
92 #define INSERT_BEFORE(list, before, elt, link) \
93         do { \
94                 INSIST(!LINKED(elt, link));\
95                 if ((before)->link.prev == NULL) \
96                         PREPEND(list, elt, link); \
97                 else { \
98                         (elt)->link.prev = (before)->link.prev; \
99                         (before)->link.prev = (elt); \
100                         (elt)->link.prev->link.next = (elt); \
101                         (elt)->link.next = (before); \
102                 } \
103         } while (0)
104
105 #define INSERT_AFTER(list, after, elt, link) \
106         do { \
107                 INSIST(!LINKED(elt, link));\
108                 if ((after)->link.next == NULL) \
109                         APPEND(list, elt, link); \
110                 else { \
111                         (elt)->link.next = (after)->link.next; \
112                         (after)->link.next = (elt); \
113                         (elt)->link.next->link.prev = (elt); \
114                         (elt)->link.prev = (after); \
115                 } \
116         } while (0)
117
118 #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
119 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)
120
121 #endif /* LIST_H */