source: trunk/kernel/libk/grdxt.c @ 656

Last change on this file since 656 was 656, checked in by alain, 4 years ago

Fix several bugs in the FATFS and in the VFS,
related to the creation of big files requiring
more than 4 Kbytes (one cluster) on device.

File size: 16.7 KB
Line 
1/*
2 * grdxt.c - Three-levels Generic Radix-tree implementation.
3 *
4 * authors  Alain Greiner (2016,2017,2018,2019))
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_kernel_types.h>
25#include <hal_special.h>
26#include <hal_remote.h>
27#include <errno.h>
28#include <printk.h>
29#include <kmem.h>
30#include <grdxt.h>
31
32////////////////////////////////////////////////////////////////////////////////////////
33//               Local access functions
34////////////////////////////////////////////////////////////////////////////////////////
35
36/////////////////////////////////
37error_t grdxt_init( grdxt_t * rt,
38                    uint32_t  ix1_width,
39                    uint32_t  ix2_width,
40                    uint32_t  ix3_width )
41{
42    void      ** root;
43        kmem_req_t   req;
44 
45        rt->ix1_width = ix1_width;
46        rt->ix2_width = ix2_width;
47        rt->ix3_width = ix3_width;
48
49    // allocates first level array
50        req.type  = KMEM_KCM;
51        req.order = ix1_width + ( (sizeof(void*) == 4) ? 2 : 3 );
52        req.flags = AF_KERNEL | AF_ZERO;
53        root = kmem_alloc( &req );
54
55        if( root == NULL )
56    {
57        printk("\n[ERROR] in %s : cannot allocate first level array\n", __FUNCTION__);
58        return -1;
59    }
60 
61        rt->root = root;
62
63        return 0;
64
65}  // end grdxt_init()
66
67//////////////////////////////////
68void grdxt_destroy( grdxt_t * rt )
69{
70        kmem_req_t req;
71
72    uint32_t   w1 = rt->ix1_width;
73    uint32_t   w2 = rt->ix2_width;
74    uint32_t   w3 = rt->ix3_width;
75
76    void    ** ptr1 = rt->root;
77    void    ** ptr2;
78    void    ** ptr3;
79
80        uint32_t   ix1;
81        uint32_t   ix2;
82        uint32_t   ix3;
83
84assert( (rt != NULL) , "pointer on radix tree is NULL\n" );
85
86        for( ix1=0 ; ix1 < (uint32_t)(1 << w1) ; ix1++ )
87        {
88        ptr2 = ptr1[ix1];
89
90                if( ptr2 == NULL ) continue;
91
92        for( ix2=0 ; ix2 < (uint32_t)(1 << w2) ; ix2++ )
93        {
94            ptr3 = ptr2[ix2];
95
96                    if( ptr3 == NULL ) continue;
97
98            for( ix3=0 ; ix3 < (uint32_t)(1 << w3) ; ix3++ )
99            {
100                 if( ptr3[ix3] != NULL )
101                 {
102                     printk("\n[WARNING] in %s : ptr3[%d][%d][%d] non empty\n",
103                     __FUNCTION__, ix1, ix2, ix3 );
104                 }
105            }
106
107            // release level 3 array
108            req.type = KMEM_KCM;
109                    req.ptr  = ptr3;
110                    kmem_free( &req );
111        }
112
113        // release level 2 array
114        req.type = KMEM_KCM;
115                req.ptr  = ptr2;
116                kmem_free( &req );
117    }
118
119    // release level 1 array
120    req.type = KMEM_KCM;
121        req.ptr  = ptr1;
122        kmem_free( &req );
123
124}  // end grdxt_destroy()
125
126////////////////////////////////////
127error_t grdxt_insert( grdxt_t  * rt,
128                      uint32_t   key,
129                      void     * value )
130{
131        kmem_req_t      req;
132
133    uint32_t        w1 = rt->ix1_width;
134    uint32_t        w2 = rt->ix2_width;
135    uint32_t        w3 = rt->ix3_width;
136
137// Check key value
138assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
139
140    // compute indexes
141    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
142        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
143        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
144
145    // get ptr1
146    void ** ptr1 = rt->root;
147
148    if( ptr1 == NULL ) return -1;
149
150    // get ptr2
151        void ** ptr2 = ptr1[ix1];
152
153    // If required, allocate memory for the missing level 2 array
154        if( ptr2 == NULL )
155        {
156        // allocate memory for level 2 array
157        req.type  = KMEM_KCM;
158        req.order = w2 + ( (sizeof(void*) == 4) ? 2 : 3 );
159        req.flags = AF_KERNEL | AF_ZERO;
160        ptr2 = kmem_alloc( &req );
161
162        if( ptr2 == NULL) return -1;
163
164        // update level 1 array
165        ptr1[ix1] = ptr2;
166        }
167
168    // get ptr3
169        void ** ptr3 = ptr2[ix2];
170
171    // If required, allocate memory for the missing level 3 array
172        if( ptr3 == NULL )
173        {
174        // allocate memory for level 3 array
175        req.type = KMEM_KCM;
176        req.order = w3 + ( (sizeof(void*) == 4) ? 2 : 3 );
177        req.flags = AF_KERNEL | AF_ZERO;
178        ptr3 = kmem_alloc( &req );
179
180        if( ptr3 == NULL) return -1;
181
182        //  update level 3 array
183                ptr2[ix2] = ptr3;
184        }
185
186    // register the value
187        ptr3[ix3] = value;
188
189        hal_fence();
190
191        return 0;
192
193}  // end grdxt_insert()
194
195///////////////////////////////////
196void * grdxt_remove( grdxt_t  * rt,
197                     uint32_t   key )
198{
199    uint32_t        w1 = rt->ix1_width;
200    uint32_t        w2 = rt->ix2_width;
201    uint32_t        w3 = rt->ix3_width;
202
203// Check key value
204assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
205
206    // compute indexes
207    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
208        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
209        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
210
211    // get ptr1
212    void ** ptr1 = rt->root;
213
214    if( ptr1 == NULL ) return NULL;
215
216    // get ptr2
217        void ** ptr2 = ptr1[ix1];
218
219        if( ptr2 == NULL ) return NULL;
220
221    // get ptr3
222        void ** ptr3 = ptr2[ix2];
223
224        if( ptr3 == NULL ) return NULL;
225
226    // get value
227        void * value = ptr3[ix3];
228
229    // reset selected slot
230        ptr3[ix3] = NULL;
231        hal_fence();
232
233        return value;
234
235}  // end grdxt_remove()
236
237///////////////////////////////////
238void * grdxt_lookup( grdxt_t  * rt,
239                     uint32_t   key )
240{
241    uint32_t        w1 = rt->ix1_width;
242    uint32_t        w2 = rt->ix2_width;
243    uint32_t        w3 = rt->ix3_width;
244
245// Check key value
246assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
247
248    void         ** ptr1 = rt->root;
249    void         ** ptr2;
250    void         ** ptr3;
251
252    // compute indexes
253    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
254        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
255        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
256
257    // get ptr2
258        ptr2 = ptr1[ix1];
259        if( ptr2 == NULL ) return NULL;
260
261    // get ptr3
262        ptr3 = ptr2[ix2];
263        if( ptr3 == NULL ) return NULL;
264
265    // get value
266        void * value = ptr3[ix3];
267
268        return value;
269
270}  // end grdxt_lookup()
271
272//////////////////////////////////////
273void * grdxt_get_first( grdxt_t  * rt,
274                        uint32_t   start_key,
275                        uint32_t * found_key )
276{
277    uint32_t        ix1;
278    uint32_t        ix2;
279    uint32_t        ix3;
280
281    uint32_t        w1 = rt->ix1_width;
282    uint32_t        w2 = rt->ix2_width;
283    uint32_t        w3 = rt->ix3_width;
284
285// Check key value
286assert( ((start_key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", start_key );
287
288    // compute max indexes
289    uint32_t        max1 = 1 << w1;
290    uint32_t        max2 = 1 << w2;
291    uint32_t        max3 = 1 << w3;
292
293    // compute min indexes
294    uint32_t        min1 = start_key >> (w2 + w3);           
295        uint32_t        min2 = (start_key >> w3) & ((1 << w2) -1);
296        uint32_t        min3 = start_key & ((1 << w3) - 1); 
297
298    void         ** ptr1 = rt->root;
299    void         ** ptr2;
300    void         ** ptr3;
301
302    for( ix1 = min1 ; ix1 < max1 ; ix1++ )
303    {
304        ptr2 = ptr1[ix1];
305        if( ptr2 == NULL ) continue;
306
307        for( ix2 = min2 ; ix2 < max2 ; ix2++ )
308        {
309            ptr3 = ptr2[ix2];
310            if( ptr3 == NULL ) continue;
311
312            for( ix3 = min3 ; ix3 < max3 ; ix3++ )
313            {
314                if( ptr3[ix3] == NULL ) continue;
315                else                   
316                {
317                    *found_key = (ix1 << (w2+w3)) | (ix2 << w3) | ix3;
318                    return ptr3[ix3];
319                }
320            }
321        }
322    }
323
324    return NULL;
325
326}  // end grdxt_get_first()
327
328
329
330////////////////////////////////////////////////////////////////////////////////////////
331//               Remote access functions
332////////////////////////////////////////////////////////////////////////////////////////
333
334//////////////////////////////////////////////
335error_t grdxt_remote_insert( xptr_t     rt_xp,
336                             uint32_t   key,
337                             void     * value )
338{
339    kmem_req_t  req;
340
341    // get cluster and local pointer on remote rt descriptor
342        cxy_t     rt_cxy = GET_CXY( rt_xp );
343    grdxt_t * rt_ptr = GET_PTR( rt_xp );
344
345#if DEBUG_GRDXT_INSERT
346uint32_t cycle = (uint32_t)hal_get_cycles();
347if(DEBUG_GRDXT_INSERT < cycle)
348printk("\n[%s] enter / rt_xp (%x,%x) / key %x / value %x\n",
349__FUNCTION__, rt_cxy, rt_ptr, key, (intptr_t)value ); 
350#endif
351
352    // get widths
353    uint32_t        w1 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix1_width ) );
354    uint32_t        w2 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix2_width ) );
355    uint32_t        w3 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix3_width ) );
356
357#if DEBUG_GRDXT_INSERT
358if(DEBUG_GRDXT_INSERT < cycle)
359printk("\n[%s] get widths : w1 %d / w2 %d / w3 %d\n",
360__FUNCTION__, w1, w2, w3 ); 
361#endif
362
363// Check key value
364assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
365
366    // compute indexes
367    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
368        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
369        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
370
371#if DEBUG_GRDXT_INSERT
372if(DEBUG_GRDXT_INSERT < cycle)
373printk("\n[%s] compute indexes : ix1 %d / ix2 %d / ix3 %d\n",
374__FUNCTION__, ix1, ix2, ix3 ); 
375#endif
376
377    // get ptr1
378    void ** ptr1 = hal_remote_lpt( XPTR( rt_cxy , &rt_ptr->root ) );
379
380    if( ptr1 == NULL ) return -1;
381
382#if DEBUG_GRDXT_INSERT
383if(DEBUG_GRDXT_INSERT < cycle)
384printk("\n[%s] compute ptr1 = %x\n",
385__FUNCTION__, (intptr_t)ptr1 ); 
386#endif
387
388    // get ptr2
389    void ** ptr2 = hal_remote_lpt( XPTR( rt_cxy , &ptr1[ix1] ) );
390
391#if DEBUG_GRDXT_INSERT
392if(DEBUG_GRDXT_INSERT < cycle)
393printk("\n[%s] get current ptr2 = %x\n",
394__FUNCTION__, (intptr_t)ptr2 ); 
395#endif
396
397    // allocate memory for the missing level_2 array if required
398    if( ptr2 == NULL )
399    {
400        // allocate memory in remote cluster
401        req.type  = KMEM_KCM;
402        req.order = w2 + ((sizeof(void*) == 4) ? 2 : 3 );
403        req.flags = AF_ZERO | AF_KERNEL;
404        ptr2 = kmem_remote_alloc( rt_cxy , &req );
405
406        if( ptr2 == NULL ) return -1;
407       
408        // update level_1 entry
409        hal_remote_spt( XPTR( rt_cxy , &ptr1[ix1] ) , ptr2 );
410
411#if DEBUG_GRDXT_INSERT
412if(DEBUG_GRDXT_INSERT < cycle)
413printk("\n[%s] update ptr1[%d] : &ptr1[%d] = %x / ptr2 = %x\n",
414__FUNCTION__, ix1, ix1, &ptr1[ix1], ptr2 );
415#endif
416
417    }
418
419    // get ptr3
420    void ** ptr3 = hal_remote_lpt( XPTR( rt_cxy , &ptr2[ix2] ) );
421
422#if DEBUG_GRDXT_INSERT
423if(DEBUG_GRDXT_INSERT < cycle)
424printk("\n[%s] get current ptr3 = %x\n",
425__FUNCTION__, (intptr_t)ptr3 ); 
426#endif
427
428    // allocate memory for the missing level_3 array if required
429    if( ptr3 == NULL )
430    {
431        // allocate memory in remote cluster
432        req.type  = KMEM_KCM;
433        req.order = w3 + ((sizeof(void*) == 4) ? 2 : 3 );
434        req.flags = AF_ZERO | AF_KERNEL;
435        ptr3 = kmem_remote_alloc( rt_cxy , &req );
436
437        if( ptr3 == NULL ) return -1;
438
439        // update level_2 entry
440        hal_remote_spt( XPTR( rt_cxy , &ptr2[ix2] ) , ptr3 );
441
442#if DEBUG_GRDXT_INSERT
443if(DEBUG_GRDXT_INSERT < cycle)
444printk("\n[%s] update  ptr2[%d] : &ptr2[%d] %x / ptr3 %x\n",
445__FUNCTION__, ix2, ix2, &ptr2[ix2], ptr3 );
446#endif
447
448    }
449
450    // register value in level_3 array
451    hal_remote_spt( XPTR( rt_cxy , &ptr3[ix3] ) , value );
452
453#if DEBUG_GRDXT_INSERT
454if(DEBUG_GRDXT_INSERT < cycle)
455printk("\n[%s] update  ptr3[%d] : &ptr3[%d] %x / value %x\n",
456__FUNCTION__, ix3, ix3, &ptr3[ix3], value );
457#endif
458
459    hal_fence();
460
461        return 0;
462
463}  // end grdxt_remote_insert()
464
465////////////////////////////////////////////
466void * grdxt_remote_remove( xptr_t    rt_xp,
467                            uint32_t  key )
468{
469    // get cluster and local pointer on remote rt descriptor
470        cxy_t     rt_cxy = GET_CXY( rt_xp );
471    grdxt_t * rt_ptr = GET_PTR( rt_xp );
472
473    // get widths
474    uint32_t        w1 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix1_width ) );
475    uint32_t        w2 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix2_width ) );
476    uint32_t        w3 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix3_width ) );
477
478// Check key value
479assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
480
481    // compute indexes
482    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
483        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
484        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
485
486    // get ptr1
487    void ** ptr1 = hal_remote_lpt( XPTR( rt_cxy , &rt_ptr->root ) );
488
489    // get ptr2
490        void ** ptr2 = hal_remote_lpt( XPTR( rt_cxy , &ptr1[ix1] ) );
491        if( ptr2 == NULL ) return NULL;
492
493    // get ptr3
494        void ** ptr3 = hal_remote_lpt( XPTR( rt_cxy , &ptr2[ix2] ) );
495        if( ptr3 == NULL ) return NULL;
496
497    // get value
498        void * value = hal_remote_lpt( XPTR( rt_cxy , &ptr3[ix3] ) );
499
500    // reset selected slot
501        hal_remote_spt( XPTR( rt_cxy, &ptr3[ix3] ) , NULL );
502        hal_fence();
503
504        return value;
505
506}  // end grdxt_remote_remove()
507
508////////////////////////////////////////////
509xptr_t grdxt_remote_lookup( xptr_t    rt_xp,
510                            uint32_t  key )
511{
512    // get cluster and local pointer on remote rt descriptor
513    grdxt_t       * rt_ptr = GET_PTR( rt_xp );
514    cxy_t           rt_cxy = GET_CXY( rt_xp );
515
516    // get widths
517    uint32_t        w1 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix1_width ) );
518    uint32_t        w2 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix2_width ) );
519    uint32_t        w3 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix3_width ) );
520
521// Check key value
522assert( ((key >> (w1 + w2 + w3)) == 0 ), "illegal key value %x\n", key );
523
524    // compute indexes
525    uint32_t        ix1 = key >> (w2 + w3);              // index in level 1 array
526        uint32_t        ix2 = (key >> w3) & ((1 << w2) -1);  // index in level 2 array
527        uint32_t        ix3 = key & ((1 << w3) - 1);         // index in level 3 array
528
529    // get ptr1
530    void ** ptr1 = hal_remote_lpt( XPTR( rt_cxy , &rt_ptr->root ) );
531
532    // get ptr2
533        void ** ptr2 = hal_remote_lpt( XPTR( rt_cxy , &ptr1[ix1] ) );
534        if( ptr2 == NULL ) return XPTR_NULL;
535
536    // get ptr3
537        void ** ptr3 = hal_remote_lpt( XPTR( rt_cxy , &ptr2[ix2] ) );
538        if( ptr3 == NULL ) return XPTR_NULL;
539
540    // get pointer on registered item
541    void  * item_ptr = hal_remote_lpt( XPTR( rt_cxy , &ptr3[ix3] ) );
542
543    // return extended pointer on registered item
544    if ( item_ptr == NULL )  return XPTR_NULL;
545        else                     return XPTR( rt_cxy , item_ptr );
546
547}  // end grdxt_remote_lookup()
548
549/////////////////////////i/////////////////
550void grdxt_remote_display( xptr_t    rt_xp,
551                           char    * name )
552{
553        uint32_t       ix1; 
554        uint32_t       ix2;
555        uint32_t       ix3;
556
557    void        ** ptr1;
558    void        ** ptr2;
559    void        ** ptr3;
560
561// check rt_xp
562assert( (rt_xp != XPTR_NULL) , "pointer on radix tree is NULL\n" );
563
564    // get cluster and local pointer on remote rt descriptor
565    grdxt_t      * rt_ptr = GET_PTR( rt_xp );
566    cxy_t          rt_cxy = GET_CXY( rt_xp );
567
568    // get widths
569    uint32_t       w1 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix1_width ) );
570    uint32_t       w2 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix2_width ) );
571    uint32_t       w3 = hal_remote_l32( XPTR( rt_cxy , &rt_ptr->ix3_width ) );
572
573    ptr1 = hal_remote_lpt( XPTR( rt_cxy , &rt_ptr->root ) );
574
575        printk("\n***** Generic Radix Tree for <%s>\n", name );
576
577        for( ix1=0 ; ix1 < (uint32_t)(1<<w1) ; ix1++ )
578        {
579            ptr2 = hal_remote_lpt( XPTR( rt_cxy , &ptr1[ix1] ) );
580        if( ptr2 == NULL )  continue;
581   
582        for( ix2=0 ; ix2 < (uint32_t)(1<<w2) ; ix2++ )
583        {
584                ptr3 = hal_remote_lpt( XPTR( rt_cxy , &ptr2[ix2] ) );
585            if( ptr3 == NULL ) continue;
586
587            for( ix3=0 ; ix3 < (uint32_t)(1<<w3) ; ix3++ )
588            {
589                void * value = hal_remote_lpt( XPTR( rt_cxy , &ptr3[ix3] ) );
590                if( value == NULL )  continue;
591
592                uint32_t key = (ix1<<(w2+w3)) + (ix2<<w3) + ix3;
593                printk(" - key = %x / value = %x / ptr1 = %x / ptr2 = %x / ptr3 = %x\n",
594                key, (intptr_t)value, (intptr_t)ptr1, (intptr_t)ptr2, (intptr_t)ptr3 );
595            }
596        }
597        }
598
599} // end grdxt_remote_display()
600
601
Note: See TracBrowser for help on using the repository browser.