Line data Source code
1 : /*
2 : * Doubly-linked list
3 : * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #ifndef LIST_H
10 : #define LIST_H
11 :
12 : /**
13 : * struct dl_list - Doubly-linked list
14 : */
15 : struct dl_list {
16 : struct dl_list *next;
17 : struct dl_list *prev;
18 : };
19 :
20 3892 : static inline void dl_list_init(struct dl_list *list)
21 : {
22 3892 : list->next = list;
23 3892 : list->prev = list;
24 3892 : }
25 :
26 1166091 : static inline void dl_list_add(struct dl_list *list, struct dl_list *item)
27 : {
28 1166091 : item->next = list->next;
29 1166091 : item->prev = list;
30 1166091 : list->next->prev = item;
31 1166091 : list->next = item;
32 1166091 : }
33 :
34 12521 : static inline void dl_list_add_tail(struct dl_list *list, struct dl_list *item)
35 : {
36 12521 : dl_list_add(list->prev, item);
37 12521 : }
38 :
39 1166073 : static inline void dl_list_del(struct dl_list *item)
40 : {
41 1166073 : item->next->prev = item->prev;
42 1166073 : item->prev->next = item->next;
43 1166073 : item->next = NULL;
44 1166073 : item->prev = NULL;
45 1166073 : }
46 :
47 1489229 : static inline int dl_list_empty(struct dl_list *list)
48 : {
49 1489229 : return list->next == list;
50 : }
51 :
52 197 : static inline unsigned int dl_list_len(struct dl_list *list)
53 : {
54 : struct dl_list *item;
55 197 : int count = 0;
56 1037 : for (item = list->next; item != list; item = item->next)
57 840 : count++;
58 197 : return count;
59 : }
60 :
61 : #ifndef offsetof
62 : #define offsetof(type, member) ((long) &((type *) 0)->member)
63 : #endif
64 :
65 : #define dl_list_entry(item, type, member) \
66 : ((type *) ((char *) item - offsetof(type, member)))
67 :
68 : #define dl_list_first(list, type, member) \
69 : (dl_list_empty((list)) ? NULL : \
70 : dl_list_entry((list)->next, type, member))
71 :
72 : #define dl_list_last(list, type, member) \
73 : (dl_list_empty((list)) ? NULL : \
74 : dl_list_entry((list)->prev, type, member))
75 :
76 : #define dl_list_for_each(item, list, type, member) \
77 : for (item = dl_list_entry((list)->next, type, member); \
78 : &item->member != (list); \
79 : item = dl_list_entry(item->member.next, type, member))
80 :
81 : #define dl_list_for_each_safe(item, n, list, type, member) \
82 : for (item = dl_list_entry((list)->next, type, member), \
83 : n = dl_list_entry(item->member.next, type, member); \
84 : &item->member != (list); \
85 : item = n, n = dl_list_entry(n->member.next, type, member))
86 :
87 : #define dl_list_for_each_reverse(item, list, type, member) \
88 : for (item = dl_list_entry((list)->prev, type, member); \
89 : &item->member != (list); \
90 : item = dl_list_entry(item->member.prev, type, member))
91 :
92 : #define DEFINE_DL_LIST(name) \
93 : struct dl_list name = { &(name), &(name) }
94 :
95 : #endif /* LIST_H */
|