1 /*	$NetBSD: list.h,v 1.5 2014/12/10 04:38:02 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1997-2001  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or 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 WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: list.h,v 1.14 2007/06/19 23:47:23 tbox Exp  */
21 
22 #ifndef LWRES_LIST_H
23 #define LWRES_LIST_H 1
24 
25 /*! \file lwres/list.h */
26 
27 #define LWRES_LIST(type) struct { type *head, *tail; }
28 #define LWRES_LIST_INIT(list) \
29 	do { (list).head = NULL; (list).tail = NULL; } while (/*CONSTCOND*/0)
30 
31 #define LWRES_LINK(type) struct { type *prev, *next; }
32 #define LWRES_LINK_INIT(elt, link) \
33 	do { \
34 		(elt)->link.prev = (void *)(-1); \
35 		(elt)->link.next = (void *)(-1); \
36 	} while (/*CONSTCOND*/0)
37 #define LWRES_LINK_LINKED(elt, link) \
38 	((void *)((elt)->link.prev) != (void *)(-1))
39 
40 #define LWRES_LIST_HEAD(list) ((list).head)
41 #define LWRES_LIST_TAIL(list) ((list).tail)
42 #define LWRES_LIST_EMPTY(list) LWRES_TF((list).head == NULL)
43 
44 #define LWRES_LIST_PREPEND(list, elt, link) \
45 	do { \
46 		if ((list).head != NULL) \
47 			(list).head->link.prev = (elt); \
48 		else \
49 			(list).tail = (elt); \
50 		(elt)->link.prev = NULL; \
51 		(elt)->link.next = (list).head; \
52 		(list).head = (elt); \
53 	} while (/*CONSTCOND*/0)
54 
55 #define LWRES_LIST_APPEND(list, elt, link) \
56 	do { \
57 		if ((list).tail != NULL) \
58 			(list).tail->link.next = (elt); \
59 		else \
60 			(list).head = (elt); \
61 		(elt)->link.prev = (list).tail; \
62 		(elt)->link.next = NULL; \
63 		(list).tail = (elt); \
64 	} while (/*CONSTCOND*/0)
65 
66 #define LWRES_LIST_UNLINK(list, elt, link) \
67 	do { \
68 		if ((elt)->link.next != NULL) \
69 			(elt)->link.next->link.prev = (elt)->link.prev; \
70 		else \
71 			(list).tail = (elt)->link.prev; \
72 		if ((elt)->link.prev != NULL) \
73 			(elt)->link.prev->link.next = (elt)->link.next; \
74 		else \
75 			(list).head = (elt)->link.next; \
76 		(elt)->link.prev = (void *)(-1); \
77 		(elt)->link.next = (void *)(-1); \
78 	} while (/*CONSTCOND*/0)
79 
80 #define LWRES_LIST_PREV(elt, link) ((elt)->link.prev)
81 #define LWRES_LIST_NEXT(elt, link) ((elt)->link.next)
82 
83 #define LWRES_LIST_INSERTBEFORE(list, before, elt, link) \
84 	do { \
85 		if ((before)->link.prev == NULL) \
86 			LWRES_LIST_PREPEND(list, elt, link); \
87 		else { \
88 			(elt)->link.prev = (before)->link.prev; \
89 			(before)->link.prev = (elt); \
90 			(elt)->link.prev->link.next = (elt); \
91 			(elt)->link.next = (before); \
92 		} \
93 	} while (/*CONSTCOND*/0)
94 
95 #define LWRES_LIST_INSERTAFTER(list, after, elt, link) \
96 	do { \
97 		if ((after)->link.next == NULL) \
98 			LWRES_LIST_APPEND(list, elt, link); \
99 		else { \
100 			(elt)->link.next = (after)->link.next; \
101 			(after)->link.next = (elt); \
102 			(elt)->link.next->link.prev = (elt); \
103 			(elt)->link.prev = (after); \
104 		} \
105 	} while (/*CONSTCOND*/0)
106 
107 #define LWRES_LIST_APPENDLIST(list1, list2, link) \
108 	do { \
109 		if (LWRES_LIST_EMPTY(list1)) \
110 			(list1) = (list2); \
111 		else if (!LWRES_LIST_EMPTY(list2)) { \
112 			(list1).tail->link.next = (list2).head; \
113 			(list2).head->link.prev = (list1).tail; \
114 			(list1).tail = (list2).tail; \
115 		} \
116 		(list2).head = NULL; \
117 		(list2).tail = NULL; \
118 	} while (/*CONSTCOND*/0)
119 
120 #define LWRES_LIST_ENQUEUE(list, elt, link) LWRES_LIST_APPEND(list, elt, link)
121 #define LWRES_LIST_DEQUEUE(list, elt, link) LWRES_LIST_UNLINK(list, elt, link)
122 
123 #endif /* LWRES_LIST_H */
124