tizen 2.3 release
[external/buxton.git] / src / shared / list.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 #ifdef HAVE_CONFIG_H
6     #include "config.h"
7 #endif
8
9 /***
10   This file is part of systemd.
11
12   Copyright 2010 Lennart Poettering
13
14   systemd is free software; you can redistribute it and/or modify it
15   under the terms of the GNU Lesser General Public License as published by
16   the Free Software Foundation; either version 2.1 of the License, or
17   (at your option) any later version.
18
19   systemd is distributed in the hope that it will be useful, but
20   WITHOUT ANY WARRANTY; without even the implied warranty of
21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22   Lesser General Public License for more details.
23
24   You should have received a copy of the GNU Lesser General Public License
25   along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 ***/
27
28 /* The head of the linked list. Use this in the structure that shall
29  * contain the head of the linked list */
30 #define LIST_HEAD(t,name)                                               \
31         t *name
32
33 /* The pointers in the linked list's items. Use this in the item structure */
34 #define LIST_FIELDS(t,name)                                             \
35         t *name##_next, *name##_prev
36
37 /* Initialize the list's head */
38 #define LIST_HEAD_INIT(t,head)                                          \
39         do {                                                            \
40                 (head) = NULL; }                                        \
41         while(false)
42
43 /* Initialize a list item */
44 #define LIST_INIT(t,name,item)                                          \
45         do {                                                            \
46                 t *_item = (item);                                      \
47                 assert(_item);                                          \
48                 _item->name##_prev = _item->name##_next = NULL;         \
49         } while(false)
50
51 /* Prepend an item to the list */
52 #define LIST_PREPEND(t,name,head,item)                                  \
53         do {                                                            \
54                 t **_head = &(head), *_item = (item);                   \
55                 assert(_item);                                          \
56                 if ((_item->name##_next = *_head))                      \
57                         _item->name##_next->name##_prev = _item;        \
58                 _item->name##_prev = NULL;                              \
59                 *_head = _item;                                         \
60         } while(false)
61
62 /* Remove an item from the list */
63 #define LIST_REMOVE(t,name,head,item)                                   \
64         do {                                                            \
65                 t **_head = &(head), *_item = (item);                   \
66                 assert(_item);                                          \
67                 if (_item->name##_next)                                 \
68                         _item->name##_next->name##_prev = _item->name##_prev; \
69                 if (_item->name##_prev)                                 \
70                         _item->name##_prev->name##_next = _item->name##_next; \
71                 else {                                                  \
72                         assert(*_head == _item);                        \
73                         *_head = _item->name##_next;                    \
74                 }                                                       \
75                 _item->name##_next = _item->name##_prev = NULL;         \
76         } while(false)
77
78 /* Find the head of the list */
79 #define LIST_FIND_HEAD(t,name,item,head)                                \
80         do {                                                            \
81                 t *_item = (item);                                      \
82                 assert(_item);                                          \
83                 while (_item->name##_prev)                              \
84                        _item = _item->name##_prev;                      \
85                 (head) = _item;                                         \
86         } while (false)
87
88 /* Find the tail of the list */
89 #define LIST_FIND_TAIL(t,name,item,tail)                                \
90         do {                                                            \
91                 t *_item = (item);                                      \
92                 assert(_item);                                          \
93                 while (_item->name##_next)                              \
94                         _item = _item->name##_next;                     \
95                 (tail) = _item;                                         \
96         } while (false)
97
98 /* Insert an item after another one (a = where, b = what) */
99 #define LIST_INSERT_AFTER(t,name,head,a,b)                              \
100         do {                                                            \
101                 t **_head = &(head), *_a = (a), *_b = (b);              \
102                 assert(_b);                                             \
103                 if (!_a) {                                              \
104                         if ((_b->name##_next = *_head))                 \
105                                 _b->name##_next->name##_prev = _b;      \
106                         _b->name##_prev = NULL;                         \
107                         *_head = _b;                                    \
108                 } else {                                                \
109                         if ((_b->name##_next = _a->name##_next))        \
110                                 _b->name##_next->name##_prev = _b;      \
111                         _b->name##_prev = _a;                           \
112                         _a->name##_next = _b;                           \
113                 }                                                       \
114         } while(false)
115
116 #define LIST_JUST_US(name,item)                                         \
117         (!(item)->name##_prev && !(item)->name##_next)                  \
118
119 #define LIST_FOREACH(name,i,head)                                       \
120         for ((i) = (head); (i); (i) = (i)->name##_next)
121
122 #define LIST_FOREACH_SAFE(name,i,n,head)                                \
123         for ((i) = (head); (i) && (((n) = (i)->name##_next), 1); (i) = (n))
124
125 #define LIST_FOREACH_BEFORE(name,i,p)                                   \
126         for ((i) = (p)->name##_prev; (i); (i) = (i)->name##_prev)
127
128 #define LIST_FOREACH_AFTER(name,i,p)                                    \
129         for ((i) = (p)->name##_next; (i); (i) = (i)->name##_next)
130
131 /* Loop starting from p->next until p->prev.
132    p can be adjusted meanwhile. */
133 #define LIST_LOOP_BUT_ONE(name,i,head,p)                                \
134         for ((i) = (p)->name##_next ? (p)->name##_next : (head);        \
135              (i) != (p);                                                \
136              (i) = (i)->name##_next ? (i)->name##_next : (head))