1 /*	$NetBSD: dlz_list.h,v 1.6 2023/01/25 21:43:29 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: ISC
7  *
8  * Permission to use, copy, modify, and distribute this software for any purpose
9  * with or without fee is hereby granted, provided that the above copyright
10  * notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18  * PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 #ifndef DLZ_LIST_H
22 #define DLZ_LIST_H 1
23 
24 #define DLZ_LIST(type)             \
25 	struct {                   \
26 		type *head, *tail; \
27 	}
28 #define DLZ_LIST_INIT(list)         \
29 	do {                        \
30 		(list).head = NULL; \
31 		(list).tail = NULL; \
32 	} while (0)
33 
34 #define DLZ_LINK(type)             \
35 	struct {                   \
36 		type *prev, *next; \
37 	}
38 #define DLZ_LINK_INIT(elt, link)                 \
39 	do {                                     \
40 		(elt)->link.prev = (void *)(-1); \
41 		(elt)->link.next = (void *)(-1); \
42 	} while (0)
43 
44 #define DLZ_LIST_HEAD(list) ((list).head)
45 #define DLZ_LIST_TAIL(list) ((list).tail)
46 
47 #define DLZ_LIST_APPEND(list, elt, link)                \
48 	do {                                            \
49 		if ((list).tail != NULL)                \
50 			(list).tail->link.next = (elt); \
51 		else                                    \
52 			(list).head = (elt);            \
53 		(elt)->link.prev = (list).tail;         \
54 		(elt)->link.next = NULL;                \
55 		(list).tail = (elt);                    \
56 	} while (0)
57 
58 #define DLZ_LIST_PREV(elt, link) ((elt)->link.prev)
59 #define DLZ_LIST_NEXT(elt, link) ((elt)->link.next)
60 
61 #define DLZ_LIST_UNLINK(list, elt, link)                                \
62 	do {                                                            \
63 		if ((elt)->link.next != NULL)                           \
64 			(elt)->link.next->link.prev = (elt)->link.prev; \
65 		else                                                    \
66 			(list).tail = (elt)->link.prev;                 \
67 		if ((elt)->link.prev != NULL)                           \
68 			(elt)->link.prev->link.next = (elt)->link.next; \
69 		else                                                    \
70 			(list).head = (elt)->link.next;                 \
71 		(elt)->link.prev = (void *)(-1);                        \
72 		(elt)->link.next = (void *)(-1);                        \
73 	} while (0)
74 
75 #endif /* DLZ_LIST_H */
76