Commit | Line | Data |
---|---|---|
63ff4873 MD |
1 | #ifndef _KCOMPAT_HLIST_H |
2 | #define _KCOMPAT_HLIST_H | |
3 | ||
4 | /* | |
5 | * Kernel sourcecode compatible lightweight single pointer list head useful | |
6 | * for implementing hash tables | |
7 | * | |
8 | * Copyright (C) 2009 Novell Inc. | |
9 | * | |
10 | * Author: Jan Blunck <jblunck@suse.de> | |
11 | * | |
6734fec1 | 12 | * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5db941e8 | 13 | * |
63ff4873 MD |
14 | * This program is free software; you can redistribute it and/or modify it |
15 | * under the terms of the GNU Lesser General Public License version 2.1 as | |
16 | * published by the Free Software Foundation. | |
17 | */ | |
18 | ||
9056e3b8 | 19 | #include <stddef.h> |
a0b5ef68 | 20 | #include <urcu/compiler.h> |
9056e3b8 | 21 | |
6734fec1 | 22 | struct cds_hlist_head { |
16aa9ee8 | 23 | struct cds_hlist_node *next; |
63ff4873 MD |
24 | }; |
25 | ||
6734fec1 MD |
26 | struct cds_hlist_node { |
27 | struct cds_hlist_node *next, *prev; | |
63ff4873 MD |
28 | }; |
29 | ||
6734fec1 MD |
30 | /* Initialize a new list head. */ |
31 | static inline | |
32 | void CDS_INIT_HLIST_HEAD(struct cds_hlist_head *ptr) | |
63ff4873 MD |
33 | { |
34 | ptr->next = NULL; | |
35 | } | |
36 | ||
d7a0f2fe MD |
37 | #define CDS_HLIST_HEAD(name) \ |
38 | struct cds_hlist_head name = { NULL } | |
39 | ||
40 | #define CDS_HLIST_HEAD_INIT(name) \ | |
41 | { .next = NULL } | |
42 | ||
6734fec1 | 43 | /* Get typed element from list at a given position. */ |
a0b5ef68 MD |
44 | #define cds_hlist_entry(ptr, type, member) caa_container_of(ptr, type, member) |
45 | ||
46 | /* Get typed element from list at a given position, keeping NULL pointers. */ | |
47 | #define cds_hlist_entry_safe(ptr, type, member) \ | |
48 | ({ \ | |
c9f0b5df | 49 | __typeof__(ptr) ____ret = (ptr); \ |
a0b5ef68 MD |
50 | ____ret ? cds_hlist_entry(____ret, type, member) : NULL; \ |
51 | }) | |
63ff4873 | 52 | |
6734fec1 MD |
53 | /* Add new element at the head of the list. */ |
54 | static inline | |
55 | void cds_hlist_add_head(struct cds_hlist_node *newp, | |
56 | struct cds_hlist_head *head) | |
63ff4873 MD |
57 | { |
58 | if (head->next) | |
59 | head->next->prev = newp; | |
63ff4873 | 60 | newp->next = head->next; |
6734fec1 | 61 | newp->prev = (struct cds_hlist_node *) head; |
63ff4873 MD |
62 | head->next = newp; |
63 | } | |
64 | ||
6734fec1 MD |
65 | /* Remove element from list. */ |
66 | static inline | |
67 | void cds_hlist_del(struct cds_hlist_node *elem) | |
63ff4873 MD |
68 | { |
69 | if (elem->next) | |
70 | elem->next->prev = elem->prev; | |
63ff4873 MD |
71 | elem->prev->next = elem->next; |
72 | } | |
73 | ||
d7a0f2fe | 74 | #define cds_hlist_for_each(pos, head) \ |
c9f0b5df | 75 | for (pos = (head)->next; (pos) != NULL; pos = (pos)->next) |
d7a0f2fe MD |
76 | |
77 | #define cds_hlist_for_each_safe(pos, p, head) \ | |
78 | for (pos = (head)->next; \ | |
c9f0b5df MD |
79 | ((pos) != NULL) && (p = (pos)->next, 1); \ |
80 | pos = (p)) | |
d7a0f2fe MD |
81 | |
82 | /* | |
83 | * cds_hlist_for_each_entry and cds_hlist_for_each_entry_safe take | |
84 | * respectively 4 and 5 arguments, while the Linux kernel APIs take 3, | |
85 | * and 4. We implement cds_hlist_for_each_entry_2() and | |
86 | * cds_hlist_for_each_entry_safe_2() to follow the Linux kernel APIs. | |
87 | */ | |
6734fec1 MD |
88 | #define cds_hlist_for_each_entry(entry, pos, head, member) \ |
89 | for (pos = (head)->next, \ | |
c9f0b5df MD |
90 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \ |
91 | (pos) != NULL; \ | |
92 | pos = (pos)->next, \ | |
93 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member)) | |
63ff4873 | 94 | |
6734fec1 MD |
95 | #define cds_hlist_for_each_entry_safe(entry, pos, p, head, member) \ |
96 | for (pos = (head)->next, \ | |
c9f0b5df MD |
97 | entry = cds_hlist_entry(pos, __typeof__(*(entry)), member); \ |
98 | ((pos) != NULL) && (p = (pos)->next, 1); \ | |
99 | pos = (p), \ | |
6734fec1 | 100 | entry = cds_hlist_entry(pos, __typeof__(*entry), member)) |
63ff4873 | 101 | |
d7a0f2fe | 102 | #define cds_hlist_for_each_entry_2(entry, head, member) \ |
c9f0b5df MD |
103 | for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*(entry)), member); \ |
104 | (entry) != NULL; \ | |
105 | entry = cds_hlist_entry_safe((entry)->member.next, __typeof__(*(entry)), member)) | |
d7a0f2fe MD |
106 | |
107 | #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \ | |
c9f0b5df MD |
108 | for (entry = cds_hlist_entry_safe((head)->next, __typeof__(*(entry)), member); \ |
109 | ((entry) != NULL) && (e = (cds_hlist_entry_safe((entry)->member.next, \ | |
110 | __typeof__(*(entry)), member)), 1); \ | |
d7a0f2fe MD |
111 | entry = e) |
112 | ||
63ff4873 | 113 | #endif /* _KCOMPAT_HLIST_H */ |