]> git.meshlink.io Git - catta/blob - libavahi-core/llist.h
16a16b6f327f65cfb6d8b92a7dba786f6e860e0c
[catta] / libavahi-core / llist.h
1 #ifndef foollistfoo
2 #define foollistfoo
3
4 #include <glib.h>
5
6 /* Some macros for maintaining doubly linked lists */
7
8 /* The head of the linked list. Use this in the structure that shall
9  * contain the head of the linked list */
10 #define AVAHI_LLIST_HEAD(t,name) t *name
11
12 /* The pointers in the linked list's items. Use this in the item structure */
13 #define AVAHI_LLIST_FIELDS(t,name) t *name##_next, *name##_prev
14
15 /* Initialize the list's head */
16 #define AVAHI_LLIST_HEAD_INIT(t,head) do { (head) = NULL; } while(0)
17
18 /* Initialize a list item */
19 #define AVAHI_LLIST_INIT(t,name,item) do { \
20                                t *_item = (item); \
21                                g_assert(_item); \
22                                _item->name##_prev = _item->name##_next = NULL; \
23                                } while(0)
24
25 /* Prepend an item to the list */
26 #define AVAHI_LLIST_PREPEND(t,name,head,item) do { \
27                                         t **_head = &(head), *_item = (item); \
28                                         g_assert(_item); \
29                                         if ((_item->name##_next = *_head)) \
30                                            _item->name##_next->name##_prev = _item; \
31                                         _item->name##_prev = NULL; \
32                                         *_head = _item; \
33                                         } while (0)
34
35 /* Remove an item from the list */
36 #define AVAHI_LLIST_REMOVE(t,name,head,item) do { \
37                                     t **_head = &(head), *_item = (item); \
38                                     g_assert(_item); \
39                                     if (_item->name##_next) \
40                                        _item->name##_next->name##_prev = _item->name##_prev; \
41                                     if (_item->name##_prev) \
42                                        _item->name##_prev->name##_next = _item->name##_next; \
43                                     else {\
44                                        g_assert(*_head == _item); \
45                                        *_head = _item->name##_next; \
46                                     } \
47                                     _item->name##_next = _item->name##_prev = NULL; \
48                                     } while(0)
49
50 #endif