Merge from vendor branch SENDMAIL:
[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 #include <isc/assertions.h>
21
22 #define LIST(type) struct { type *head, *tail; }
23 #define INIT_LIST(list) \
24         do { (list).head = NULL; (list).tail = NULL; } while (0)
25
26 #define LINK(type) struct { type *prev, *next; }
27 #define INIT_LINK_TYPE(elt, link, type) \
28         do { \
29                 (elt)->link.prev = (type *)(-1); \
30                 (elt)->link.next = (type *)(-1); \
31         } while (0)
32 #define INIT_LINK(elt, link) \
33         INIT_LINK_TYPE(elt, link, void)
34 #define LINKED(elt, link) ((void *)((elt)->link.prev) != (void *)(-1))
35
36 #define HEAD(list) ((list).head)
37 #define TAIL(list) ((list).tail)
38 #define EMPTY(list) ((list).head == NULL)
39
40 #define PREPEND(list, elt, link) \
41         do { \
42                 INSIST(!LINKED(elt, link));\
43                 if ((list).head != NULL) \
44                         (list).head->link.prev = (elt); \
45                 else \
46                         (list).tail = (elt); \
47                 (elt)->link.prev = NULL; \
48                 (elt)->link.next = (list).head; \
49                 (list).head = (elt); \
50         } while (0)
51
52 #define APPEND(list, elt, link) \
53         do { \
54                 INSIST(!LINKED(elt, link));\
55                 if ((list).tail != NULL) \
56                         (list).tail->link.next = (elt); \
57                 else \
58                         (list).head = (elt); \
59                 (elt)->link.prev = (list).tail; \
60                 (elt)->link.next = NULL; \
61                 (list).tail = (elt); \
62         } while (0)
63
64 #define UNLINK_TYPE(list, elt, link, type) \
65         do { \
66                 INSIST(LINKED(elt, link));\
67                 if ((elt)->link.next != NULL) \
68                         (elt)->link.next->link.prev = (elt)->link.prev; \
69                 else { \
70                         INSIST((list).tail == (elt)); \
71                         (list).tail = (elt)->link.prev; \
72                 } \
73                 if ((elt)->link.prev != NULL) \
74                         (elt)->link.prev->link.next = (elt)->link.next; \
75                 else { \
76                         INSIST((list).head == (elt)); \
77                         (list).head = (elt)->link.next; \
78                 } \
79                 INIT_LINK_TYPE(elt, link, type); \
80         } while (0)
81 #define UNLINK(list, elt, link) \
82         UNLINK_TYPE(list, elt, link, void)
83
84 #define PREV(elt, link) ((elt)->link.prev)
85 #define NEXT(elt, link) ((elt)->link.next)
86
87 #define INSERT_BEFORE(list, before, elt, link) \
88         do { \
89                 INSIST(!LINKED(elt, link));\
90                 if ((before)->link.prev == NULL) \
91                         PREPEND(list, elt, link); \
92                 else { \
93                         (elt)->link.prev = (before)->link.prev; \
94                         (before)->link.prev = (elt); \
95                         (elt)->link.prev->link.next = (elt); \
96                         (elt)->link.next = (before); \
97                 } \
98         } while (0)
99
100 #define INSERT_AFTER(list, after, elt, link) \
101         do { \
102                 INSIST(!LINKED(elt, link));\
103                 if ((after)->link.next == NULL) \
104                         APPEND(list, elt, link); \
105                 else { \
106                         (elt)->link.next = (after)->link.next; \
107                         (after)->link.next = (elt); \
108                         (elt)->link.next->link.prev = (elt); \
109                         (elt)->link.prev = (after); \
110                 } \
111         } while (0)
112
113 #define ENQUEUE(list, elt, link) APPEND(list, elt, link)
114 #define DEQUEUE(list, elt, link) UNLINK(list, elt, link)
115
116 #endif /* LIST_H */