1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * Description: OS Abstract Layer. 15 */ 16 17 /** 18 * @defgroup osal_bitmap osal_bitmap 19 */ 20 #ifndef __OSAL_BITMAP_H__ 21 #define __OSAL_BITMAP_H__ 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 29 #define OSAL_BITS_PER_BYTE 8 30 #define OSAL_BITS_PER_LONG (OSAL_BITS_PER_BYTE * sizeof(unsigned long)) 31 #define OSAL_BIT_MASK(nr) (1UL << ((nr) % OSAL_BITS_PER_LONG)) 32 #define OSAL_BIT_WORD(nr) ((nr) / OSAL_BITS_PER_LONG) 33 #define OSAL_BITS_TO_LONGS(nr) (((nr) + (OSAL_BITS_PER_LONG)-1) / (OSAL_BITS_PER_LONG)) 34 #define OSAL_DECLARE_BITMAP(name, bits) unsigned long name[OSAL_BITS_TO_LONGS(bits)] 35 #define osal_for_each_set_bit(bit, addr, size) \ 36 for ((bit) = osal_bitmap_find_first_bit((addr), (size)); (bit) < (size); \ 37 (bit) = osal_bitmap_find_next_bit((addr), (size), (bit) + 1)) 38 39 #define osal_for_each_clear_bit(bit, addr, size) \ 40 for ((bit) = osal_bitmap_find_first_zero_bit((addr), (size)); (bit) < (size); \ 41 (bit) = osal_bitmap_find_next_zero_bit((addr), (size), (bit) + 1)) 42 43 /** 44 * @ingroup osal_bitmap 45 * @brief Used to set bit for bitmap. 46 * 47 * @par Description: 48 * Used to set bit for bitmap. 49 * 50 * @param nr [in] bit number in bit array. 51 * @param addr [in] address of the bit array to be set. 52 * 53 * @par Support System: 54 * linux. 55 */ 56 void osal_bitmap_set_bit(int nr, unsigned long *addr); 57 58 /** 59 * @ingroup osal_bitmap 60 * @brief Used to clear bit for bitmap. 61 * 62 * @par Description: 63 * Used to clear bit for bitmap. 64 * 65 * @param nr [in] bit number in bit array. 66 * @param addr [in] address of the bit array to be clear. 67 * 68 * @par Support System: 69 * linux. 70 */ 71 void osal_bitmap_clear_bit(int nr, unsigned long *addr); 72 73 /** 74 * @ingroup osal_bitmap 75 * @brief Used to change bit for bitmap. 76 * 77 * @par Description: 78 * Used to change bit for bitmap. 79 * 80 * @param nr [in] bit number in bit array. 81 * @param addr [in] address of the bit array to be change. 82 * 83 * @par Support System: 84 * linux. 85 */ 86 void osal_bitmap_change_bit(int nr, unsigned long *addr); 87 88 /** 89 * @ingroup osal_bitmap 90 * @brief Tests whether a given bit in an array of bits is set. 91 * 92 * @par Description: 93 * Tests whether a given bit in an array of bits is set. 94 * 95 * @param nr [in] bit number in bit array. 96 * @param addr [in] address of the bit array to be change. 97 * 98 * @return true/false 99 * 100 * @par Support System: 101 * linux. 102 */ 103 int osal_bitmap_test_bit(int nr, unsigned long *addr); 104 105 /** 106 * @ingroup osal_bitmap 107 * @brief Set bit and return old value. 108 * 109 * @par Description: 110 * Set bit and return old value. 111 * 112 * @param nr [in] bit number in bit array. 113 * @param addr [in] address of the bit array to be change. 114 * 115 * @return return old value. 116 * 117 * @par Support System: 118 * linux. 119 */ 120 int osal_bitmap_test_and_set_bit(int nr, unsigned long *addr); 121 122 /** 123 * @ingroup osal_bitmap 124 * @brief Clear bit and return old value. 125 * 126 * @par Description: 127 * Clear bit and return old value. 128 * 129 * @param nr [in] bit number in bit array. 130 * @param addr [in] address of the bit array to be change. 131 * 132 * @return return old value. 133 * 134 * @par Support System: 135 * linux. 136 */ 137 int osal_bitmap_test_and_clear_bit(int nr, unsigned long *addr); 138 139 /** 140 * @ingroup osal_bitmap 141 * @brief Change bit and return old value. 142 * 143 * @par Description: 144 * Change bit and return old value. 145 * 146 * @param nr [in] bit number in bit array. 147 * @param addr [in] address of the bit array to be change. 148 * 149 * @return return old value. 150 * 151 * @par Support System: 152 * linux. 153 */ 154 int osal_bitmap_test_and_change_bit(int nr, unsigned long *addr); 155 156 /** 157 * @ingroup osal_bitmap 158 * @brief find the first cleared bit in a memory region. 159 * 160 * @par Description: 161 * find the first cleared bit in a memory region. 162 * 163 * @param name [in] The name to base the search on. 164 * @param size [in] The bitmap size in bits. 165 * 166 * @return The position of first zero bit in *addr 167 * 168 * @par Support System: 169 * linux. 170 */ 171 int osal_bitmap_find_first_zero_bit(const unsigned long *name, unsigned size); 172 173 /** 174 * @ingroup osal_bitmap 175 * @brief find the first set bit in a memory region. 176 * 177 * @par Description: 178 * find the first set bit in a memory region. 179 * 180 * @param name [in] The name to base the search on. 181 * @param size [in] The bitmap size in bits. 182 * 183 * @return The position of first set bit in *addr 184 * 185 * @par Support System: 186 * linux. 187 */ 188 int osal_bitmap_find_first_bit(const unsigned long *name, unsigned size); 189 190 /** 191 * @ingroup osal_bitmap 192 * @brief find the next cleared bit in a memory region. 193 * 194 * @par Description: 195 * find the next cleared bit in a memory region. 196 * 197 * @param name [in] The name to base the search on. 198 * @param size [in] The bitmap size in bits. 199 * @param offset [in] The bitnumber to start searching at. 200 * 201 * @return The position of next zero bit in *addr >= bit 202 * 203 * @par Support System: 204 * linux. 205 */ 206 int osal_bitmap_find_next_zero_bit(const unsigned long *name, int size, int offset); 207 208 /** 209 * @ingroup osal_bitmap 210 * @brief find the next set bit in a memory region. 211 * 212 * @par Description: 213 * find the next set bit in a memory region. 214 * 215 * @param name [in] The name to base the search on. 216 * @param size [in] The bitmap size in bits. 217 * @param offset [in] The bitnumber to start searching at. 218 * 219 * @return The position of next set bit in *addr >= bit 220 * 221 * @par Support System: 222 * linux. 223 */ 224 int osal_bitmap_find_next_bit(const unsigned long *name, unsigned size, int offset); 225 226 #ifdef __cplusplus 227 #if __cplusplus 228 } 229 #endif 230 #endif 231 #endif /* __OSAL_BITMAP_H__ */