source: trunk/kernel/libk/bits.h @ 657

Last change on this file since 657 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: 13.0 KB
RevLine 
[1]1/*
[657]2 * bits.h - bitmap API definition
[1]3 *
4 * Author   Ghassan Almaless (2008,2009,2010,2011,2012)
[657]5 *          Alain Greiner    (2016,2017,2018,2019,2020)
[1]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#ifndef _BITS_H_
26#define _BITS_H_
27
[14]28#include <kernel_config.h>
[457]29#include <hal_kernel_types.h>
[657]30#include <hal_remote.h>
[1]31
[657]32/**********************************************************************************************
33 * This file defines the API to access a generic bitmap, that can be local or remote.
34 * It is implemented as an array of uint32_t words.
35 * The number of entries in this array is statically defined at compile time
36 * and defines the max number of items that can be registered in the bitmap.
37 * The remote accesses are used in the VFS by the inum allocator.
[1]38 *********************************************************************************************/
39
[657]40typedef uint32_t    bitmap_t;
[1]41
[657]42/**********************************************************************************************
43 * This macro returns the number of 32 bits words required to register <size> entries.
44 *********************************************************************************************/
[1]45
[657]46#define BITMAP_SIZE(size) ( ((size) & 31) ? (((size)>>5) + 1) : ((size)>>5) )
47
[1]48/**********************************************************************************************
49 * This macro returns the smallest power of 2 value, that is larger or equal to data value.
50 * It returns 0xFFFFFFFF if data is larger than 0x80000000.
51 *********************************************************************************************/
[657]52
[1]53#define POW2_ROUNDUP(data) ( (data <= 0x00000001) ? 0x00000001  : \
54                             (data <= 0x00000002) ? 0x00000002  : \
55                             (data <= 0x00000004) ? 0x00000004  : \
56                             (data <= 0x00000008) ? 0x00000008  : \
57                             (data <= 0x00000010) ? 0x00000010  : \
58                             (data <= 0x00000020) ? 0x00000020  : \
59                             (data <= 0x00000040) ? 0x00000040  : \
60                             (data <= 0x00000080) ? 0x00000080  : \
61                             (data <= 0x00000100) ? 0x00000100  : \
62                             (data <= 0x00000200) ? 0x00000200  : \
63                             (data <= 0x00000400) ? 0x00000400  : \
64                             (data <= 0x00000800) ? 0x00000800  : \
65                             (data <= 0x00001000) ? 0x00001000  : \
66                             (data <= 0x00002000) ? 0x00002000  : \
67                             (data <= 0x00004000) ? 0x00004000  : \
68                             (data <= 0x00008000) ? 0x00008000  : \
69                             (data <= 0x00010000) ? 0x00010000  : \
70                             (data <= 0x00020000) ? 0x00020000  : \
71                             (data <= 0x00040000) ? 0x00040000  : \
72                             (data <= 0x00080000) ? 0x00080000  : \
73                             (data <= 0x00100000) ? 0x00100000  : \
74                             (data <= 0x00200000) ? 0x00200000  : \
75                             (data <= 0x00400000) ? 0x00400000  : \
76                             (data <= 0x00800000) ? 0x00800000  : \
77                             (data <= 0x01000000) ? 0x01000000  : \
78                             (data <= 0x02000000) ? 0x02000000  : \
79                             (data <= 0x04000000) ? 0x04000000  : \
80                             (data <= 0x08000000) ? 0x08000000  : \
81                             (data <= 0x10000000) ? 0x10000000  : \
82                             (data <= 0x20000000) ? 0x20000000  : \
83                             (data <= 0x40000000) ? 0x40000000  : \
84                             (data <= 0x80000000) ? 0x80000000  : 0xFFFFFFFF )
85
[657]86/*********************************************************************************************
87 * These macros are NOT used by the bitmap, but are useful in other contexts... [AG]
[1]88 *********************************************************************************************/
89
[657]90#define ARROUND_UP(val, size) (((val) & ((size) -1)) ? ((val) & ~((size)-1)) + (size) : (val))
91#define ARROUND_DOWN(val, size)  ((val) & ~((size) - 1))
[1]92
[657]93#define ABS(x) (((x) < 0) ? -(x) : (x))
94#define MIN(x,y) (((x) < (y)) ? (x) : (y))
95#define MAX(x,y) (((x) < (y)) ? (y) : (x))
[1]96
[23]97/*********************************************************************************************
[657]98 * This function reset all bits in a local or remote bitmap.
[23]99 *********************************************************************************************
100 * @ bitmap  : pointer on first word in the bitmap.
[657]101 * @ size    : number of bits in bitmap.
[23]102 ********************************************************************************************/
[657]103extern void bitmap_init( bitmap_t * bitmap,
104                         uint32_t   size );
[23]105
[657]106extern void bitmap_remote_init( xptr_t   bitmap_xp,
107                                uint32_t size );
108
[23]109/*********************************************************************************************
[657]110 * These functions set a specific bit in a local or remote bitmap.
[23]111 *********************************************************************************************
[1]112 * @ bitmap  : pointer on the bitmap
113 * @ index   : bit index in the bitmap
[23]114 ********************************************************************************************/
[1]115extern inline void bitmap_set( bitmap_t * bitmap,
116                               uint32_t   index );
117
[657]118extern inline void bitmap_remote_set( xptr_t    bitmap_xp,
119                                      uint32_t  index );
120
[23]121/*********************************************************************************************
[657]122 * These functions clear a specific bit in a local or remote bitmap.
[23]123 *********************************************************************************************
[1]124 * @ bitmap  : pointer on the bitmap
125 * @ index   : bit index in the bitmap
[23]126 ********************************************************************************************/
[1]127extern inline void bitmap_clear( bitmap_t * bitmap, 
128                                 uint32_t   index );
129
[657]130extern inline void bitmap_remote_clear( xptr_t     bitmap_xp, 
131                                        uint32_t   index );
132
[23]133/*********************************************************************************************
[657]134 * These functions search the first bit non-set in a local or remote bitmap, in the
135 * range [0 , size-1], set this bit, and return the index of the found bit.
136 * The lock protecting the bitmap must be taken by the caller.
[23]137 *********************************************************************************************
[657]138 * @ bitmap  : pointer on the bitmap.
139 * @ size    : number of bits to scan.
140 * @ returns index of found bit / returns 0xFFFFFFFF if not found.
141 ********************************************************************************************/
142extern uint32_t bitmap_alloc( bitmap_t * bitmap, 
143                              uint32_t   size );
144
145extern uint32_t bitmap_remote_alloc( xptr_t    bitmap_xp,
146                                     uint32_t  size );
147
148/*********************************************************************************************
149 * This function returns the index of aa specific bit in a bitmap.
150 *********************************************************************************************
[1]151 * @ bitmap  : pointer on the bitmap
152 * @ index   : bit index in the bitmap
[23]153 * @ returns true if bitmap[index] is set
154 ********************************************************************************************/
[1]155extern inline bool_t bitmap_state( bitmap_t * bitmap, 
156                                   uint32_t   index );
157
[23]158/*********************************************************************************************
[1]159 * This function set a range of bits in a bitmap : [index ... (index + len)[
[23]160 *********************************************************************************************
[1]161 * @ bitmap  : pointer on the bitmap
162 * @ index   : first bit index in the bitmap
163 * @ len     : number of bits to set
[23]164 ********************************************************************************************/
[1]165extern void bitmap_set_range( bitmap_t * bitmap, 
166                              uint32_t   index, 
167                              uint32_t   len );
168
[23]169/*********************************************************************************************
[1]170 * This function reset a range of bits in a bitmap : [index ... (index + len)[
[23]171 *********************************************************************************************
[1]172 * @ bitmap  : pointer on the bitmap
173 * @ index   : first bit index in the bitmap
174 * @ len     : number of bits to clear
[23]175 ********************************************************************************************/
[1]176extern void bitmap_clear_range( bitmap_t * bitmap, 
177                                uint32_t   index, 
178                                uint32_t   len );
179
[23]180/*********************************************************************************************
[1]181 * This function returns the index of first bit set in a bitmap, starting from index.
[23]182 *********************************************************************************************
[1]183 * @ bitmap  : pointer on the bitmap
184 * @ index   : first bit to analyse in the bitmap
185 * @ size    : number of bits to analyse in bitmap
186 * @ returns index if found / returns 0xFFFFFFFF if bit not found
[23]187 ********************************************************************************************/
[1]188extern uint32_t bitmap_ffs2( bitmap_t * bitmap,
189                             uint32_t   index, 
190                             uint32_t   size );
191
[23]192/*********************************************************************************************
[1]193 * This function returns the index of first bit cleared in a bitmap, starting from index.
[23]194 *********************************************************************************************
[1]195 * @ bitmap  : pointer on the bitmap
196 * @ index   : first bit to analyse in the bitmap
197 * @ size    : number of bits to analyse in bitmap
198 * @ returns index if found / returns 0xFFFFFFFF if bit not found
[23]199 ********************************************************************************************/
[1]200extern uint32_t bitmap_ffc2( bitmap_t * bitmap,
201                             uint32_t   index, 
202                             uint32_t   size );
203
[23]204/*********************************************************************************************
[1]205 * This function returns the index of first bit set in a bitmap, starting from bit 0.
[23]206 *********************************************************************************************
[1]207 * @ bitmap  : pointer on the bitmap
208 * @ size    : number of bits to analyse in bitmap
209 * @ returns index if found / returns 0xFFFFFFFF if bit not found
[23]210 ********************************************************************************************/
[1]211extern uint32_t bitmap_ffs( bitmap_t * bitmap,
212                            uint32_t   size );
213
[23]214/*********************************************************************************************
[657]215 * These functions return the index of first bit cleared in a local or remote bitmap,
216 * starting from bit 0.
[23]217 *********************************************************************************************
[1]218 * @ bitmap  : pointer on the bitmap
[454]219 * @ size    : number of bits to analyse in bitmap
[1]220 * @ returns index if found / returns 0xFFFFFFFF if bit not found
[23]221 ********************************************************************************************/
[1]222extern uint32_t bitmap_ffc( bitmap_t * bitmap, 
223                            uint32_t   size );
224
[657]225extern uint32_t bitmap_remote_ffc( xptr_t   bitmap_xp, 
226                                   uint32_t size );
227
[23]228/*********************************************************************************************
[635]229 * This function takes a positive integer <val> as input argument, and returns the smallest
230 * integer <order> such as : 1<<order >= val.
231 * In other words, <order> is the min number of bits to encode <val> values.
[23]232 *********************************************************************************************
[1]233 * @ val   : value to analyse
234 * @ returns logarithm value
[23]235 ********************************************************************************************/
[1]236static inline uint32_t bits_log2( uint32_t val )
237{
[635]238    uint32_t i;
239
240    if( val > 0 )
241    {
242        val--;
243        for( i=0 ; val > 0 ; i++ ) val = val >> 1;
244        return i;
245    }
246    return 0;
[1]247}
248
249#endif  /* _BITS_H_ */
Note: See TracBrowser for help on using the repository browser.