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