]> code.delx.au - pulseaudio/blob - src/pulsecore/llist.h
make PA_LLIST_HEAD_INIT thread safe
[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 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 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 <assert.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) t *name
32
33 /* The pointers in the linked list's items. Use this in the item structure */
34 #define PA_LLIST_FIELDS(t) t *next, *prev
35
36 /* Initialize the list's head */
37 #define PA_LLIST_HEAD_INIT(t,item) do { (item) = (t*) NULL; } while(0)
38
39 /* Initialize a list item */
40 #define PA_LLIST_INIT(t,item) do { \
41 t *_item = (item); \
42 assert(_item); \
43 _item->prev = _item->next = NULL; \
44 } while(0)
45
46 /* Prepend an item to the list */
47 #define PA_LLIST_PREPEND(t,head,item) do { \
48 t **_head = &(head), *_item = (item); \
49 assert(_item); \
50 if ((_item->next = *_head)) \
51 _item->next->prev = _item; \
52 _item->prev = NULL; \
53 *_head = _item; \
54 } while (0)
55
56 /* Remove an item from the list */
57 #define PA_LLIST_REMOVE(t,head,item) do { \
58 t **_head = &(head), *_item = (item); \
59 assert(_item); \
60 if (_item->next) \
61 _item->next->prev = _item->prev; \
62 if (_item->prev) \
63 _item->prev->next = _item->next; \
64 else {\
65 assert(*_head == _item); \
66 *_head = _item->next; \
67 } \
68 _item->next = _item->prev = NULL; \
69 } while(0)
70
71 #define PA_LLIST_FIND_HEAD(t,item,head) \
72 do { \
73 t **_head = (head), *_item = (item); \
74 *_head = _item; \
75 assert(_head); \
76 while ((*_head)->prev) \
77 *_head = (*_head)->prev; \
78 } while (0)
79
80 #define PA_LLIST_INSERT_AFTER(t,head,a,b) \
81 do { \
82 t **_head = &(head), *_a = (a), *_b = (b); \
83 assert(_b); \
84 if (!_a) { \
85 if ((_b->next = *_head)) \
86 _b->next->prev = _b; \
87 _b->prev = NULL; \
88 *_head = _b; \
89 } else { \
90 if ((_b->next = _a->next)) \
91 _b->next->prev = _b; \
92 _b->prev = _a; \
93 _a->next = _b; \
94 } \
95 } while (0)
96
97
98 #endif