1 /* $NetBSD: list.h,v 1.1.1.1 2009/03/22 15:02:39 christos Exp $ */
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1997-2001 Internet Software Consortium.
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.
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.
20 /* Id: list.h,v 1.14 2007/06/19 23:47:23 tbox Exp */
23 #define LWRES_LIST_H 1
25 /*! \file lwres/list.h */
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)
31 #define LWRES_LINK(type) struct { type *prev, *next; }
32 #define LWRES_LINK_INIT(elt, link) \
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))
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)
44 #define LWRES_LIST_PREPEND(list, elt, link) \
46 if ((list).head != NULL) \
47 (list).head->link.prev = (elt); \
49 (list).tail = (elt); \
50 (elt)->link.prev = NULL; \
51 (elt)->link.next = (list).head; \
52 (list).head = (elt); \
53 } while (/*CONSTCOND*/0)
55 #define LWRES_LIST_APPEND(list, elt, link) \
57 if ((list).tail != NULL) \
58 (list).tail->link.next = (elt); \
60 (list).head = (elt); \
61 (elt)->link.prev = (list).tail; \
62 (elt)->link.next = NULL; \
63 (list).tail = (elt); \
64 } while (/*CONSTCOND*/0)
66 #define LWRES_LIST_UNLINK(list, elt, link) \
68 if ((elt)->link.next != NULL) \
69 (elt)->link.next->link.prev = (elt)->link.prev; \
71 (list).tail = (elt)->link.prev; \
72 if ((elt)->link.prev != NULL) \
73 (elt)->link.prev->link.next = (elt)->link.next; \
75 (list).head = (elt)->link.next; \
76 (elt)->link.prev = (void *)(-1); \
77 (elt)->link.next = (void *)(-1); \
78 } while (/*CONSTCOND*/0)
80 #define LWRES_LIST_PREV(elt, link) ((elt)->link.prev)
81 #define LWRES_LIST_NEXT(elt, link) ((elt)->link.next)
83 #define LWRES_LIST_INSERTBEFORE(list, before, elt, link) \
85 if ((before)->link.prev == NULL) \
86 LWRES_LIST_PREPEND(list, elt, link); \
88 (elt)->link.prev = (before)->link.prev; \
89 (before)->link.prev = (elt); \
90 (elt)->link.prev->link.next = (elt); \
91 (elt)->link.next = (before); \
93 } while (/*CONSTCOND*/0)
95 #define LWRES_LIST_INSERTAFTER(list, after, elt, link) \
97 if ((after)->link.next == NULL) \
98 LWRES_LIST_APPEND(list, elt, link); \
100 (elt)->link.next = (after)->link.next; \
101 (after)->link.next = (elt); \
102 (elt)->link.next->link.prev = (elt); \
103 (elt)->link.prev = (after); \
105 } while (/*CONSTCOND*/0)
107 #define LWRES_LIST_APPENDLIST(list1, list2, link) \
109 if (LWRES_LIST_EMPTY(list1)) \
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; \
116 (list2).head = NULL; \
117 (list2).tail = NULL; \
118 } while (/*CONSTCOND*/0)
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)
123 #endif /* LWRES_LIST_H */