1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2009 Chris Wilson
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is Chris Wilson.
31
 *
32
 * Contributor(s):
33
 *      Chris Wilson <chris@chris-wilson.co.uk>
34
 *
35
 */
36

            
37
#ifndef CAIRO_LIST_INLINE_H
38
#define CAIRO_LIST_INLINE_H
39

            
40
#include "cairo-list-private.h"
41

            
42
/*< private >
43
 * cairo_list_entry:
44
 * @ptr: the pointer to the #cairo_list_t member.
45
 * @type: the type of the struct.
46
 * @member: the name of the list_head within the struct.
47
 *
48
 * Return value: the pointer the struct containing the @member that @ptr points to.
49
 **/
50
#define cairo_list_entry(ptr, type, member) \
51
	cairo_container_of(ptr, type, member)
52

            
53
#define cairo_list_first_entry(ptr, type, member) \
54
	cairo_list_entry((ptr)->next, type, member)
55

            
56
#define cairo_list_last_entry(ptr, type, member) \
57
	cairo_list_entry((ptr)->prev, type, member)
58

            
59
/*< private >
60
 * cairo_list_foreach:
61
 * @pos: a #cairo_list_t* to use as a loop variable.
62
 * @head: the list.
63
 *
64
 * Iterate the list. @pos points to the #cairo_list_t member of the entry struct.
65
 **/
66
#define cairo_list_foreach(pos, head)                                   \
67
	for (pos = (head)->next; pos != (head);	pos = pos->next)
68

            
69
/*< private >
70
 * cairo_list_foreach_entry:
71
 * @pos: a variable of type T * to use as a loop variable.
72
 * @type: the type of the entry struct
73
 * @head: the list
74
 * @member: the name of the #cairo_list_t member of the entry
75
 *
76
 * Iterate the list of type T.
77
 **/
78
#define cairo_list_foreach_entry(pos, type, head, member)		\
79
	for (pos = cairo_list_entry((head)->next, type, member);\
80
	     &pos->member != (head);					\
81
	     pos = cairo_list_entry(pos->member.next, type, member))
82

            
83
/*< private >
84
 * cairo_list_foreach_entry_safe:
85
 * @pos: a variable of type T * to use as a loop variable.
86
 * @n: a variable of type T * that point to the next item after @pos.
87
 * @type: the type of the entry struct
88
 * @head: the list
89
 * @member: the name of the #cairo_list_t member of the entry
90
 *
91
 * Iterate the list of type T. It is safe to remove items while
92
 * iterating. @n is a temporary variable required to support safe
93
 * iterating.
94
 *
95
 * |[<!-- language="C" -->
96
 *      struct foo {
97
 *          int a;
98
 *          cairo_list_t list;
99
 *      }
100
 *
101
 *      struct foo linked_list;
102
 *      cairo_list_init (&linked_list);
103
 *      ... calls to cairo_list_add (entry, &linked_list);
104
 *
105
 *      struct foo *pos, *next;
106
 *      cairo_list_foreach_entry_safe(pos, next, struct foo, &linked_list, list) {
107
 *          printf("%d\n", pos->a);
108
 *          cairo_list_del (pos);
109
 *      }
110
 * ]|
111
 **/
112
#define cairo_list_foreach_entry_safe(pos, n, type, head, member)	\
113
	for (pos = cairo_list_entry ((head)->next, type, member),\
114
	     n = cairo_list_entry (pos->member.next, type, member);\
115
	     &pos->member != (head);					\
116
	     pos = n, n = cairo_list_entry (n->member.next, type, member))
117

            
118
/*< private >
119
 * cairo_list_foreach_entry:
120
 * @pos: a variable of type T * to use as a loop variable.
121
 * @type: the type of the entry struct
122
 * @head: the list
123
 * @member: the name of the #cairo_list_t member of the entry
124
 *
125
 * Iterate the list of type T in reverse direction.
126
 **/
