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 12607 : static inline void dl_list_init(struct dl_list *list)
21 : {
22 12607 : list->next = list;
23 12607 : list->prev = list;
24 12607 : }
25 :
26 2869762 : static inline void dl_list_add(struct dl_list *list, struct dl_list *item)
27 : {
28 2869762 : item->next = list->next;
29 2869762 : item->prev = list;
30 2869762 : list->next->prev = item;
31 2869762 : list->next = item;
32 2869762 : }
33 :
34 22637 : static inline void dl_list_add_tail(struct dl_list *list, struct dl_list *item)
35 : {
36 22637 : dl_list_add(list->prev, item);
37 22637 : }
38 :
39 2869701 : static inline void dl_list_del(struct dl_list *item)
40 : {
41 2869701 : item->next->prev = item->prev;
42 2869701 : item->prev->next = item->next;
43 2869701 : item->next = NULL;
44 2869701 : item->prev = NULL;
45 2869701 : }
46 :
47 2898109 : static inline int dl_list_empty(struct dl_list *list)
48 : {
49 2898109 : return list->next == list;
50 : }
51 :
52 204 : static inline unsigned int dl_list_len(struct dl_list *list)
53 : {
54 : struct dl_list *item;
55 204 : int count = 0;
56 1047 : for (item = list->next; item != list; item = item->next)
57 843 : count++;
58 204 : 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 */
|