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