127
#define cairo_list_foreach_entry_reverse(pos, type, head, member)	\
128
	for (pos = cairo_list_entry((head)->prev, type, member);\
129
	     &pos->member != (head);					\
130
	     pos = cairo_list_entry(pos->member.prev, type, member))
131

            
132
/*< private >
133
 * cairo_list_foreach_entry_safe:
134
 * @pos: a variable of type T * to use as a loop variable.
135
 * @n: a variable of type T * that point to the next item after @pos.
136
 * @type: the type of the entry struct
137
 * @head: the list
138
 * @member: the name of the #cairo_list_t member of the entry
139
 *
140
 * Iterate the list of type T in reverse direction. It is safe to
141
 * remove items while iterating. @n is a temporary variable required
142
 * to support safe iterating.
143
 **/
144
#define cairo_list_foreach_entry_reverse_safe(pos, n, type, head, member)	\
145
	for (pos = cairo_list_entry((head)->prev, type, member),\
146
	     n = cairo_list_entry (pos->member.prev, type, member);\
147
	     &pos->member != (head);					\
148
	     pos = n, n = cairo_list_entry (n->member.prev, type, member))
149

            
150
#ifdef CAIRO_LIST_DEBUG
151
static inline void
152
_cairo_list_validate (const cairo_list_t *link)
153
{
154
    assert (link->next->prev == link);
155
    assert (link->prev->next == link);
156
}
157
static inline void
158
cairo_list_validate (const cairo_list_t *head)
159
{
160
    cairo_list_t *link;
161

            
162
    cairo_list_foreach (link, head)
163
	_cairo_list_validate (link);
164
}
165
static inline cairo_bool_t
166
cairo_list_is_empty (const cairo_list_t *head);
167
static inline void
168
cairo_list_validate_is_empty (const cairo_list_t *head)
169
{
170
    assert (head->next == NULL || (cairo_list_is_empty (head) && head->next == head->prev));
171
}
172
#else
173
#define _cairo_list_validate(link)
174
#define cairo_list_validate(head)
175
#define cairo_list_validate_is_empty(head)
176
#endif
177

            
178
/*< private >
179
 * cairo_list_init:
180
 * @entry: list entry to initialize
181
 *
182
 * Initializes the list entry to point to itself. The result is an
183
 * empty list.
184
 **/
185
static inline void
186
3796830
cairo_list_init (cairo_list_t *entry)
187
{
188
3796830
    entry->next = entry;
189
3796830
    entry->prev = entry;
190
3796830
}
191

            
192
static inline void
193
630971
__cairo_list_add (cairo_list_t *entry,
194
	          cairo_list_t *prev,
195
		  cairo_list_t *next)
196
{
197
630971
    next->prev = entry;
198
630971
    entry->next = next;
199
630971
    entry->prev = prev;
200
630971
    prev->next = entry;
201
630971
}
202

            
203
/*< private >
204
 * cairo_list_add:
205
 * @entry: new entry
206
 * @head: linked list head
207
 *
208
 * Insert a @entry at the start of the list.
209
 **/
210
static inline void
211
117758
cairo_list_add (cairo_list_t *entry, cairo_list_t *head)
212
{
213
    cairo_list_validate (head);
214
    cairo_list_validate_is_empty (entry);
215
117758
    __cairo_list_add (entry, head, head->next);
216
    cairo_list_validate (head);
217
117758
}
218

            
219
/*< private >
220
 * cairo_list_add_tail:
221
 * @entry: new entry
222
 * @head: linked list head
223
 *
224
 * Append a @entry to the end of the list.
225
 **/
226
static inline void
227
512564
cairo_list_add_tail (cairo_list_t *entry, cairo_list_t *head)
228
{
229
    cairo_list_validate (head);
230
    cairo_list_validate_is_empty (entry);
231
512564
    __cairo_list_add (entry, head->prev, head);
232
    cairo_list_validate (head);
233
512564
}
234

            
235
static inline void
236
113392
__cairo_list_del (cairo_list_t *prev, cairo_list_t *next)
237
{
238
113392
    next->prev = prev;
239
113392
    prev->next = next;
240
113392
}
241

            
242
static inline void
243
112743
_cairo_list_del (cairo_list_t *entry)
244
{
245
112743
    __cairo_list_del (entry->prev, entry->next);
246
112743
}
247

            
248
/*< private >
249
 * cairo_list_del:
250
 * @entry: entry to remove
251
 *
252
 * Remove @entry from the list it is in.
253
 **/
