1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #ifndef NETLINK_LIST_H_
7 #define NETLINK_LIST_H_
8
9 #include <stddef.h>
10
11 struct nl_list_head
12 {
13 struct nl_list_head * next;
14 struct nl_list_head * prev;
15 };
16
NL_INIT_LIST_HEAD(struct nl_list_head * list)17 static inline void NL_INIT_LIST_HEAD(struct nl_list_head *list)
18 {
19 list->next = list;
20 list->prev = list;
21 }
22
__nl_list_add(struct nl_list_head * obj,struct nl_list_head * prev,struct nl_list_head * next)23 static inline void __nl_list_add(struct nl_list_head *obj,
24 struct nl_list_head *prev,
25 struct nl_list_head *next)
26 {
27 prev->next = obj;
28 obj->prev = prev;
29 next->prev = obj;
30 obj->next = next;
31 }
32
nl_list_add_tail(struct nl_list_head * obj,struct nl_list_head * head)33 static inline void nl_list_add_tail(struct nl_list_head *obj,
34 struct nl_list_head *head)
35 {
36 __nl_list_add(obj, head->prev, head);
37 }
38
nl_list_add_head(struct nl_list_head * obj,struct nl_list_head * head)39 static inline void nl_list_add_head(struct nl_list_head *obj,
40 struct nl_list_head *head)
41 {
42 __nl_list_add(obj, head, head->next);
43 }
44
nl_list_del(struct nl_list_head * obj)45 static inline void nl_list_del(struct nl_list_head *obj)
46 {
47 obj->next->prev = obj->prev;
48 obj->prev->next = obj->next;
49 }
50
nl_list_empty(struct nl_list_head * head)51 static inline int nl_list_empty(struct nl_list_head *head)
52 {
53 return head->next == head;
54 }
55
56 #define nl_container_of(ptr, type, member) ({ \
57 const __typeof__( ((type *)0)->member ) *__mptr = (ptr);\
58 (type *)( (char *)__mptr - (offsetof(type, member)));})
59
60 #define nl_list_entry(ptr, type, member) \
61 nl_container_of(ptr, type, member)
62
63 #define nl_list_at_tail(pos, head, member) \
64 ((pos)->member.next == (head))
65
66 #define nl_list_at_head(pos, head, member) \
67 ((pos)->member.prev == (head))
68
69 #define NL_LIST_HEAD(name) \
70 struct nl_list_head name = { &(name), &(name) }
71
72 #define nl_list_first_entry(head, type, member) \
73 nl_list_entry((head)->next, type, member)
74
75 #define nl_list_for_each_entry(pos, head, member) \
76 for (pos = nl_list_entry((head)->next, __typeof__(*pos), member); \
77 &(pos)->member != (head); \
78 (pos) = nl_list_entry((pos)->member.next, __typeof__(*(pos)), member))
79
80 #define nl_list_for_each_entry_safe(pos, n, head, member) \
81 for (pos = nl_list_entry((head)->next, __typeof__(*pos), member), \
82 n = nl_list_entry(pos->member.next, __typeof__(*pos), member); \
83 &(pos)->member != (head); \
84 pos = n, n = nl_list_entry(n->member.next, __typeof__(*n), member))
85
86 #define nl_init_list_head(head) \
87 do { (head)->next = (head); (head)->prev = (head); } while (0)
88
89 #endif
90