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 <kmem.h> |
---|
29 | #include <grdxt.h> |
---|
30 | |
---|
31 | ///////////////////////////////// |
---|
32 | error_t grdxt_init( grdxt_t * rt, |
---|
33 | uint32_t ix1_width, |
---|
34 | uint32_t ix2_width, |
---|
35 | uint32_t ix3_width ) |
---|
36 | { |
---|
37 | void ** root; |
---|
38 | kmem_req_t req; |
---|
39 | |
---|
40 | rt->ix1_width = ix1_width; |
---|
41 | rt->ix2_width = ix2_width; |
---|
42 | rt->ix3_width = ix3_width; |
---|
43 | |
---|
44 | // allocates first level array |
---|
45 | req.type = KMEM_GENERIC; |
---|
46 | req.size = sizeof(void *) << ix1_width; |
---|
47 | req.flags = AF_KERNEL | AF_ZERO; |
---|
48 | root = kmem_alloc( &req ); |
---|
49 | if( root == NULL ) return ENOMEM; |
---|
50 | |
---|
51 | rt->root = root; |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | ////////////////////////////////// |
---|
57 | void grdxt_destroy( grdxt_t * rt ) |
---|
58 | { |
---|
59 | kmem_req_t req; |
---|
60 | |
---|
61 | uint32_t w1 = rt->ix1_width; |
---|
62 | uint32_t w2 = rt->ix2_width; |
---|
63 | uint32_t w3 = rt->ix3_width; |
---|
64 | |
---|
65 | void ** ptr1 = rt->root; |
---|
66 | void ** ptr2; |
---|
67 | void ** ptr3; |
---|
68 | |
---|
69 | uint32_t ix1; |
---|
70 | uint32_t ix2; |
---|
71 | |
---|
72 | req.type = KMEM_GENERIC; |
---|
73 | |
---|
74 | for( ix1=0 ; ix1 < (1 << w1) ; ix1++ ) |
---|
75 | { |
---|
76 | ptr2 = ptr1[ix1]; |
---|
77 | |
---|
78 | if( ptr2 == NULL ) continue; |
---|
79 | |
---|
80 | for( ix2=0 ; ix2 < (1 << w2) ; ix2++ ) |
---|
81 | { |
---|
82 | ptr3 = ptr2[ix2]; |
---|
83 | |
---|
84 | if( ptr3 == NULL ) continue; |
---|
85 | |
---|
86 | // release level 3 array |
---|
87 | req.ptr = ptr3; |
---|
88 | req.type = KMEM_GENERIC; |
---|
89 | req.size = sizeof(void *) * (1 << w3); |
---|
90 | kmem_free( &req ); |
---|
91 | } |
---|
92 | |
---|
93 | // release level 2 array |
---|
94 | req.ptr = ptr2; |
---|
95 | req.type = KMEM_GENERIC; |
---|
96 | req.size = sizeof(void *) * (1 << w2); |
---|
97 | kmem_free( &req ); |
---|
98 | } |
---|
99 | |
---|
100 | // release level 1 array |
---|
101 | req.ptr = ptr1; |
---|
102 | req.type = KMEM_GENERIC; |
---|
103 | req.size = sizeof(void *) * (1 << w1); |
---|
104 | kmem_free( &req ); |
---|
105 | |
---|
106 | } // end grdxt_destroy() |
---|
107 | |
---|
108 | /////////////////////////////// |
---|
109 | void grdxt_print( grdxt_t * rt, |
---|
110 | char * name ) |
---|
111 | { |
---|
112 | uint32_t ix1; |
---|
113 | uint32_t ix2; |
---|
114 | uint32_t ix3; |
---|
115 | |
---|
116 | uint32_t w1 = rt->ix1_width; |
---|
117 | uint32_t w2 = rt->ix2_width; |
---|
118 | uint32_t w3 = rt->ix3_width; |
---|
119 | |
---|
120 | void ** ptr1 = rt->root; |
---|
121 | void ** ptr2; |
---|
122 | void ** ptr3; |
---|
123 | |
---|
124 | intptr_t key; |
---|
125 | intptr_t value; |
---|
126 | |
---|
127 | printk("*** %s : n1 = %d / n2 = %d / n3 = %d\n", |
---|
128 | name, 1<<w1 , 1<<w2 , 1<<w3 ); |
---|
129 | |
---|
130 | for( ix1=0 ; ix1 < (1<<w1) ; ix1++ ) |
---|
131 | { |
---|
132 | ptr2 = ptr1[ix1]; |
---|
133 | if( ptr2 == NULL ) continue; |
---|
134 | |
---|
135 | for( ix2=0 ; ix2 < (1<<w2) ; ix2++ ) |
---|
136 | { |
---|
137 | ptr3 = ptr2[ix2]; |
---|
138 | if( ptr3 == NULL ) continue; |
---|
139 | |
---|
140 | for( ix3=0 ; ix3 < (1<<w3) ; ix3++ ) |
---|
141 | { |
---|
142 | value = (intptr_t)ptr3[ix3]; |
---|
143 | if( value == 0 ) continue; |
---|
144 | |
---|
145 | key = (ix1<<(w2+w3)) + (ix2<<w3) + ix3; |
---|
146 | printk(" - key = %x / value = %x\n", key , value ); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | } // end grdxt_print() |
---|
151 | |
---|
152 | //////////////////////////////////// |
---|
153 | error_t grdxt_insert( grdxt_t * rt, |
---|
154 | uint32_t key, |
---|
155 | void * value ) |
---|
156 | { |
---|
157 | kmem_req_t req; |
---|
158 | |
---|
159 | uint32_t w1 = rt->ix1_width; |
---|
160 | uint32_t w2 = rt->ix2_width; |
---|
161 | uint32_t w3 = rt->ix3_width; |
---|
162 | |
---|
163 | // Check key |
---|
164 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
165 | { |
---|
166 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
167 | } |
---|
168 | |
---|
169 | // compute indexes |
---|
170 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
171 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
172 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
173 | |
---|
174 | void ** ptr1 = rt->root; // pointer on level 1 array |
---|
175 | void ** ptr2; // pointer on level 2 array |
---|
176 | void ** ptr3; // pointer on level 3 array |
---|
177 | |
---|
178 | // If required, we must allocate memory for the selected level 2 array, |
---|
179 | // and atomically update the level 1 array. |
---|
180 | if( ptr1[ix1] == NULL ) |
---|
181 | { |
---|
182 | // allocate memory for level 2 array |
---|
183 | req.type = KMEM_GENERIC; |
---|
184 | req.size = sizeof(void *) << w2; |
---|
185 | req.flags = AF_KERNEL | AF_ZERO; |
---|
186 | ptr2 = kmem_alloc( &req ); |
---|
187 | if( ptr2 == NULL) return ENOMEM; |
---|
188 | |
---|
189 | // update level 1 array |
---|
190 | ptr1[ix1] = ptr2; |
---|
191 | } |
---|
192 | else // get pointer on selected level 2 array. |
---|
193 | { |
---|
194 | ptr2 = ptr1[ix1]; |
---|
195 | } |
---|
196 | |
---|
197 | // If required, we must allocate memory for the selected level 3 array, |
---|
198 | // and atomically update the level 2 array. |
---|
199 | if( ptr2[ix2] == NULL ) |
---|
200 | { |
---|
201 | // allocate memory for level 3 array |
---|
202 | req.type = KMEM_GENERIC; |
---|
203 | req.size = sizeof(void *) << w3; |
---|
204 | req.flags = AF_KERNEL | AF_ZERO; |
---|
205 | ptr3 = kmem_alloc( &req ); |
---|
206 | if( ptr3 == NULL) return ENOMEM; |
---|
207 | |
---|
208 | // update level 3 array |
---|
209 | ptr2[ix2] = ptr3; |
---|
210 | } |
---|
211 | else // get pointer on selected level 3 array. |
---|
212 | { |
---|
213 | ptr3 = ptr2[ix2]; |
---|
214 | } |
---|
215 | |
---|
216 | // selected slot in level 3 array must be empty |
---|
217 | if( ptr3[ix3] != NULL ) return EEXIST; |
---|
218 | |
---|
219 | // register the value |
---|
220 | ptr3[ix3] = value; |
---|
221 | hal_fence(); |
---|
222 | |
---|
223 | return 0; |
---|
224 | } |
---|
225 | |
---|
226 | /////////////////////////////////// |
---|
227 | void * grdxt_remove( grdxt_t * rt, |
---|
228 | uint32_t key ) |
---|
229 | { |
---|
230 | uint32_t w1 = rt->ix1_width; |
---|
231 | uint32_t w2 = rt->ix2_width; |
---|
232 | uint32_t w3 = rt->ix3_width; |
---|
233 | |
---|
234 | // Check key |
---|
235 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
236 | { |
---|
237 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
238 | } |
---|
239 | |
---|
240 | // compute indexes |
---|
241 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
242 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
243 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
244 | |
---|
245 | void ** ptr1 = rt->root; // pointer on level 1 array |
---|
246 | void ** ptr2; // pointer on level 2 array |
---|
247 | void ** ptr3; // pointer on level 3 array |
---|
248 | |
---|
249 | // get ptr2 |
---|
250 | ptr2 = ptr1[ix1]; |
---|
251 | if( ptr2 == NULL ) return NULL; |
---|
252 | |
---|
253 | // get ptr3 |
---|
254 | ptr3 = ptr2[ix2]; |
---|
255 | if( ptr3 == NULL ) return NULL; |
---|
256 | |
---|
257 | // get value |
---|
258 | void * value = ptr3[ix3]; |
---|
259 | |
---|
260 | // reset selected slot |
---|
261 | ptr3[ix3] = NULL; |
---|
262 | hal_fence(); |
---|
263 | |
---|
264 | return value; |
---|
265 | } |
---|
266 | |
---|
267 | /////////////////////////////////// |
---|
268 | void * grdxt_lookup( grdxt_t * rt, |
---|
269 | uint32_t key ) |
---|
270 | { |
---|
271 | uint32_t w1 = rt->ix1_width; |
---|
272 | uint32_t w2 = rt->ix2_width; |
---|
273 | uint32_t w3 = rt->ix3_width; |
---|
274 | |
---|
275 | // Check key |
---|
276 | if( (key >> (w1 + w2 + w3)) != 0 ) |
---|
277 | { |
---|
278 | panic("key value %x exceed (%d + %d + %d) bits", key , w1 , w2 , w3 ); |
---|
279 | } |
---|
280 | |
---|
281 | void ** ptr1 = rt->root; |
---|
282 | void ** ptr2; |
---|
283 | void ** ptr3; |
---|
284 | |
---|
285 | // compute indexes |
---|
286 | uint32_t ix1 = key >> (w2 + w3); // index in level 1 array |
---|
287 | uint32_t ix2 = (key >> w3) & ((1 << w2) -1); // index in level 2 array |
---|
288 | uint32_t ix3 = key & ((1 << w3) - 1); // index in level 3 array |
---|
289 | |
---|
290 | // get ptr2 |
---|
291 | ptr2 = ptr1[ix1]; |
---|
292 | if( ptr2 == NULL ) return NULL; |
---|
293 | |
---|
294 | // get ptr3 |
---|
295 | ptr3 = ptr2[ix2]; |
---|
296 | if( ptr3 == NULL ) return NULL; |
---|
297 | |
---|
298 | // get value |
---|
299 | void * value = ptr3[ix3]; |
---|
300 | |
---|
301 | return value; |
---|
302 | } |
---|
303 | |
---|
304 | ////////////////////////////////////// |
---|
305 | void * grdxt_get_first( grdxt_t * rt, |
---|
306 | uint32_t start_key, |
---|
307 | uint32_t * found_key ) |
---|
308 | { |
---|
309 | uint32_t ix1; |
---|
310 | uint32_t ix2; |
---|
311 | uint32_t ix3; |
---|
312 | |
---|
313 | uint32_t w1 = rt->ix1_width; |
---|
314 | uint32_t w2 = rt->ix2_width; |
---|
315 | uint32_t w3 = rt->ix3_width; |
---|
316 | |
---|
317 | // Check start_key |
---|
318 | if( (start_key >> (w1 + w2 + w3)) != 0 ) |
---|
319 | { |
---|
320 | panic("start_key value %x exceed (%d + %d + %d) bits", start_key , w1 , |
---|
321 | w2 , w3 ); |
---|
322 | } |
---|
323 | |
---|
324 | // compute max indexes |
---|
325 | uint32_t max1 = 1 << w1; |
---|
326 | uint32_t max2 = 1 << w2; |
---|
327 | uint32_t max3 = 1 << w3; |
---|
328 | |
---|
329 | // compute min indexes |
---|
330 | uint32_t min1 = start_key >> (w2 + w3); |
---|
331 | uint32_t min2 = (start_key >> w3) & ((1 << w2) -1); |
---|
332 | uint32_t min3 = start_key & ((1 << w3) - 1); |
---|
333 | |
---|
334 | void ** ptr1 = rt->root; |
---|
335 | void ** ptr2; |
---|
336 | void ** ptr3; |
---|
337 | |
---|
338 | for( ix1 = min1 ; ix1 < max1 ; ix1++ ) |
---|
339 | { |
---|
340 | ptr2 = ptr1[ix1]; |
---|
341 | if( ptr2 == NULL ) continue; |
---|
342 | |
---|
343 | for( ix2 = min2 ; ix2 < max2 ; ix2++ ) |
---|
344 | { |
---|
345 | ptr3 = ptr2[ix2]; |
---|
346 | if( ptr3 == NULL ) continue; |
---|
347 | |
---|
348 | for( ix3 = min3 ; ix3 < max3 ; ix3++ ) |
---|
349 | { |
---|
350 | if( ptr3[ix3] == NULL ) continue; |
---|
351 | else |
---|
352 | { |
---|
353 | *found_key = (ix1 << (w2+w3)) | (ix2 << w1) | ix3; |
---|
354 | return ptr3[ix3]; |
---|
355 | } |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | return NULL; |
---|
361 | } |
---|