1#ifndef _LINUX_GENERIC_RADIX_TREE_H
2#define _LINUX_GENERIC_RADIX_TREE_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <asm/page.h>
40#include <linux/bug.h>
41#include <linux/kernel.h>
42#include <linux/log2.h>
43
44struct genradix_root;
45
46struct __genradix {
47 struct genradix_root *root;
48};
49
50
51
52
53
54#define __GENRADIX_INITIALIZER \
55 { \
56 .tree = { \
57 .root = NULL, \
58 } \
59 }
60
61
62
63
64
65
66
67
68
69#define GENRADIX(_type) \
70struct { \
71 struct __genradix tree; \
72 _type type[0] __aligned(1); \
73}
74
75#define DEFINE_GENRADIX(_name, _type) \
76 GENRADIX(_type) _name = __GENRADIX_INITIALIZER
77
78
79
80
81
82
83
84#define genradix_init(_radix) \
85do { \
86 *(_radix) = (typeof(*_radix)) __GENRADIX_INITIALIZER; \
87} while (0)
88
89void __genradix_free(struct __genradix *);
90
91
92
93
94
95
96
97#define genradix_free(_radix) __genradix_free(&(_radix)->tree)
98
99static inline size_t __idx_to_offset(size_t idx, size_t obj_size)
100{
101 if (__builtin_constant_p(obj_size))
102 BUILD_BUG_ON(obj_size > PAGE_SIZE);
103 else
104 BUG_ON(obj_size > PAGE_SIZE);
105
106 if (!is_power_of_2(obj_size)) {
107 size_t objs_per_page = PAGE_SIZE / obj_size;
108
109 return (idx / objs_per_page) * PAGE_SIZE +
110 (idx % objs_per_page) * obj_size;
111 } else {
112 return idx * obj_size;
113 }
114}
115
116#define __genradix_cast(_radix) (typeof((_radix)->type[0]) *)
117#define __genradix_obj_size(_radix) sizeof((_radix)->type[0])
118#define __genradix_idx_to_offset(_radix, _idx) \
119 __idx_to_offset(_idx, __genradix_obj_size(_radix))
120
121void *__genradix_ptr(struct __genradix *, size_t);
122
123
124
125
126
127
128
129
130#define genradix_ptr(_radix, _idx) \
131 (__genradix_cast(_radix) \
132 __genradix_ptr(&(_radix)->tree, \
133 __genradix_idx_to_offset(_radix, _idx)))
134
135void *__genradix_ptr_alloc(struct __genradix *, size_t, gfp_t);
136
137
138
139
140
141
142
143
144
145
146#define genradix_ptr_alloc(_radix, _idx, _gfp) \
147 (__genradix_cast(_radix) \
148 __genradix_ptr_alloc(&(_radix)->tree, \
149 __genradix_idx_to_offset(_radix, _idx), \
150 _gfp))
151
152struct genradix_iter {
153 size_t offset;
154 size_t pos;
155};
156
157
158
159
160
161
162#define genradix_iter_init(_radix, _idx) \
163 ((struct genradix_iter) { \
164 .pos = (_idx), \
165 .offset = __genradix_idx_to_offset((_radix), (_idx)),\
166 })
167
168void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);
169
170
171
172
173
174
175
176
177
178#define genradix_iter_peek(_iter, _radix) \
179 (__genradix_cast(_radix) \
180 __genradix_iter_peek(_iter, &(_radix)->tree, \
181 PAGE_SIZE / __genradix_obj_size(_radix)))
182
183static inline void __genradix_iter_advance(struct genradix_iter *iter,
184 size_t obj_size)
185{
186 iter->offset += obj_size;
187
188 if (!is_power_of_2(obj_size) &&
189 (iter->offset & (PAGE_SIZE - 1)) + obj_size > PAGE_SIZE)
190 iter->offset = round_up(iter->offset, PAGE_SIZE);
191
192 iter->pos++;
193}
194
195#define genradix_iter_advance(_iter, _radix) \
196 __genradix_iter_advance(_iter, __genradix_obj_size(_radix))
197
198#define genradix_for_each_from(_radix, _iter, _p, _start) \
199 for (_iter = genradix_iter_init(_radix, _start); \
200 (_p = genradix_iter_peek(&_iter, _radix)) != NULL; \
201 genradix_iter_advance(&_iter, _radix))
202
203
204
205
206
207
208
209
210
211
212#define genradix_for_each(_radix, _iter, _p) \
213 genradix_for_each_from(_radix, _iter, _p, 0)
214
215int __genradix_prealloc(struct __genradix *, size_t, gfp_t);
216
217
218
219
220
221
222
223
224
225#define genradix_prealloc(_radix, _nr, _gfp) \
226 __genradix_prealloc(&(_radix)->tree, \
227 __genradix_idx_to_offset(_radix, _nr + 1),\
228 _gfp)
229
230
231#endif
232