FreeTDS API: include/freetds/dlist.tmpl.h Source File

FreeTDS API

dlist.tmpl.h
1 /* Dlist - dynamic list
2 * Copyright (C) 2016 Frediano Ziglio
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20 #if !defined(DLIST_NAME) || !defined(DLIST_TYPE) || !defined(DLIST_LIST_TYPE)
21 #error Required defines missing!
22 #endif
23
24 typedef struct
25 {
26 dlist_ring ring;
27 } DLIST_LIST_TYPE;
28
29 #undef DLIST_ITEM
30 #define DLIST_ITEM(ring) ((DLIST_TYPE *) (((char *) (ring)) - TDS_OFFSET(DLIST_TYPE, DLIST_NAME(item))))
31
32 static inline void DLIST_NAME(check)(DLIST_LIST_TYPE *list)
33 {
34 #if ENABLE_EXTRA_CHECKS
35 assert(list != NULL);
36 dlist_ring_check(&list->ring);
37 #endif
38 }
39
40 static inline void DLIST_NAME(init)(DLIST_LIST_TYPE *list)
41 {
42 list->ring.next = list->ring.prev = &list->ring;
43 DLIST_NAME(check)(list);
44 }
45
46 static inline DLIST_TYPE *DLIST_NAME(first)(DLIST_LIST_TYPE *list)
47 {
48 return list->ring.next == &list->ring ? NULL : DLIST_ITEM(list->ring.next);
49 }
50
51 static inline DLIST_TYPE *DLIST_NAME(last)(DLIST_LIST_TYPE *list)
52 {
53 return list->ring.prev == &list->ring ? NULL : DLIST_ITEM(list->ring.prev);
54 }
55
56 static inline DLIST_TYPE *DLIST_NAME(next)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
57 {
58 return item->DLIST_NAME(item).next == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).next);
59 }
60
61 static inline DLIST_TYPE *DLIST_NAME(prev)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
62 {
63 return item->DLIST_NAME(item).prev == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).prev);
64 }
65
66 static inline void DLIST_NAME(prepend)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
67 {
68 DLIST_NAME(check)(list);
69 assert(item->DLIST_NAME(item).next == NULL && item->DLIST_NAME(item).prev == NULL);
70 list->ring.next->prev = &item->DLIST_NAME(item);
71 item->DLIST_NAME(item).next = list->ring.next;
72 item->DLIST_NAME(item).prev = &list->ring;
73 list->ring.next = &item->DLIST_NAME(item);
74 assert(item->DLIST_NAME(item).next != NULL && item->DLIST_NAME(item).prev != NULL);
75 DLIST_NAME(check)(list);
76 }
77
78 static inline void DLIST_NAME(append)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
79 {
80 DLIST_NAME(check)(list);
81 assert(item->DLIST_NAME(item).next == NULL && item->DLIST_NAME(item).prev == NULL);
82 list->ring.prev->next = &item->DLIST_NAME(item);
83 item->DLIST_NAME(item).prev = list->ring.prev;
84 item->DLIST_NAME(item).next = &list->ring;
85 list->ring.prev = &item->DLIST_NAME(item);
86 assert(item->DLIST_NAME(item).next != NULL && item->DLIST_NAME(item).prev != NULL);
87 DLIST_NAME(check)(list);
88 }
89
90 static inline void DLIST_NAME(remove)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
91 {
92 dlist_ring *prev = item->DLIST_NAME(item).prev, *next = item->DLIST_NAME(item).next;
93 DLIST_NAME(check)(list);
94 if (prev)
95 prev->next = next;
96 if (next)
97 next->prev = prev;
98 item->DLIST_NAME(item).prev = NULL;
99 item->DLIST_NAME(item).next = NULL;
100 DLIST_NAME(check)(list);
101 }
102
103 static inline bool DLIST_NAME(in_list)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
104 {
105 DLIST_NAME(check)(list);
106 return item->DLIST_NAME(item).prev != NULL || item->DLIST_NAME(item).next != NULL;
107 }
108
109 #undef DLIST_ITEM
110 #undef DLIST_NAME
111 #undef DLIST_TYPE
112 #undef DLIST_LIST_TYPE
113
DLIST_LIST_TYPE
Definition: dlist.tmpl.h:24
dlist_ring
Definition: dlist.h:23