2 * list.h - useful list handling header
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef AWESOME_COMMON_LIST_H
24 #define AWESOME_COMMON_LIST_H
26 #define DO_SLIST(type, prefix, dtor) \
27 static inline type *prefix##_list_next(type **list __attribute__ ((unused)), \
33 static inline type *prefix##_list_pop(type **list) \
40 res->next->prev = NULL; \
47 static inline void prefix##_list_push(type **list, type *item) \
50 (*list)->prev = item; \
55 static inline type **prefix##_list_last(type **list) \
57 while (*list && (*list)->next) \
58 list = &(*list)->next; \
62 static inline void prefix##_list_append(type **list, type *item) \
64 if(*(list = prefix##_list_last(list))) \
66 (*list)->next = item; \
73 static inline type *prefix##_list_rev(type *list) \
77 prefix##_list_push(&l, prefix##_list_pop(&list)); \
81 static inline type **prefix##_list_init(type **list) \
87 static inline void prefix##_list_wipe(type **list) \
91 type *item = prefix##_list_pop(list); \
96 static inline type *prefix##_list_prev(type **list __attribute__ ((unused)), \
102 static inline void prefix##_list_swap(type **list, type *item1, type *item2) \
104 type *i1n, *i2n, *i1p, *i2p; \
106 if(!item1 || !item2) return; \
115 item1->next = item2; \
116 item2->prev = item1; \
117 if((item1->prev = i2p)) \
119 if((item2->next = i1n)) \
122 else if(item2 == i1n) \
124 item2->next = item1; \
125 item1->prev = item2; \
126 if((item2->prev = i1p)) \
128 if((item1->next = i2n)) \
133 if((item1->next = i2n)) \
134 item1->next->prev = item1; \
135 if((item1->prev = i2p)) \
136 item1->prev->next = item1; \
137 if((item2->prev = i1p)) \
138 item2->prev->next = item2; \
139 if((item2->next = i1n)) \
140 item2->next->prev = item2; \
144 else if(*list == item2) \
148 static inline type *prefix##_list_prev_cycle(type **list, type *item) \
151 return *prefix##_list_last(list); \
155 static inline type *prefix##_list_next_cycle(type **list, type *item) \
162 static inline type *prefix##_list_detach(type **list, type *item) \
165 *list = item->next; \
166 else if(item->prev) \
167 item->prev->next = item->next; \
169 item->next->prev = item->prev; \
174 static inline type *prefix##_list_attach_after(type *item1, type *item2) \
176 item2->prev = item1; \
177 item2->next = item1->next; \
179 item1->next->prev = item2; \
180 item1->next = item2; \
185 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80