]> git.meshlink.io Git - catta/blob - include/catta/llist.h
process change events in the order they are received
[catta] / include / catta / llist.h
1 #ifndef foollistfoo
2 #define foollistfoo
3
4 /***
5   This file is part of catta.
6
7   catta is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as
9   published by the Free Software Foundation; either version 2.1 of the
10   License, or (at your option) any later version.
11
12   catta is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15   Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with catta; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 /** \file llist.h A simple macro based linked list implementation */
24
25 #include <assert.h>
26
27 #include <catta/cdecl.h>
28
29 CATTA_C_DECL_BEGIN
30
31 /** The head of the linked list. Use this in the structure that shall
32  * contain the head of the linked list */
33 #define CATTA_LLIST_HEAD(t,name) t *name
34
35 /** The pointers in the linked list's items. Use this in the item structure */
36 #define CATTA_LLIST_FIELDS(t,name) t *name##_next, *name##_prev
37
38 /** Initialize the list's head */
39 #define CATTA_LLIST_HEAD_INIT(t,head) do { (head) = NULL; } while(0)
40
41 /** Initialize a list item */
42 #define CATTA_LLIST_INIT(t,name,item) do { \
43                                t *_item = (item); \
44                                assert(_item); \
45                                _item->name##_prev = _item->name##_next = NULL; \
46                                } while(0)
47
48 /** Prepend an item to the list */
49 #define CATTA_LLIST_PREPEND(t,name,head,item) do { \
50                                         t **_head = &(head), *_item = (item); \
51                                         assert(_item); \
52                                         if ((_item->name##_next = *_head)) \
53                                            _item->name##_next->name##_prev = _item; \
54                                         _item->name##_prev = NULL; \
55                                         *_head = _item; \
56                                         } while (0)
57
58 /** Append an item to the list */
59 #define CATTA_LLIST_APPEND(t,name,head,item) do { \
60         t **_cur = &(head), *_prev, *_item = (item); \
61         assert(_item); \
62         while ((_prev = *_cur)) \
63             _cur = &_prev->name##_next; \
64         _item->name##_prev = _prev; \
65         _item->name##_next = NULL; \
66         *_cur = _item; \
67     } while (0)
68
69 /** Remove an item from the list */
70 #define CATTA_LLIST_REMOVE(t,name,head,item) do { \
71                                     t **_head = &(head), *_item = (item); \
72                                     assert(_item); \
73                                     if (_item->name##_next) \
74                                        _item->name##_next->name##_prev = _item->name##_prev; \
75                                     if (_item->name##_prev) \
76                                        _item->name##_prev->name##_next = _item->name##_next; \
77                                     else {\
78                                        assert(*_head == _item); \
79                                        *_head = _item->name##_next; \
80                                     } \
81                                     _item->name##_next = _item->name##_prev = NULL; \
82                                     } while(0)
83
84 CATTA_C_DECL_END
85
86 #endif