]> code.delx.au - pulseaudio/blob - src/pulsecore/llist.h
merge 'lennart' branch back into trunk.
[pulseaudio] / src / pulsecore / llist.h
1 #ifndef foollistfoo
2 #define foollistfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2004-2006 Lennart Poettering
10
11 PulseAudio is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as
13 published by the Free Software Foundation; either version 2 of the
14 License, or (at your option) any later version.
15
16 PulseAudio is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with PulseAudio; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 USA.
25 ***/
26
27 #include <pulsecore/macro.h>
28
29 /* Some macros for maintaining doubly linked lists */
30
31 /* The head of the linked list. Use this in the structure that shall
32 * contain the head of the linked list */
33 #define PA_LLIST_HEAD(t,name) \
34 t *name
35
36 /* The pointers in the linked list's items. Use this in the item structure */
37 #define PA_LLIST_FIELDS(t) \
38 t *next, *prev
39
40 /* Initialize the list's head */
41 #define PA_LLIST_HEAD_INIT(t,item) \
42 do { \
43 (item) = (t*) NULL; } \
44 while(0)
45
46 /* Initialize a list item */
47 #define PA_LLIST_INIT(t,item) \
48 do { \
49 t *_item = (item); \
50 pa_assert(_item); \
51 _item->prev = _item->next = NULL; \
52 } while(0)
53
54 /* Prepend an item to the list */
55 #define PA_LLIST_PREPEND(t,head,item) \
56 do { \
57 t **_head = &(head), *_item = (item); \
58 pa_assert(_item); \
59 if ((_item->next = *_head)) \
60 _item->next->prev = _item; \
61 _item->prev = NULL; \
62 *_head = _item; \
63 } while (0)
64
65 /* Remove an item from the list */
66 #define PA_LLIST_REMOVE(t,head,item) \
67 do { \
68 t **_head = &(head), *_item = (item); \
69 pa_assert(_item); \
70 if (_item->next) \
71 _item->next->prev = _item->prev; \
72 if (_item->prev) \
73 _item->prev->next = _item->next; \
74 else { \
75 pa_assert(*_head == _item); \
76 *_head = _item->next; \
77 } \
78 _item->next = _item->prev = NULL; \
79 } while(0)
80
81 /* Find the head of the list */
82 #define PA_LLIST_FIND_HEAD(t,item,head) \
83 do { \
84 t **_head = (head), *_item = (item); \
85 *_head = _item; \
86 pa_assert(_head); \
87 while ((*_head)->prev) \
88 *_head = (*_head)->prev; \
89 } while (0)
90
91 /* Insert an item after another one (a = where, b = what) */
92 #define PA_LLIST_INSERT_AFTER(t,head,a,b) \
93 do { \
94 t **_head = &(head), *_a = (a), *_b = (b); \
95 pa_assert(_b); \
96 if (!_a) { \
97 if ((_b->next = *_head)) \
98 _b->next->prev = _b; \
99 _b->prev = NULL; \
100 *_head = _b; \
101 } else { \
102 if ((_b->next = _a->next)) \
103 _b->next->prev = _b; \
104 _b->prev = _a; \
105 _a->next = _b; \
106 } \
107 } while (0)
108
109 #endif