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