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

FreeTDS API

Loading...
Searching...
No Matches
dlist.tmpl.h
1/* Dlist - dynamic list
2 * Copyright (C) 2016-2024 Frediano Ziglio
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20#include <freetds/bool.h>
21#include <freetds/utils.h>
22#include <freetds/macros.h>
23
24#if !defined(DLIST_PREFIX) || !defined(DLIST_ITEM_TYPE) || !defined(DLIST_LIST_TYPE)
25#error Required defines for dlist missing!
26#endif
27
28#if defined(DLIST_NAME) || defined(DLIST_PASTER) || \
29 defined(DLIST_EVALUATOR) || defined(DLIST_ITEM)
30#error Some internal dlist macros already defined
31#endif
32
33 typedef struct
34{
35 dlist_ring ring;
36} DLIST_LIST_TYPE;
37
38#define DLIST_PASTER(x,y) x ## _ ## y
39#define DLIST_EVALUATOR(x,y) DLIST_PASTER(x,y)
40#define DLIST_NAME(suffix) DLIST_EVALUATOR(DLIST_PREFIX, suffix)
41#define DLIST_ITEM(ring) \
42 ((DLIST_ITEM_TYPE *) (((char *) (ring)) - TDS_OFFSET(DLIST_ITEM_TYPE, DLIST_NAME(item))))
43
44static inline void DLIST_NAME(check)(DLIST_LIST_TYPE *list TDS_UNUSED)
45{
46#if ENABLE_EXTRA_CHECKS
47 assert(list != NULL);
48 dlist_ring_check(&list->ring);
49#endif
50}
51
52static inline void DLIST_NAME(init)(DLIST_LIST_TYPE *list)
53{
54 list->ring.next = list->ring.prev = &list->ring;
55 DLIST_NAME(check)(list);
56}
57
58static inline DLIST_ITEM_TYPE *DLIST_NAME(first)(DLIST_LIST_TYPE *list)
59{
60 return list->ring.next == &list->ring ? NULL : DLIST_ITEM(list->ring.next);
61}
62
63static inline DLIST_ITEM_TYPE *DLIST_NAME(last)(DLIST_LIST_TYPE *list)
64{
65 return list->ring.prev == &list->ring ? NULL : DLIST_ITEM(list->ring.prev);
66}
67
68static inline DLIST_ITEM_TYPE *DLIST_NAME(next)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
69{
70 return item->DLIST_NAME(item).next == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).next);
71}
72
73static inline DLIST_ITEM_TYPE *DLIST_NAME(prev)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
74{
75 return item->DLIST_NAME(item).prev == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).prev);
76}
77
78static inline void DLIST_NAME(prepend)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
79{
80 DLIST_NAME(check)(list);
81 dlist_insert_after(&list->ring, &item->DLIST_NAME(item));
82 DLIST_NAME(check)(list);
83}
84
85static inline void DLIST_NAME(append)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
86{
87 DLIST_NAME(check)(list);
88 dlist_insert_after(list->ring.prev, &item->DLIST_NAME(item));
89 DLIST_NAME(check)(list);
90}
91
92static inline void DLIST_NAME(remove)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
93{
94 dlist_ring *prev = item->DLIST_NAME(item).prev, *next = item->DLIST_NAME(item).next;
95 DLIST_NAME(check)(list);
96 if (prev) {
97 prev->next = next;
98 next->prev = prev;
99 }
100 item->DLIST_NAME(item).prev = NULL;
101 item->DLIST_NAME(item).next = NULL;
102 DLIST_NAME(check)(list);
103}
104
105static inline bool DLIST_NAME(in_list)(DLIST_LIST_TYPE *list, DLIST_ITEM_TYPE *item)
106{
107 DLIST_NAME(check)(list);
108 return item->DLIST_NAME(item).prev != NULL || item->DLIST_NAME(item).next != NULL;
109}
110
111#undef DLIST_ITEM
112#undef DLIST_NAME
113#undef DLIST_ITEM_TYPE
114#undef DLIST_LIST_TYPE
115#undef DLIST_PREFIX
116#undef DLIST_PASTER
117#undef DLIST_EVALUATOR
118
dlist_ring
Definition dlist.h:23