list.h revision 535e83162249ed6274ba46bc72d8cc683ba20e17
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
15struct nl_list_head
16{
17	struct nl_list_head *	next;
18	struct nl_list_head *	prev;
19};
20
21
22static inline void __nl_list_add(struct nl_list_head *obj,
23				 struct nl_list_head *prev,
24				 struct nl_list_head *next)
25{
26	prev->next = obj;
27	obj->prev = prev;
28	next->prev = obj;
29	obj->next = next;
30}
31
32static inline void nl_list_add_tail(struct nl_list_head *obj,
33				    struct nl_list_head *head)
34{
35	__nl_list_add(obj, head->prev, head);
36}
37
38static inline void nl_list_add_head(struct nl_list_head *obj,
39				    struct nl_list_head *head)
40{
41	__nl_list_add(obj, head, head->next);
42}
43
44static inline void nl_list_del(struct nl_list_head *obj)
45{
46	obj->next->prev = obj->prev;
47	obj->prev->next = obj->next;
48}
49
50static inline int nl_list_empty(struct nl_list_head *head)
51{
52	return head->next == head;
53}
54
55#define nl_container_of(ptr, type, member) ({			\
56        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
57        (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
58
59#define nl_list_entry(ptr, type, member) \
60	nl_container_of(ptr, type, member)
61
62#define nl_list_at_tail(pos, head, member) \
63	((pos)->member.next == (head))
64
65#define nl_list_at_head(pos, head, member) \
66	((pos)->member.prev == (head))
67
68#define NL_LIST_HEAD(name) \
69	struct nl_list_head name = { &(name), &(name) }
70
71#define nl_list_first_entry(head, type, member)			\
72	nl_list_entry((head)->next, type, member)
73
74#define nl_list_for_each_entry(pos, head, member)				\
75	for (pos = nl_list_entry((head)->next, typeof(*pos), member);	\
76	     &(pos)->member != (head); 	\
77	     (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
78
79#define nl_list_for_each_entry_safe(pos, n, head, member)			\
80	for (pos = nl_list_entry((head)->next, typeof(*pos), member),	\
81		n = nl_list_entry(pos->member.next, typeof(*pos), member);	\
82	     &(pos)->member != (head); 					\
83	     pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
84
85#define nl_init_list_head(head) \
86	do { (head)->next = (head); (head)->prev = (head); } while (0)
87
88#endif
89