1 | /* |
---|
2 | * grdxt.c - Three-levels Generic Radix-tree implementation |
---|
3 | * |
---|
4 | * authors Alain Greiner (2016) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <hal_types.h> |
---|
25 | #include <hal_special.h> |
---|
26 | #include <errno.h> |
---|
27 | #include <printk.h> |
---|
28 | #include <vseg.h> |
---|
29 | #include <kmem.h> |
---|
30 | #include <grdxt.h> |
---|
31 | |
---|
32 | ///////////////////////////////// |
---|
33 | error_t grdxt_init( grdxt_t * rt, |
---|
34 | uint32_t ix1_width, |
---|
35 | uint32_t ix2_width, |
---|
36 | uint32_t ix3_width ) |
---|
37 | { |
---|
38 | void ** root; |
---|
39 | kmem_req_t req; |
---|
40 | |
---|
41 | rt->ix1_width = ix1_width; |
---|
42 | rt->ix2_width = ix2_width; |
---|
43 | rt->ix3_width = ix3_width; |
---|
44 | |
---|
45 | // allocates first level array |
---|
46 | req.type = KMEM_GENERIC; |
---|
47 | req.size = sizeof(void *) << ix1_width; |
---|
48 | req.flags = AF_KERNEL | AF_ZERO; |
---|
49 | root = kmem_alloc( &req ); |
---|
50 | if( root == NULL ) return ENOMEM; |
---|
51 | |
---|
52 | rt->root = root; |
---|
53 | |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | ////////////////////////////////// |
---|
58 | void grdxt_destroy( grdxt_t * rt ) |
---|
59 | { |
---|
60 | kmem_req_t req; |
---|
61 | |
---|
62 | uint32_t w1 = rt->ix1_width; |
---|
63 | uint32_t w2 = rt->ix2_width; |
---|
64 | uint32_t w3 = rt->ix3_width; |
---|
65 | |
---|
66 | void ** ptr1 = rt->root; |
---|
67 | void ** ptr2; |
---|
68 | void ** ptr3; |
---|
69 | |
---|
70 | uint32_t ix1; |
---|
71 | uint32_t ix2; |
---|
72 | |
---|
73 | req.type = KMEM_GENERIC; |
---|
74 | |
---|
75 | for( ix1=0 ; ix1 < (1 << w1) ; ix1++ ) |
---|
76 | { |
---|
77 | ptr2 = ptr1[ix1]; |
---|
78 | |
---|
79 | if( ptr2 == NULL ) continue; |
---|
80 | |
---|
81 | for( ix2=0 ; ix2 < (1 << w2) ; ix2++ ) |
---|
82 | { |
---|
83 | ptr3 = ptr2[ix2]; |
---|
84 | |
---|
85 | if( ptr3 == NULL ) continue; |
---|
86 | |
---|
87 | // release level 3 array |
---|
88 | req.ptr = ptr3; |
---|
89 | req.type = KMEM_GENERIC; |
---|
90 | req.size = sizeof(void *) * (1 << w3); |
---|
91 | kmem_free( &req ); |
---|
92 | } |
---|
93 | |
---|
94 | // release level 2 array |
---|
95 | req.ptr = ptr2; |
---|
96 | req.type = KMEM_GENERIC; |
---|
97 | req.size = sizeof(void *) * (1 << w2); |
---|
98 | kmem_free( &req ); |
---|
99 | } |
---|
100 | |
---|
101 | // release level 1 array |
---|
102 | req.ptr = ptr1; |
---|
103 | req.type = KMEM_GENERIC; |
---|
104 | req.size = sizeof(void *) * (1 << w1); |
---|
105 | kmem_free( &req ); |
---|
106 | |
---|
107 | } // end grdxt_destroy() |
---|
108 | |
---|
109 | /////////////////////////////// |
---|
110 | void grdxt_print( grdxt_t * rt, |
---|
111 | char * name ) |
---|
112 | { |
---|
113 | uint32_t ix1; |
---|
114 | uint32_t ix2; |
---|
115 | uint32_t ix3; |
---|
116 | |
---|
117 | uint32_t w1 = rt->ix1_width; |
---|
118 | uint32_t w2 = rt->ix2_width; |
---|
119 | uint32_t w3 = rt->ix3_width; |
---|
120 | |
---|
121 | void ** ptr1 = rt->root; |
---|
122 | void ** ptr2; |
---|
123 | void ** ptr3; |
---|
124 | |
---|
125 | intptr_t key; |
---|
126 | intptr_t value; |
---|
127 | |
---|
128 | printk("\n***** Generic Radix tree %s : n1 = %d / n2 = %d / n3 = %d\n\n", |
---|
129 | name, 1<<w1 , 1<<w2 , 1<<w3 ); |
---|
130 | |
---|
131 | for( ix1=0 ; ix1 < (1<<w1) ; ix1++ ) |
---|
132 | { |
---|
133 | ptr2 = ptr1[ix1]; |
---|
134 | if( ptr2 == NULL ) continue; |
---|
135 | |
---|
136 | for( ix2=0 ; ix2 < (1<<w2) ; ix2++ ) |
---|
137 | { |
---|
138 | ptr3 = ptr2[ix2]; |
---|
139 | if( ptr3 == NULL ) continue; |
---|
140 | |
---|
141 | for( ix3=0 ; ix3 < (1<<w3) ; ix3++ ) |
---|
142 | { |
---|
143 | value = (intptr_t)ptr3[ix3]; |
---|
144 | if( value == 0 ) continue; |
---|
145 | |
---|
146 | key = (ix1<<(w2+w3)) + (ix2<<w3) + ix3; |
---|
147 | printk(" - key = %x / value = %x\n", key , value ); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } // end grdxt_print() |
---|
152 | |
---|
153 | //////////////////////////////////// |
---|
154 | error_t grdxt_insert( grdxt_t * rt, |
---|
155 | uint32_t key, |
---|
156 | void * value ) |
---|
157 | { |
---|
158 | kmem_req_t req; |
---|
159 | |
---|
160 | uint32_t w1 = rt->ix1_width; |
---|
161 | uint32_t w2 = rt->ix2_width; |
---|
162 | uint32_t w3 = rt->ix3_width; |
---|
163 | |
---|
164 | // Check key |
---|
165 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
166 | { |
---|
167 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
168 | } |
---|
169 | |
---|
170 | // compute indexes |
---|
171 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
172 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
173 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
174 | |
---|
175 | void ** ptr1 = rt->root; // pointer on level 1 array |
---|
176 | void ** ptr2; // pointer on level 2 array |
---|
177 | void ** ptr3; // pointer on level 3 array |
---|
178 | |
---|
179 | // If required, we must allocate memory for the selected level 2 array, |
---|
180 | // and atomically update the level 1 array. |
---|
181 | if( ptr1[ix1] == NULL ) |
---|
182 | { |
---|
183 | // allocate memory for level 2 array |
---|
184 | req.type = KMEM_GENERIC; |
---|
185 | req.size = sizeof(void *) << w2; |
---|
186 | req.flags = AF_KERNEL | AF_ZERO; |
---|
187 | ptr2 = kmem_alloc( &req ); |
---|
188 | if( ptr2 == NULL) return ENOMEM; |
---|
189 | |
---|
190 | // update level 1 array |
---|
191 | ptr1[ix1] = ptr2; |
---|
192 | } |
---|
193 | else // get pointer on selected level 2 array. |
---|
194 | { |
---|
195 | ptr2 = ptr1[ix1]; |
---|
196 | } |
---|
197 | |
---|
198 | // If required, we must allocate memory for the selected level 3 array, |
---|
199 | // and atomically update the level 2 array. |
---|
200 | if( ptr2[ix2] == NULL ) |
---|
201 | { |
---|
202 | // allocate memory for level 3 array |
---|
203 | req.type = KMEM_GENERIC; |
---|
204 | req.size = sizeof(void *) << w3; |
---|
205 | req.flags = AF_KERNEL | AF_ZERO; |
---|
206 | ptr3 = kmem_alloc( &req ); |
---|
207 | if( ptr3 == NULL) return ENOMEM; |
---|
208 | |
---|
209 | // update level 3 array |
---|
210 | ptr2[ix2] = ptr3; |
---|
211 | } |
---|
212 | else // get pointer on selected level 3 array. |
---|
213 | { |
---|
214 | ptr3 = ptr2[ix2]; |
---|
215 | } |
---|
216 | |
---|
217 | // selected slot in level 3 array must be empty |
---|
218 | if( ptr3[ix3] != NULL ) return EEXIST; |
---|
219 | |
---|
220 | // register the value |
---|
221 | ptr3[ix3] = value; |
---|
222 | hal_fence(); |
---|
223 | |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | /////////////////////////////////// |
---|
228 | void * grdxt_remove( grdxt_t * rt, |
---|
229 | uint32_t key ) |
---|
230 | { |
---|
231 | uint32_t w1 = rt->ix1_width; |
---|
232 | uint32_t w2 = rt->ix2_width; |
---|
233 | uint32_t w3 = rt->ix3_width; |
---|
234 | |
---|
235 | // Check key |
---|
236 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
237 | { |
---|
238 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
239 | } |
---|
240 | |
---|
241 | // compute indexes |
---|
242 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
243 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
244 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
245 | |
---|
246 | void ** ptr1 = rt->root; // pointer on level 1 array |
---|
247 | void ** ptr2; // pointer on level 2 array |
---|
248 | void ** ptr3; // pointer on level 3 array |
---|
249 | |
---|
250 | // get ptr2 |
---|
251 | ptr2 = ptr1[ix1]; |
---|
252 | if( ptr2 == NULL ) return NULL; |
---|
253 | |
---|
254 | // get ptr3 |
---|
255 | ptr3 = ptr2[ix2]; |
---|
256 | if( ptr3 == NULL ) return NULL; |
---|
257 | |
---|
258 | // get value |
---|
259 | void * value = ptr3[ix3]; |
---|
260 | |
---|
261 | // reset selected slot |
---|
262 | ptr3[ix3] = NULL; |
---|
263 | hal_fence(); |
---|
264 | |
---|
265 | return value; |
---|
266 | } |
---|
267 | |
---|
268 | /////////////////////////////////// |
---|
269 | void * grdxt_lookup( grdxt_t * rt, |
---|
270 | uint32_t key ) |
---|
271 | { |
---|
272 | uint32_t w1 = rt->ix1_width; |
---|
273 | uint32_t w2 = rt->ix2_width; |
---|
274 | uint32_t w3 = rt->ix3_width; |
---|
275 | |
---|
276 | // Check key |
---|
277 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
278 | { |
---|
279 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
280 | } |
---|
281 | |
---|
282 | void ** ptr1 = rt->root; |
---|
283 | void ** ptr2; |
---|
284 | void ** ptr3; |
---|
285 | |
---|
286 | // compute indexes |
---|
287 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
288 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
289 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
290 | |
---|
291 | // get ptr2 |
---|
292 | ptr2 = ptr1[ix1]; |
---|
293 | if( ptr2 == NULL ) return NULL; |
---|
294 | |
---|
295 | // get ptr3 |
---|
296 | ptr3 = ptr2[ix2]; |
---|
297 | if( ptr3 == NULL ) return NULL; |
---|
298 | |
---|
299 | // get value |
---|
300 | void * value = ptr3[ix3]; |
---|
301 | |
---|
302 | return value; |
---|
303 | } |
---|
304 | |
---|
305 | ////////////////////////////////////// |
---|
306 | void * grdxt_get_first( grdxt_t * rt, |
---|
307 | uint32_t start_key, |
---|
308 | uint32_t * found_key ) |
---|
309 | { |
---|
310 | uint32_t ix1; |
---|
311 | uint32_t ix2; |
---|
312 | uint32_t ix3; |
---|
313 | |
---|
314 | uint32_t w1 = rt->ix1_width; |
---|
315 | uint32_t w2 = rt->ix2_width; |
---|
316 | uint32_t w3 = rt->ix3_width; |
---|
317 | |
---|
318 | // Check start_key |
---|
319 | if( (start_key >> (w1 + w2 + w3)) != 0 ) |
---|
320 | { |
---|
321 | panic("start_key value %x exceed (%d + %d + %d) bits", start_key , w1 , |
---|
322 | w2 , w3 ); |
---|
323 | } |
---|
324 | |
---|
325 | // compute max indexes |
---|
326 | uint32_t max1 = 1 << w1; |
---|
327 | uint32_t max2 = 1 << w2; |
---|
328 | uint32_t max3 = 1 << w3; |
---|
329 | |
---|
330 | // compute min indexes |
---|
331 | uint32_t min1 = start_key >> (w2 + w3); |
---|
332 | uint32_t min2 = (start_key >> w3) & ((1 << w2) -1); |
---|
333 | uint32_t min3 = start_key & ((1 << w3) - 1); |
---|
334 | |
---|
335 | void ** ptr1 = rt->root; |
---|
336 | void ** ptr2; |
---|
337 | void ** ptr3; |
---|
338 | |
---|
339 | for( ix1 = min1 ; ix1 < max1 ; ix1++ ) |
---|
340 | { |
---|
341 | ptr2 = ptr1[ix1]; |
---|
342 | if( ptr2 == NULL ) continue; |
---|
343 | |
---|
344 | for( ix2 = min2 ; ix2 < max2 ; ix2++ ) |
---|
345 | { |
---|
346 | ptr3 = ptr2[ix2]; |
---|
347 | if( ptr3 == NULL ) continue; |
---|
348 | |
---|
349 | for( ix3 = min3 ; ix3 < max3 ; ix3++ ) |
---|
350 | { |
---|
351 | if( ptr3[ix3] == NULL ) continue; |
---|
352 | else |
---|
353 | { |
---|
354 | *found_key = (ix1 << (w2+w3)) | (ix2 << w1) | ix3; |
---|
355 | return ptr3[ix3]; |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | return NULL; |
---|
362 | } |
---|