libnl  1.1
list.h
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 
16 {
17  struct nl_list_head * next;
18  struct nl_list_head * prev;
19 };
20 
21 
22 static 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 
32 static 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 
38 static 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 
44 static 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 
50 static 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_for_each_entry(pos, head, member) \
72  for (pos = nl_list_entry((head)->next, typeof(*pos), member); \
73  &(pos)->member != (head); \
74  (pos) = nl_list_entry((pos)->member.next, typeof(*(pos)), member))
75 
76 #define nl_list_for_each_entry_safe(pos, n, head, member) \
77  for (pos = nl_list_entry((head)->next, typeof(*pos), member), \
78  n = nl_list_entry(pos->member.next, typeof(*pos), member); \
79  &(pos)->member != (head); \
80  pos = n, n = nl_list_entry(n->member.next, typeof(*n), member))
81 
82 #define nl_init_list_head(head) \
83  do { (head)->next = (head); (head)->prev = (head); } while (0)
84 
85 #endif