Commit | Line | Data |
---|---|---|
d3d3857f MJ |
1 | // SPDX-FileCopyrightText: 2009 Novell Inc. |
2 | // SPDX-FileCopyrightText: 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | // | |
4 | // SPDX-License-Identifier: LGPL-2.1-only | |
5 | ||
63ff4873 MD |
6 | #ifndef _KCOMPAT_HLIST_H |
7 | #define _KCOMPAT_HLIST_H | |
8 | ||
9 | /* | |
10 | * Kernel sourcecode compatible lightweight single pointer list head useful | |
11 | * for implementing hash tables | |
12 | * | |
63ff4873 | 13 | * Author: Jan Blunck <jblunck@suse.de> |
63ff4873 MD |
14 | */ |
15 | ||
9056e3b8 | 16 | #include <stddef.h> |
a0b5ef68 | 17 | #include <urcu/compiler.h> |
9056e3b8 | 18 | |
6734fec1 | 19 | struct cds_hlist_head { |
16aa9ee8 | 20 | struct cds_hlist_node *next; |
63ff4873 MD |
21 | }; |
22 | ||
6734fec1 MD |
23 | struct cds_hlist_node { |
24 | struct cds_hlist_node *next, *prev; | |
63ff4873 MD |
25 | }; |
26 | ||
6734fec1 MD |
27 | /* Initialize a new list head. */ |
28 | static inline | |
29 | void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr) | |
63ff4873 MD |
30 | { |
31 | ptr->next = NULL; | |
32 | } | |
33 | ||
d7a0f2fe MD |
34 | #define CDS_HLIST_HEAD(name) \ |
35 | struct cds_hlist_head name = { NULL } | |
36 | ||
37 | #define CDS_HLIST_HEAD_INIT(name) \ | |
38 | { .next = NULL } | |
39 | ||
6734fec1 | 40 | /* Get typed element from list at a given position. */ |
a0b5ef68 MD |
41 | #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr, type, member) |
42 | ||
43 | /* Get typed element from list at a given position, keeping NULL pointers. */ | |
44 | #define cds_hlist_entry_safe(ptr, type, member) \ | |
45 | ({ \ | |
c9f0b5df | 46 | __typeof__(ptr) ____ret = (ptr); \ |
a0b5ef68 MD |
47 | ____ret ? cds_hlist_entry(____ret, type, member) : NULL; \ |
48 | }) | |
63ff4873 | 49 | |
6734fec1 MD |
50 | /* Add new element at the head of the list. */ |
51 | static inline | |
52 | void cds_hlist_add_head(struct cds_hlist_node *newp, | |
53 | struct cds_hlist_head *head) | |
63ff4873 MD |
54 | { |
55 | if (head->next) | |
56 | head->next->prev = newp; | |
63ff4873 | 57 | newp->next = head->next; |
6734fec1 | 58 | newp->prev = (struct cds_hlist_node *) head; |
63ff4873 MD |
59 | head->next = newp; |
60 | } | |
61 | ||
6734fec1 MD |
62 | /* Remove element from list. */ |
63 | static inline | |
64 | void cds_hlist_del(struct cds_hlist_node *elem) | |
63ff4873 MD |
65 | { |
66 | if (elem->next) | |
67 | elem->next->prev = elem->prev; | |
63ff4873 MD |
68 | elem->prev->next = elem->next; |
69 | } | |
70 | ||
d7a0f2fe | 71 | #define cds_hlist_for_each(pos, head) \ |
c9f0b5df | 72 | for (pos = (head)->next; (pos) != NULL; pos = (pos)->next) |
d7a0f2fe MD |
73 | |
74 | #define cds_hlist_for_each_safe(pos, p, head) \ | |
75 | for (pos = (head)->next; \ | |
c9f0b5df MD |
76 | ((pos) != NULL) && (p = (pos)->next, 1); \ |
77 | pos = (p)) | |
d7a0f2fe MD |
78 | |
79 | /* | |
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. | |
84 | */ | |
6734fec1 MD |
85 | #define cds_hlist_for_each_entry(entry, pos, head, member) \ |
86 | for (pos = (head)->next, \ | |
c9f0b5df MD |
87 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \ |
88 | (pos) != NULL; \ | |
89 | pos = (pos)->next, \ | |
90 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member)) | |
63ff4873 | 91 | |
6734fec1 MD |
92 | #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \ |
93 | for (pos = (head)->next, \ | |
c9f0b5df MD |
94 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \ |
95 | ((pos) != NULL) && (p = (pos)->next, 1); \ | |
96 | pos = (p), \ | |
6734fec1 | 97 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) |
63ff4873 | 98 | |
d7a0f2fe | 99 | #define cds_hlist_for_each_entry_2(entry, head, member) \ |
c9f0b5df MD |
100 | for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*(entry)), member); \ |
101 | (entry) != NULL; \ | |
102 | entry = cds_hlist_entry_safe((entry)->member.next, __typeof__(*(entry)), member)) | |
d7a0f2fe MD |
103 | |
104 | #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \ | |
c9f0b5df MD |
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); \ | |
d7a0f2fe MD |
108 | entry = e) |
109 | ||
63ff4873 | 110 | #endif /* _KCOMPAT_HLIST_H */ |