1 /*
2  * netlink/list.h	Netlink List Utilities
3  *
4  *	This library is free software; you can redistribute it and/or
5  *	modify it under the terms of the GNU Lesser General Public
6  *	License as published by the Free Software Foundation version 2.1
7  *	of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #ifndef NETLINK_LIST_H_
13 #define NETLINK_LIST_H_
14 
15 struct nl_list_head
16 {
17 	struct nl_list_head *	next;
18 	struct nl_list_head *	prev;
19 };
20 
NL_INIT_LIST_HEAD(struct nl_list_head * list)21 static inline void NL_INIT_LIST_HEAD(struct nl_list_head *list)
22 {
23 	list->next = list;
24 	list->prev = list;
25 }
26 
__nl_list_add(struct nl_list_head * obj,struct nl_list_head * prev,struct nl_list_head * next)27 static inline void __nl_list_add(struct nl_list_head *obj,
28 				 struct nl_list_head *prev,
29 				 struct nl_list_head *next)
30 {
31 	prev->next = obj;
32 	obj->prev = prev;
33 	next->prev = obj;
34 	obj->next = next;
35 }
36 
nl_list_add_tail(struct nl_list_head * obj,struct nl_list_head * head)37 static inline void nl_list_add_tail(struct nl_list_head *obj,
38 				    struct nl_list_head *head)
39 {
40 	__nl_list_add(obj, head->prev, head);
41 }
42 
nl_list_add_head(struct nl_list_head * obj,struct nl_list_head * head)43 static inline void nl_list_add_head(struct nl_list_head *obj,
44 				    struct nl_list_head *head)
45 {
46 	__nl_list_add(obj, head, head->next);
47 }
48 
nl_list_del(struct nl_list_head * obj)49 static inline void nl_list_del(struct nl_list_head *obj)
50 {
51 	obj->next->prev = obj->prev;
52 	obj->prev->next = obj->next;
53 }
54 
nl_list_empty(struct nl_list_head * head)55 static inline int nl_list_empty(struct nl_list_head *head)
56 {
57 	return head->next == head;
58 }
59 
60 #define nl_container_of(ptr, type, member) ({			\
61         const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
62         (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
63 
64 #define nl_list_entry(ptr, type, member) \
65 	nl_container_of(ptr, type, member)
66 
67 #define nl_list_at_tail(pos, head, member) \
68 	((pos)->member.next == (head))
69 
70 #define nl_list_at_head(pos, head, member) \
71 	((pos)->member.prev == (head))
72 
73 #define NL_LIST_HEAD(name) \
74 	struct nl_list_head name = { &(name), &(name) }
75 
76 #define nl_list_first_entry(head, type, member)			\
77 	nl_list_entry((head)->next, type, member)
78 
79 #define nl_list_for_each_entry(pos, head, member)				\
80 	for (pos = nl_list_entry((head)->next, typeof(*pos), member);	\
81 	     &(pos)->member != (head); 	\
82 	     (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
83 
84 #define nl_list_for_each_entry_safe(pos, n, head, member)			\
85 	for (pos = nl_list_entry((head)->next, typeof(*pos), member),	\
86 		n = nl_list_entry(pos->member.next, typeof(*pos), member);	\
87 	     &(pos)->member != (head); 					\
88 	     pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
89 
90 #define nl_init_list_head(head) \
91 	do { (head)->next = (head); (head)->prev = (head); } while (0)
92 
93 #endif
94