254
static inline void
255
112743
cairo_list_del (cairo_list_t *entry)
256
{
257
112743
    _cairo_list_del (entry);
258
112743
    cairo_list_init (entry);
259
112743
}
260

            
261
/*< private >
262
 * cairo_list_move:
263
 * @entry: entry to move
264
 * @head: linked list to move @entry to
265
 *
266
 * Remove @entry from the list it is in and insert it at the start of @head list.
267
 **/
268
static inline void
269
649
cairo_list_move (cairo_list_t *entry, cairo_list_t *head)
270
{
271
    cairo_list_validate (head);
272
649
    __cairo_list_del (entry->prev, entry->next);
273
649
    __cairo_list_add (entry, head, head->next);
274
    cairo_list_validate (head);
275
649
}
276

            
277
/*< private >
278
 * cairo_list_move_tail:
279
 * @entry: entry tp move
280
 * @head: linked list to move @entry to
281
 *
282
 * Remove @entry from the list it is in and append it to the end of @head list.
283
 **/
284
static inline void
285
cairo_list_move_tail (cairo_list_t *entry, cairo_list_t *head)
286
{
287
    cairo_list_validate (head);
288
    __cairo_list_del (entry->prev, entry->next);
289
    __cairo_list_add (entry, head->prev, head);
290
    cairo_list_validate (head);
291
}
292

            
293
/*< private >
294
 * cairo_list_move_list:
295
 * @old: List to move
296
 * @new: List to move to. Should be empty,
297
 *
298
 * Move @old list to @new list, fixing up the references.
299
 **/
300
static inline void
301
cairo_list_move_list (cairo_list_t *old, cairo_list_t *new)
302
{
303
    __cairo_list_add (new, old->prev, old->next);
304
    cairo_list_init (old);
305
}
306

            
307
/*< private >
308
 * cairo_list_is_first:
309
 * @entry: entry to check
310
 * @head: linked list
311
 *
312
 * Return %TRUE if @entry is the first item in @head.
313
 **/
314
static inline cairo_bool_t
315
23
cairo_list_is_first (const cairo_list_t *entry,
316
	             const cairo_list_t *head)
317
{
318
    cairo_list_validate (head);
319
23
    return entry->prev == head;
320
}
321

            
322
/*< private >
323
 * cairo_list_is_last:
324
 * @entry: entry to check
325
 * @head: linked list
326
 *
327
 * Return %TRUE if @entry is the last item in @head.
328
 **/
329
static inline cairo_bool_t
330
cairo_list_is_last (const cairo_list_t *entry,
331
	            const cairo_list_t *head)
332
{
333
    cairo_list_validate (head);
334
    return entry->next == head;
335
}
336

            
337
/*< private >
338
 * cairo_list_is_empty:
339
 * @head: linked list
340
 *
341
 * Return %TRUE if @head is empty.
342
 **/
343
static inline cairo_bool_t
344
1928499
cairo_list_is_empty (const cairo_list_t *head)
345
{
346
    cairo_list_validate (head);
347
1928499
    return head->next == head;
348
}
349

            
350
/*< private >
351
 * cairo_list_is_singular:
352
 * @head: linked list
353
 *
354
 * Return %TRUE if @head has only one entry.
355
 **/
356
static inline cairo_bool_t
357
cairo_list_is_singular (const cairo_list_t *head)
358
{
359
    cairo_list_validate (head);
360
    return head->next != head && head->next == head->prev;
361
}
362

            
363
#endif /* CAIRO_LIST_INLINE_H */