• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <pulsecore/macro.h>
24 
25 /* Some macros for maintaining doubly linked lists */
26 
27 /* The head of the linked list. Use this in the structure that shall
28  * contain the head of the linked list */
29 #define PA_LLIST_HEAD(t,name)                                           \
30     t *name
31 
32 /* The pointers in the linked list's items. Use this in the item structure */
33 #define PA_LLIST_FIELDS(t)                                              \
34     t *next, *prev
35 
36 /* Initialize the list's head */
37 #define PA_LLIST_HEAD_INIT(t,item)                                      \
38     do {                                                                \
39         (item) = (t*) NULL; }                                           \
40     while(0)
41 
42 /* Initialize a list item */
43 #define PA_LLIST_INIT(t,item)                                           \
44     do {                                                                \
45         t *_item = (item);                                              \
46         pa_assert(_item);                                               \
47         _item->prev = _item->next = NULL;                               \
48     } while(0)
49 
50 /* Prepend an item to the list */
51 #define PA_LLIST_PREPEND(t,head,item)                                   \
52     do {                                                                \
53         t **_head = &(head), *_item = (item);                           \
54         pa_assert(_item);                                               \
55         if ((_item->next = *_head))                                     \
56             _item->next->prev = _item;                                  \
57         _item->prev = NULL;                                             \
58         *_head = _item;                                                 \
59     } while (0)
60 
61 /* Remove an item from the list */
62 #define PA_LLIST_REMOVE(t,head,item)                                    \
63     do {                                                                \
64         t **_head = &(head), *_item = (item);                           \
65         pa_assert(_item);                                               \
66         if (_item->next)                                                \
67             _item->next->prev = _item->prev;                            \
68         if (_item->prev)                                                \
69             _item->prev->next = _item->next;                            \
70         else {                                                          \
71             pa_assert(*_head == _item);                                 \
72             *_head = _item->next;                                       \
73         }                                                               \
74         _item->next = _item->prev = NULL;                               \
75     } while(0)
76 
77 /* Find the head of the list */
78 #define PA_LLIST_FIND_HEAD(t,item,head)                                 \
79     do {                                                                \
80         t **_head = (head), *_item = (item);                            \
81         *_head = _item;                                                 \
82         pa_assert(_head);                                               \
83         while ((*_head)->prev)                                          \
84             *_head = (*_head)->prev;                                    \
85     } while (0)
86 
87 /* Insert an item after another one (a = where, b = what) */
88 #define PA_LLIST_INSERT_AFTER(t,head,a,b)                               \
89     do {                                                                \
90         t **_head = &(head), *_a = (a), *_b = (b);                      \
91         pa_assert(_b);                                                  \
92         if (!_a) {                                                      \
93             if ((_b->next = *_head))                                    \
94                 _b->next->prev = _b;                                    \
95             _b->prev = NULL;                                            \
96             *_head = _b;                                                \
97         } else {                                                        \
98             if ((_b->next = _a->next))                                  \
99                 _b->next->prev = _b;                                    \
100             _b->prev = _a;                                              \
101             _a->next = _b;                                              \
102         }                                                               \
103     } while (0)
104 
105 #define PA_LLIST_FOREACH(i,head)                                        \
106     for (i = (head); i; i = i->next)
107 
108 #define PA_LLIST_FOREACH_SAFE(i,n,head)                                 \
109     for (i = (head); i && ((n = i->next), 1); i = n)
110 
111 #endif
112