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