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