1 // SPDX-FileCopyrightText: 2009 Novell Inc.
2 // SPDX-FileCopyrightText: 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 // SPDX-License-Identifier: LGPL-2.1-only
6 #ifndef _KCOMPAT_HLIST_H
7 #define _KCOMPAT_HLIST_H
10 * Kernel sourcecode compatible lightweight single pointer list head useful
11 * for implementing hash tables
13 * Author: Jan Blunck <jblunck@suse.de>
17 #include <urcu/compiler.h>
19 struct cds_hlist_head
{
20 struct cds_hlist_node
*next
;
23 struct cds_hlist_node
{
24 struct cds_hlist_node
*next
, *prev
;
27 /* Initialize a new list head. */
29 void CDS_INIT_HLIST_HEAD(struct cds_hlist_head
*ptr
)
34 #define CDS_HLIST_HEAD(name) \
35 struct cds_hlist_head name = { NULL }
37 #define CDS_HLIST_HEAD_INIT(name) \
40 /* Get typed element from list at a given position. */
41 #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr, type, member)
43 /* Get typed element from list at a given position, keeping NULL pointers. */
44 #define cds_hlist_entry_safe(ptr, type, member) \
46 __typeof__(ptr) ____ret = (ptr); \
47 ____ret ? cds_hlist_entry(____ret, type, member) : NULL; \
50 /* Add new element at the head of the list. */
52 void cds_hlist_add_head(struct cds_hlist_node
*newp
,
53 struct cds_hlist_head
*head
)
56 head
->next
->prev
= newp
;
57 newp
->next
= head
->next
;
58 newp
->prev
= (struct cds_hlist_node
*) head
;
62 /* Remove element from list. */
64 void cds_hlist_del(struct cds_hlist_node
*elem
)
67 elem
->next
->prev
= elem
->prev
;
68 elem
->prev
->next
= elem
->next
;
71 #define cds_hlist_for_each(pos, head) \
72 for (pos = (head)->next; (pos) != NULL; pos = (pos)->next)
74 #define cds_hlist_for_each_safe(pos, p, head) \
75 for (pos = (head)->next; \
76 ((pos) != NULL) && (p = (pos)->next, 1); \
80 * cds_hlist_for_each_entry and cds_hlist_for_each_entry_safe take
81 * respectively 4 and 5 arguments, while the Linux kernel APIs take 3,
82 * and 4. We implement cds_hlist_for_each_entry_2() and
83 * cds_hlist_for_each_entry_safe_2() to follow the Linux kernel APIs.
85 #define cds_hlist_for_each_entry(entry, pos, head, member) \
86 for (pos = (head)->next, \
87 entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \
90 entry = cds_hlist_entry(pos, __typeof__(*(entry)), member))
92 #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \
93 for (pos = (head)->next, \
94 entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \
95 ((pos) != NULL) && (p = (pos)->next, 1); \
97 entry = cds_hlist_entry(pos, __typeof__(*entry), member))
99 #define cds_hlist_for_each_entry_2(entry, head, member) \
100 for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*(entry)), member); \
102 entry = cds_hlist_entry_safe((entry)->member.next, __typeof__(*(entry)), member))
104 #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \
105 for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*(entry)), member); \
106 ((entry) != NULL) && (e = (cds_hlist_entry_safe((entry)->member.next, \
107 __typeof__(*(entry)), member)), 1); \
110 #endif /* _KCOMPAT_HLIST_H */
This page took 0.035379 seconds and 5 git commands to generate.