3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 * struct dl_list - Doubly-linked list
26 static inline void dl_list_init(struct dl_list
*list
)
32 static inline void dl_list_add(struct dl_list
*list
, struct dl_list
*item
)
34 item
->next
= list
->next
;
36 list
->next
->prev
= item
;
40 static inline void dl_list_add_tail(struct dl_list
*list
, struct dl_list
*item
)
42 dl_list_add(list
->prev
, item
);
45 static inline void dl_list_del(struct dl_list
*item
)
47 item
->next
->prev
= item
->prev
;
48 item
->prev
->next
= item
->next
;
53 static inline int dl_list_empty(struct dl_list
*list
)
55 return list
->next
== list
;
58 static inline unsigned int dl_list_len(struct dl_list
*list
)
62 for (item
= list
->next
; item
!= list
; item
= item
->next
)
68 #define offsetof(type, member) ((long) &((type *) 0)->member)
71 #define dl_list_entry(item, type, member) \
72 ((type *) ((char *) item - offsetof(type, member)))
74 #define dl_list_first(list, type, member) \
75 (dl_list_empty((list)) ? NULL : \
76 dl_list_entry((list)->next, type, member))
78 #define dl_list_for_each(item, list, type, member) \
79 for (item = dl_list_entry((list)->next, type, member); \
80 &item->member != (list); \
81 item = dl_list_entry(item->member.next, type, member))
83 #define dl_list_for_each_safe(item, n, list, type, member) \
84 for (item = dl_list_entry((list)->next, type, member), \
85 n = dl_list_entry(item->member.next, type, member); \
86 &item->member != (list); \
87 item = n, n = dl_list_entry(n->member.next, type, member))