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