更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_bitmap.h
浏览该文件的文档.
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
44extern "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 */
94VOID 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 */
114VOID 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 */
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 */
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 *
166 * @retval int The bit index of the first zero bit from LSB.
167 * @par Dependency:
168 * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
169 * @see LOS_BitmapFfz
170 */
171int LOS_BitmapFfz(UINTPTR *bitmap, UINT32 numBits);
172
173/**
174 * @ingroup los_bitmap
175 * @brief Set the number of bit to 1 from start.
176 *
177 * @par Description:
178 * This API is used to set the number of bit to 1 from start.
179 * @attention
180 * <ul>
181 * <li>None</li>
182 * </ul>
183 * @param *bitmap [IN] The bitmap pointer.
184 * @param start [IN] The start bit.
185 * @param numsSet [IN] The number of set bits
186 *
187 * @retval none.
188 * @par Dependency:
189 * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
190 * @see LOS_BitmapSetNBits
191 */
192VOID LOS_BitmapSetNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsSet);
193
194/**
195 * @ingroup los_bitmap
196 * @brief Clear the number of bit to zero from start.
197 *
198 * @par Description:
199 * This API is used to set the number of bit to zero from start.
200 * @attention
201 * <ul>
202 * <li>None</li>
203 * </ul>
204 * @param *bitmap [IN] The bitmap pointer.
205 * @param start [IN] The start bit.
206 * @param numsClear [IN] The number of clear bits
207 *
208 * @retval none.
209 * @par Dependency:
210 * <ul><li>los_bitmap.h: the header file that contains the API declaration.</li></ul>
211 * @see LOS_BitmapClrNBits
212 */
213VOID LOS_BitmapClrNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsClear);
214
215#ifdef __cplusplus
216#if __cplusplus
217}
218#endif /* __cplusplus */
219#endif /* __cplusplus */
220
221#endif /* _LOS_BITMAP_H */
VOID LOS_BitmapSet(UINT32 *bitmap, UINT16 pos)
Set one bit.
Definition: los_bitmap.c:60
VOID LOS_BitmapClr(UINT32 *bitmap, UINT16 pos)
Clear one bit.
Definition: los_bitmap.c:69
UINT16 LOS_LowBitGet(UINT32 bitmap)
Find the lowest one bit that is set.
Definition: los_bitmap.c:97
VOID LOS_BitmapClrNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsClear)
Clear the number of bit to zero from start.
Definition: los_bitmap.c:126
UINT16 LOS_HighBitGet(UINT32 bitmap)
Find the highest one bit that is set.
Definition: los_bitmap.c:88
int LOS_BitmapFfz(UINTPTR *bitmap, UINT32 numBits)
Find the first zero bit starting from LSB.
Definition: los_bitmap.c:146
VOID LOS_BitmapSetNBits(UINTPTR *bitmap, UINT32 start, UINT32 numsSet)
Set the number of bit to 1 from start.
Definition: los_bitmap.c:106
unsigned short UINT16
Definition: los_typedef.h:56
unsigned long UINTPTR
Definition: los_typedef.h:68
unsigned int UINT32
Definition: los_typedef.h:57