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