2 * netlink/list.h Netlink List Utilities
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_LIST_H_
13 #define NETLINK_LIST_H_
19 struct nl_list_head
* next
;
20 struct nl_list_head
* prev
;
24 static inline void __nl_list_add(struct nl_list_head
*obj
,
25 struct nl_list_head
*prev
,
26 struct nl_list_head
*next
)
34 static inline void nl_list_add_tail(struct nl_list_head
*obj
,
35 struct nl_list_head
*head
)
37 __nl_list_add(obj
, head
->prev
, head
);
40 static inline void nl_list_add_head(struct nl_list_head
*obj
,
41 struct nl_list_head
*head
)
43 __nl_list_add(obj
, head
, head
->next
);
46 static inline void nl_list_del(struct nl_list_head
*obj
)
48 obj
->next
->prev
= obj
->prev
;
49 obj
->prev
->next
= obj
->next
;
52 static inline int nl_list_empty(struct nl_list_head
*head
)
54 return head
->next
== head
;
57 #define nl_container_of(ptr, type, member) ({ \
58 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
59 (type *) ((char *) __mptr - (offsetof(type, member)));})
61 #define nl_list_entry(ptr, type, member) \
62 nl_container_of(ptr, type, member)
64 #define nl_list_at_tail(pos, head, member) \
65 ((pos)->member.next == (head))
67 #define nl_list_at_head(pos, head, member) \
68 ((pos)->member.prev == (head))
70 #define NL_LIST_HEAD(name) \
71 struct nl_list_head name = { &(name), &(name) }
73 #define nl_list_first_entry(head, type, member) \
74 nl_list_entry((head)->next, type, member)
76 #define nl_list_for_each_entry(pos, head, member) \
77 for (pos = nl_list_entry((head)->next, typeof(*pos), member); \
78 &(pos)->member != (head); \
79 (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
81 #define nl_list_for_each_entry_safe(pos, n, head, member) \
82 for (pos = nl_list_entry((head)->next, typeof(*pos), member), \
83 n = nl_list_entry(pos->member.next, typeof(*pos), member); \
84 &(pos)->member != (head); \
85 pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
87 #define nl_init_list_head(head) \
88 do { (head)->next = (head); (head)->prev = (head); } while (0)