File: prelude-list.h

package info (click to toggle)
libprelude 1.0.0-11.9
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 24,456 kB
  • ctags: 17,851
  • sloc: ansic: 190,841; cpp: 38,769; xml: 30,141; sh: 11,668; makefile: 692; python: 481; awk: 341; yacc: 238; lex: 186; perl: 5
file content (275 lines) | stat: -rw-r--r-- 9,445 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*****
*
* Copyright (C) 2005 PreludeIDS Technologies. All Rights Reserved.
* Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
*
* This file is part of the Prelude library.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING.  If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****/

/*
 * This is a blind rewrite of the kernel linked list handling code,
 * done so that we can dual-license libprelude. The code still look
 * pretty similar, but there is no way to write such list implementation
 * in many different manner.
 */

#ifndef HAVE_LIBPRELUDE_PRELUDE_LIST_H
#define HAVE_LIBPRELUDE_PRELUDE_LIST_H

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "prelude-inttypes.h"

#ifdef __cplusplus
  extern "C" {
#endif

#define PRELUDE_LIST(item) prelude_list_t (item) = { &(item), &(item) }


typedef struct prelude_list {
        struct prelude_list *next;
        struct prelude_list *prev;
} prelude_list_t;



static inline void __prelude_list_add(prelude_list_t *item, prelude_list_t *prev, prelude_list_t *next)
{
        prev->next = item;
        item->prev = prev;
        item->next = next;
        next->prev = item;
}


/**
 * prelude_list_init:
 * @item: Pointer to a #prelude_list_t object.
 *
 * Initialize @item. Note that this is only required if @item
 * is the head of a list, but might also be useful in case you
 * want to use prelude_list_del_init().
 */
static inline void prelude_list_init(prelude_list_t *item)
{
        item->next = item->prev = item;
}



/**
 * prelude_list_is_empty:
 * @item: Pointer to a #prelude_list_t object.
 *
 * Check whether @item is empty or not.
 *
 * Returns: TRUE if @item is empty, FALSE otherwise.
 */
static inline prelude_bool_t prelude_list_is_empty(prelude_list_t *item)
{
        return (item->next == item) ? PRELUDE_BOOL_TRUE : PRELUDE_BOOL_FALSE;
}



/**
 * prelude_list_add:
 * @head: Pointer to a #prelude_list_t list.
 * @item: Pointer to a #prelude_list_t object to add to @head.
 *
 * Add @item at the beginning of @head list.
 */
static inline void prelude_list_add(prelude_list_t *head, prelude_list_t *item)
{
        __prelude_list_add(item, head, head->next);
}



/**
 * prelude_list_add_tail:
 * @head: Pointer to a #prelude_list_t list.
 * @item: Pointer to a #prelude_list_t object to add to @head.
 *
 * Add @item at the tail of @head list.
 */
static inline void prelude_list_add_tail(prelude_list_t *head, prelude_list_t *item)
{
        __prelude_list_add(item, head->prev, head);
}



/**
 * prelude_list_del:
 * @item: Pointer to a #prelude_list_t object.
 *
 * Delete @item from the list it is linked in.
 */
static inline void prelude_list_del(prelude_list_t *item)
{
        item->prev->next = item->next;
        item->next->prev = item->prev;
}



/**
 * prelude_list_del_init:
 * @item: Pointer to a #prelude_list_t object.
 *
 * Delete @item from the list it is linked in, and reinitialize
 * @item member so that the list can be considered as empty.
 */
static inline void prelude_list_del_init(prelude_list_t *item)
{
        item->prev->next = item->next;
        item->next->prev = item->prev;
        prelude_list_init(item);
}


/**
 * prelude_list_entry:
 * @item: Pointer to a #prelude_list_t object to retrieve the entry from.
 * @type: Type of the entry to retrieve.
 * @member: List member in @type used to link it to a list.
 *
 * Retrieve the entry of type @type from the #prelude_list_t object @tmp,
 * using the item list member @member. Returns the entry associated with @item.
 */
#define prelude_list_entry(item, type, member)                             \
        (type *) ((unsigned long) item - (unsigned long) &((type *) 0)->member)



/**
 * prelude_list_for_each:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 *
 * Iterate through all @list entry. prelude_list_entry() can be used to retrieve
 * and entry from the @pos pointer. It is not safe to call prelude_list_del() while
 * iterating using this function, see prelude_list_for_each_safe().
 */
#define prelude_list_for_each(list, pos)                                   \
        for ( (pos) = (list)->next; (pos) != (list); (pos) = (pos)->next )


/**
 * prelude_list_for_each_safe:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 * @bkp: Pointer to a #prelude_list_t object pointing to the next list member.
 *
 * Iterate through all @list entry. prelude_list_entry() can be used to retrieve
 * and entry from the @pos pointer. Calling prelude_list_del() while iterating the
 * list is safe.
 */
#define prelude_list_for_each_safe(list, pos, bkp)                         \
        for ( (pos) = (list)->next, (bkp) = (pos)->next; (pos) != (list); (pos) = (bkp), (bkp) = (pos)->next )



/**
 * prelude_list_for_each_reversed:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 *
 * Iterate through all @list entry in reverse order. prelude_list_entry() can be
 * used to retrieve and entry from the @pos pointer. It is not safe to call
 * prelude_list_del() while iterating using this function, see
 * prelude_list_for_each_reversed_safe().
 */
#define prelude_list_for_each_reversed(list, pos)                          \
        for ( (pos) = (list)->prev; (pos) != (list); (pos) = (pos)->prev ) 



/**
 * prelude_list_for_each_reversed_safe:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 * @bkp: Pointer to a #prelude_list_t object pointing to the next list member.
 *
 * Iterate through all @list entry in reverse order. prelude_list_entry() can be used to retrieve
 * and entry from the @pos pointer. Calling prelude_list_del() while iterating the
 * list is safe.
 */
#define prelude_list_for_each_reversed_safe(list, pos, bkp)                \
        for ( (pos) = (list)->prev, (bkp) = (pos)->prev; (pos) != (list); (pos) = (bkp), (bkp) = (pos)->prev )


/**
 * prelude_list_for_each_continue:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 *
 * Iterate through all @list entry starting from @pos if it is not NULL, or from
 * the start of @list if it is. prelude_list_entry() can be used to retrieve
 * and entry from the @pos pointer. Calling prelude_list_del() while iterating the
 * list is not safe.
 */
#define prelude_list_for_each_continue(list, pos)                          \
        for ( (pos) = ((pos) == NULL) ? (list)->next : (pos)->next; (pos) != (list); (pos) = (pos)->next )


/**
 * prelude_list_for_each_continue_safe:
 * @list: Pointer to a #prelude_list_t list.
 * @pos: Pointer to a #prelude_list_t object pointing to the current list member.
 * @bkp: Pointer to a #prelude_list_t object pointing to the next list member.
 * 
 * Iterate through all @list entry starting from @pos if it is not NULL, or from
 * the start of @list if it is. prelude_list_entry() can be used to retrieve
 * and entry from the @pos pointer. Calling prelude_list_del() while iterating the
 * list is safe.
 */
#define prelude_list_for_each_continue_safe(list, pos, bkp)                \
        for ( (pos) = ((bkp) == NULL) ? (list)->next : (bkp); (bkp) = (pos)->next, (pos) != (list); (pos) = (bkp) )



#define prelude_list_get_next(list, pos, class, member) \
        pos ? \
                ((pos)->member.next == (list)) ? NULL : \
                                prelude_list_entry((pos)->member.next, class, member) \
        : \
                ((list)->next == (list)) ? NULL : \
                                prelude_list_entry((list)->next, class, member)


#define prelude_list_get_next_safe(list, pos, bkp, class, member)                                                                \
        pos ?                                                                                                            \
              (((pos) = bkp),                                                                                            \
               ((bkp) = (! (bkp) || (bkp)->member.next == list) ? NULL : prelude_list_entry((pos)->member.next, class, member)), \
               (pos))                                                                                                    \
        :                                                                                                                \
              (((pos) = ((list)->next == list) ? NULL : prelude_list_entry((list)->next, class, member)),                        \
               ((bkp) = (! (pos) ||(pos)->member.next == list ) ? NULL : prelude_list_entry((pos)->member.next, class, member)), \
               (pos))


#ifdef __cplusplus
  }
#endif

#endif