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