• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @defgroup los_bitmap Bitmap
34  * @ingroup kernel
35  */
36 
37 #ifndef _LOS_BITMAP_H
38 #define _LOS_BITMAP_H
39 
40 #include "los_typedef.h"
41 
42 #ifdef __cplusplus
43 #if __cplusplus
44 extern "C" {
45 #endif /* __cplusplus */
46 #endif /* __cplusplus */
47 
48 /* Trick to get a 1 of the right size */
49 #define _ONE(x) (1 + ((x) - (x)))
50 #define BIT(n)  (1U << (n))
51 #define BIT_GET(x, bit) ((x) & (_ONE(x) << (bit)))
52 #define BIT_SHIFT(x, bit) (((x) >> (bit)) & 1)
53 #define BITS_GET(x, high, low) ((x) & (((_ONE(x) << ((high) + 1)) - 1) & ~((_ONE(x) << (low)) - 1)))
54 #define BITS_SHIFT(x, high, low) (((x) >> (low)) & ((_ONE(x) << ((high) - (low) + 1)) - 1))
55 #define BIT_SET(x, bit) (((x) & (_ONE(x) << (bit))) ? 1 : 0)
56 #define BITMAP_BITS_PER_WORD (sizeof(UINTPTR) * 8)
57 #define BITMAP_NUM_WORDS(x) (((x) + BITMAP_BITS_PER_WORD - 1) / BITMAP_BITS_PER_WORD)
58 #define BITMAP_WORD(x) ((x) / BITMAP_BITS_PER_WORD)
59 #define BITMAP_BIT_IN_WORD(x) ((x) & (BITMAP_BITS_PER_WORD - 1))
60 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITMAP_BITS_PER_WORD))
61 #define BITMAP_LAST_WORD_MASK(nbits) \
62     (((nbits) % BITMAP_BITS_PER_WORD) ? (1UL << ((nbits) % BITMAP_BITS_PER_WORD)) - 1 : ~0UL)
63 #define BITMAP_BITS_PER_INT (sizeof(INTPTR) * 8)
64 #define BITMAP_BIT_IN_INT(x) ((x) & (BITMAP_BITS_PER_INT - 1))
65 #define BITMAP_INT(x) ((x) / BITMAP_BITS_PER_INT)
66 #define BIT_MASK(x) (((x) >= sizeof(UINTPTR) * 8) ? (0UL - 1) : ((1UL << (x)) - 1))
67 
68 /**
69  * @ingroup los_bitmap
70  * Flag that indicates the invalid bit index.
71  *
72  * The effective bit index is from 0 to 31.
73  */
74 #define LOS_INVALID_BIT_INDEX 32
75 
76 /**
77  * @ingroup los_bitmap
78  * @brief Set one bit.
79  *
80  * @par Description:
81  * This API is used to set one bit of variable according to the parameter.
82  * @attention
83  * <ul>
84  * <li>When the value of pos is greater than 31, the bit (pos & 0x1f) of bitmap will be set.</li>
85  * </ul>
86  * @param bitmap   [IN] The bitmap variable pointer.
87  * @param pos       [IN] The number bit to be set.
88  *
89  * @retval None
90  * @par Dependency:
91  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
92  * @see LOS_BitmapClr
93  */
94 VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos);
95 
96 /**
97  * @ingroup los_bitmap
98  * @brief Clear one bit.
99  *
100  * @par Description:
101  * This API is used to clear one bit of variable according to the parameter.
102  * @attention
103  * <ul>
104  * <li>When the value of pos is greater than 31, the bit (pos & 0x1f) of bitmap will be clear.</li>
105  * </ul>
106  * @param bitmap   [IN] The bitmap variable pointer.
107  * @param pos          [IN] The number bit to be cleared.
108  *
109  * @retval none.
110  * @par Dependency:
111  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
112  * @see LOS_BitmapSet.
113  */
114 VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos);
115 
116 /**
117  * @ingroup los_bitmap
118  * @brief Find the lowest one bit that is set.
119  *
120  * @par Description:
121  * This API is used to find the lowest one bit that is set and return the bit index.
122  * @attention
123  * <ul>
124  * <li>None</li>
125  * </ul>
126  * @param bitmap   [IN] The bitmap variable.
127  *
128  * @retval UINT16 The bit index of the lowest one bit that is set.
129  * @par Dependency:
130  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
131  * @see LOS_HighBitGet
132  */
133 UINT16 LOS_LowBitGet(UINT32 bitmap);
134 
135 /**
136  * @ingroup los_bitmap
137  * @brief Find the highest one bit that is set.
138  *
139  * @par Description:
140  * This API is used to find the highest one bit that is set and return the bit index.
141  * @attention
142  * <ul>
143  * <li>None</li>
144  * </ul>
145  * @param bitmap   [IN] The bitmap variable.
146  *
147  * @retval UINT16 The bit index of the highest one bit that is set.
148  * @par Dependency:
149  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
150  * @see LOS_LowBitGet
151  */
152 UINT16 LOS_HighBitGet(UINT32 bitmap);
153 
154 /**
155  * @ingroup los_bitmap
156  * @brief Find the first zero bit starting from LSB.
157  *
158  * @par Description:
159  * This API is used to find the first zero bit starting from LSB and return the bit index.
160  * @attention
161  * <ul>
162  * <li>None</li>
163  * </ul>
164  * @param *bitmap   [IN] The bitmap pointer.
165  * @param numBits   [IN] The number of bits that is used to limit the bitmap range.
166  *
167  * @retval int The bit index of the first zero bit from LSB.
168  * @par Dependency:
169  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
170  * @see LOS_BitmapFfz
171  */
172 int LOS_BitmapFfz(UINTPTR *bitmap, UINT32 numBits);
173 
174 /**
175  * @ingroup los_bitmap
176  * @brief Set the number of bit to 1 from start.
177  *
178  * @par Description:
179  * This API is used to set the number of bit to 1 from start.
180  * @attention
181  * <ul>
182  * <li>None</li>
183  * </ul>
184  * @param *bitmap   [IN] The bitmap pointer.
185  * @param start     [IN] The start bit.
186  * @param numsSet   [IN] The number of set bits
187  *
188  * @retval none.
189  * @par Dependency:
190  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
191  * @see LOS_BitmapSetNBits
192  */
193 VOID LOS_BitmapSetNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsSet);
194 
195 /**
196  * @ingroup los_bitmap
197  * @brief Clear the number of bit to zero from start.
198  *
199  * @par Description:
200  * This API is used to set the number of bit to zero from start.
201  * @attention
202  * <ul>
203  * <li>None</li>
204  * </ul>
205  * @param *bitmap   [IN] The bitmap pointer.
206  * @param start     [IN] The start bit.
207  * @param numsClear [IN] The number of clear bits
208  *
209  * @retval none.
210  * @par Dependency:
211  * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
212  * @see LOS_BitmapClrNBits
213  */
214 VOID LOS_BitmapClrNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsClear);
215 
216 #ifdef __cplusplus
217 #if __cplusplus
218 }
219 #endif /* __cplusplus */
220 #endif /* __cplusplus */
221 
222 #endif /* _LOS_BITMAP_H */
223