Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / includes / isc-dhcp / list.h
1 /*
2  * Copyright (C) 1997, 1999  Internet Software Consortium.
3  * 
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  * 
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17
18 #ifndef ISC_LIST_H
19 #define ISC_LIST_H 1
20
21 #define ISC_LIST(type) struct { type *head, *tail; }
22 #define ISC_LIST_INIT(list) \
23         do { (list).head = NULL; (list).tail = NULL; } while (0)
24
25 #define ISC_LINK(type) struct { type *prev, *next; }
26 #define ISC_LINK_INIT(elt, link) \
27         do { \
28                 (elt)->link.prev = (void *)(-1); \
29                 (elt)->link.next = (void *)(-1); \
30         } while (0)
31 #define ISC_LINK_LINKED(elt, link) ((elt)->link.prev != (void *)(-1))
32
33 #define ISC_LIST_HEAD(list) ((list).head)
34 #define ISC_LIST_TAIL(list) ((list).tail)
35 #define ISC_LIST_EMPTY(list) ((list).head == NULL)
36
37 #define ISC_LIST_PREPEND(list, elt, link) \
38         do { \
39                 if ((list).head != NULL) \
40                         (list).head->link.prev = (elt); \
41                 else \
42                         (list).tail = (elt); \
43                 (elt)->link.prev = NULL; \
44                 (elt)->link.next = (list).head; \
45                 (list).head = (elt); \
46         } while (0)
47
48 #define ISC_LIST_APPEND(list, elt, link) \
49         do { \
50                 if ((list).tail != NULL) \
51                         (list).tail->link.next = (elt); \
52                 else \
53                         (list).head = (elt); \
54                 (elt)->link.prev = (list).tail; \
55                 (elt)->link.next = NULL; \
56                 (list).tail = (elt); \
57         } while (0)
58
59 #define ISC_LIST_UNLINK(list, elt, link) \
60         do { \
61                 if ((elt)->link.next != NULL) \
62                         (elt)->link.next->link.prev = (elt)->link.prev; \
63                 else \
64                         (list).tail = (elt)->link.prev; \
65                 if ((elt)->link.prev != NULL) \
66                         (elt)->link.prev->link.next = (elt)->link.next; \
67                 else \
68                         (list).head = (elt)->link.next; \
69                 (elt)->link.prev = (void *)(-1); \
70                 (elt)->link.next = (void *)(-1); \
71         } while (0)
72
73 #define ISC_LIST_PREV(elt, link) ((elt)->link.prev)
74 #define ISC_LIST_NEXT(elt, link) ((elt)->link.next)
75
76 #define ISC_LIST_INSERTBEFORE(list, before, elt, link) \
77         do { \
78                 if ((before)->link.prev == NULL) \
79                         ISC_LIST_PREPEND(list, elt, link); \
80                 else { \
81                         (elt)->link.prev = (before)->link.prev; \
82                         (before)->link.prev = (elt); \
83                         (elt)->link.prev->link.next = (elt); \
84                         (elt)->link.next = (before); \
85                 } \
86         } while (0)
87
88 #define ISC_LIST_INSERTAFTER(list, after, elt, link) \
89         do { \
90                 if ((after)->link.next == NULL) \
91                         ISC_LIST_APPEND(list, elt, link); \
92                 else { \
93                         (elt)->link.next = (after)->link.next; \
94                         (after)->link.next = (elt); \
95                         (elt)->link.next->link.prev = (elt); \
96                         (elt)->link.prev = (after); \
97                 } \
98         } while (0)
99
100 #define ISC_LIST_APPENDLIST(list1, list2, link) \
101         do { \
102                 if (ISC_LIST_EMPTY(list1)) \
103                         (list1) = (list2); \
104                 else if (!ISC_LIST_EMPTY(list2)) { \
105                         (list1).tail->link.next = (list2).head; \
106                         (list2).head->link.prev = (list1).tail; \
107                         (list1).tail = (list2).tail; \
108                         (list2).head = NULL; \
109                         (list2).tail = NULL; \
110                 } \
111         } while (0)
112
113 #define ISC_LIST_ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
114 #define ISC_LIST_DEQUEUE(list, elt, link) ISC_LIST_UNLINK(list, elt, link)
115
116 #endif /* ISC_LIST_H */