source: trunk/kernel/libk/bits.c @ 666

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

Introduce remote_buf.c/.h & socket.c/.h files.
Update dev_nic.c/.h files.

File size: 10.0 KB
Line 
1/*
2 * bits.c - bitmap API implementation
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Alain Greiner    (2016,2017,2018,2019,2020)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <hal_kernel_types.h>
26#include <bits.h>
27
28//////////////////////////////////////////////////////////////////////////////
29//////////////     local access functions     ///////////////////////////////
30//////////////////////////////////////////////////////////////////////////////
31
32////////////////////////////////////
33void bitmap_init( bitmap_t * bitmap,
34                  uint32_t   len )
35{
36    uint32_t word;
37    uint32_t nwords = BITMAP_SIZE( len );
38    for( word = 0 ; word < nwords ; word++ )
39    {
40        bitmap[word] = 0;
41    }
42}  // end bitmap_init()
43
44//////////////////////////////////////////
45inline void bitmap_set( bitmap_t * bitmap,
46                        uint32_t   index )
47{
48        uint32_t  word = index >> 5;
49        uint32_t  bit  = index & 0x1F;
50
51        bitmap[word] |= ( 1 << bit );
52}
53
54////////////////////////////////////////////
55inline void bitmap_clear( bitmap_t * bitmap, 
56                          uint32_t   index )
57{
58        uint32_t  word = index >> 5;
59        uint32_t  bit  = index & 0x1F;
60
61        bitmap[word] &= ~( 1 << bit );
62}
63
64//////////////////////////////////////////////
65inline bool_t bitmap_state( bitmap_t * bitmap, 
66                            uint32_t   index )
67{
68        uint32_t  word = index >> 5;
69        uint32_t  bit  = index & 0x1F;
70
71        return (bitmap[word] & ( 1 << bit )) != 0;
72}
73
74/////////////////////////////////////////
75uint32_t bitmap_alloc( bitmap_t * bitmap,
76                       uint32_t   size )
77{
78    uint32_t max_word;
79    uint32_t max_bit; 
80        uint32_t word;
81        uint32_t bit;
82 
83    if( size )
84    {
85        max_word = ( (size-1) >>5 ) + 1;
86
87        for( word = 0 ; word < max_word ; word++ )
88            {
89            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
90
91                    if(bitmap[word] != 0XFFFFFFFF)
92                    {
93                            for(bit = 0 ; bit < max_bit ; bit++)
94                            {
95                                    if( (bitmap[word] & (1 << bit)) == 0 )
96                    {
97                        bitmap[word] |= (1 << bit);
98                        return (word*32 + bit);
99                    }
100                            }
101            }
102                }
103        }
104
105        return -1;
106
107}  // end bitmap_alloc()
108
109//////////////////////////////////////////
110void bitmap_set_range( bitmap_t * bitmap,
111                       uint32_t   index, 
112                       uint32_t   len )
113{
114        uint32_t val;
115        uint32_t word = index / 32;
116        uint32_t bit  = index % 32;
117 
118        while((len > 0))
119        {
120                if((len + bit) >= 32)
121                {
122            if( bit == 0 ) val = 0xFFFFFFFF;
123            else           val = (uint32_t)((1 << (32 - bit)) - 1);
124
125                        bitmap[word] |= (val << bit);
126                        word++;
127                        len -= (32 - bit);
128                        bit = 0;
129                }
130                else
131                {
132                        bitmap[word] |= (((1 << len ) - 1) << bit);
133                        break;
134                }
135        }
136}  // bitmap_set_range()
137
138///////////////////////////////////////////
139void bitmap_clear_range( bitmap_t * bitmap,
140                         uint32_t   index, 
141                         uint32_t   len )
142{
143    uint32_t val;
144        uint32_t word = index / 32;
145        uint32_t bit  = index % 32;
146
147        while((len > 0))
148        {
149                if((len + bit) >= 32)
150                {
151            if( bit == 0 ) val = 0xFFFFFFFF;
152            else           val = (uint32_t)((1 << (32 - bit)) - 1);
153
154                        bitmap[word] &= ~(val << bit);
155                        word++;
156                        len -= (32 - bit);
157                        bit = 0;
158                }
159                else
160                {
161                    bitmap[word] &= ~(((1 << len ) - 1) << bit);
162                        break;
163                }
164        }
165}  // bitmap_clear_range()
166
167///////////////////////////////////////
168uint32_t bitmap_ffs2( bitmap_t * bitmap,
169                      uint32_t   index,
170                      uint32_t   size )
171{
172    uint32_t max_word;
173        uint32_t word = index / 32; 
174        uint32_t bit  = index % 32; 
175 
176    if( index < size )
177    {
178            if( bit != 0 )
179            {
180                    for( ; bit < 32; bit++)
181                    {
182                            if((bitmap[word] & (1 << bit)) ) return (word*32 + bit);
183                    }
184                    word++;
185            }
186
187        max_word = ( (size-1) >>5 ) + 1;
188
189        for( ; word < max_word ; word++ )
190            {
191                    if(bitmap[word] != 0)
192                    {
193                            for(bit = 0 ; bit < 32 ; bit++)
194                            {
195                                    if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
196                            }
197            }
198                }
199        }
200
201        return -1;
202
203}  // bitmap_ffs2()
204
205///////////////////////////////////////
206uint32_t bitmap_ffc2( bitmap_t * bitmap,
207                      uint32_t   index,
208                      uint32_t   size )
209{
210    uint32_t max_word;
211        uint32_t word = index / 32;
212        uint32_t bit  = index % 32;
213   
214    if( index < size )
215    {
216            if( bit != 0 )
217            {
218                    for( ; bit < 32; bit++)
219                    {
220                            if( (bitmap[word] & (1 << bit)) == 0) return (word*32 + bit);
221                    }
222                    word++;
223            }
224
225        max_word = ( (size-1) >>5 ) + 1;
226
227        for( ; word < max_word ; word++ )
228            {
229                    if(bitmap[word] != 0xFFFFFFFF)
230                    {
231                            for(bit = 0 ; bit < 32 ; bit++)
232                            {
233                                    if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
234                            }
235            }
236                }
237        }
238
239        return -1;
240
241}  // bitmap_ffc2()
242
243//////////////////////////////////////
244uint32_t bitmap_ffs( bitmap_t * bitmap,
245                     uint32_t   size )
246{
247    uint32_t max_word;
248    uint32_t max_bit;
249        uint32_t word;
250        uint32_t bit;
251
252    if( size )
253    {
254        max_word = ( (size-1) >>5 ) + 1;
255
256        for( word = 0 ; word < max_word ; word++ )
257            {
258            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
259
260                    if(bitmap[word] != 0)
261                    {
262                            for(bit = 0 ; bit < max_bit ; bit++)
263                            {
264                                    if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
265                            }
266            }
267                }
268        }
269
270        return -1;
271
272}  // bitmap_ffs()
273
274//////////////////////////////////////
275uint32_t bitmap_ffc( bitmap_t * bitmap, 
276                     uint32_t   size )
277{
278    uint32_t max_word;
279    uint32_t max_bit;
280        uint32_t word;
281        uint32_t bit;
282 
283    if( size )
284    {
285        max_word = ( (size-1) >>5 ) + 1;
286
287        for( word = 0 ; word < max_word ; word++ )
288            {
289            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
290
291                    if(bitmap[word] != 0XFFFFFFFF)
292                    {
293                            for(bit = 0 ; bit < max_bit ; bit++)
294                            {
295                                    if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
296                            }
297            }
298                }
299        }
300
301        return -1;
302
303}  // bitmap_ffc()
304
305
306//////////////////////////////////////////////////////////////////////////////
307//////////////     remote access functions     ///////////////////////////////
308//////////////////////////////////////////////////////////////////////////////
309
310////////////////////////////////////////////
311void bitmap_remote_init( xptr_t   bitmap_xp,
312                         uint32_t len )
313{
314    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
315    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
316
317    uint32_t word;
318    uint32_t nwords = BITMAP_SIZE( len );
319
320    for( word = 0 ; word < nwords ; word++ )
321    {
322        hal_remote_s32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , 0 );
323    }
324}
325
326////////////////////////////////////////////////////
327inline void bitmap_remote_set( xptr_t     bitmap_xp,
328                               uint32_t   index )
329{
330    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
331    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
332
333        uint32_t  word = index / 32;
334        uint32_t  bit  = index % 32;
335
336    hal_remote_atomic_or( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , (1 <<bit) );
337}
338
339//////////////////////////////////////////////////////
340inline void bitmap_remote_clear( xptr_t     bitmap_xp, 
341                                 uint32_t   index )
342{
343    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
344    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
345
346        uint32_t  word = index / 32;
347        uint32_t  bit  = index % 32;
348
349    hal_remote_atomic_and( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , ~(1 <<bit) );
350}
351
352///////////////////////////////////////////////////
353uint32_t bitmap_remote_alloc( xptr_t     bitmap_xp,
354                              uint32_t   size )
355{
356    uint32_t max_word;
357    uint32_t max_bit; 
358        uint32_t word;
359        uint32_t bit;
360    xptr_t   word_xp;
361    uint32_t value;
362 
363    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
364    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
365
366    if( size )
367    {
368        max_word = ( (size-1) >>5 ) + 1;
369
370        for( word = 0 ; word < max_word ; word++ )
371            {
372            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
373
374            word_xp = XPTR( bitmap_cxy , &bitmap_ptr[word] );
375
376            value   = hal_remote_l32( word_xp );
377
378                    if( value != 0XFFFFFFFF )
379                    {
380                            for(bit = 0 ; bit < max_bit ; bit++)
381                            {
382                                    if( (value & (1 << bit)) == 0 )
383                    {
384                        hal_remote_s32( word_xp , value | (1 << bit) );
385                        return (word*32 + bit);
386                    }
387                            }
388            }
389                }
390        }
391
392        return -1;
393
394}  // end bitmap_alloc()
395
396///////////////////////////////////////////////
397uint32_t bitmap_remote_ffc( xptr_t   bitmap_xp, 
398                                     uint32_t size )
399{
400    uint32_t max_word;
401    uint32_t max_bit;
402        uint32_t word;
403        uint32_t bit;
404    uint32_t value;
405 
406    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
407    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
408
409    if( size )
410    {
411        max_word = ( (size-1) >>5 ) + 1;
412
413        for( word = 0 ; word < max_word ; word++ )
414            {
415            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
416
417            value = hal_remote_l32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) );
418
419                    if( value != 0xFFFFFFFF )
420                    {
421                            for(bit = 0 ; bit < max_bit ; bit++)
422                            {
423                                    if( (value & (1 << bit)) == 0 ) return (word*32 + bit);
424                            }
425            }
426                }
427        }
428
429        return -1;
430
431}  // bitmap_remote_ffc()
432
433
Note: See TracBrowser for help on using the repository browser.