Branch data Line data Source code
1 : : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 2 : : /* Copyright 2016-2017 IBM Corp. */ 3 : : 4 : : #ifndef __BITMAP_H 5 : : #define __BITMAP_H 6 : : 7 : : #include <stdint.h> 8 : : #include <stdbool.h> 9 : : 10 : : typedef unsigned long bitmap_elem_t; 11 : : typedef bitmap_elem_t bitmap_t[]; 12 : : 13 : : #define BITMAP_ELSZ (sizeof(bitmap_elem_t) << 3) 14 : : 15 : : /* Number of elements for _n bits (rounded up) */ 16 : : #define BITMAP_ELEMS(_n) (((_n) + (BITMAP_ELSZ - 1)) / BITMAP_ELSZ) 17 : : /* Number of bytes for _n bits (rounded up) */ 18 : : #define BITMAP_BYTES(_n) (BITMAP_ELEMS(_n) * sizeof(bitmap_elem_t)) 19 : : /* Bit number within an elemnt for bit _n */ 20 : : #define BITMAP_BIT(_n) ((_n) & (BITMAP_ELSZ - 1)) 21 : : /* Corresponding mask */ 22 : : #define BITMAP_MASK(_n) (1ul << BITMAP_BIT(_n)) 23 : : /* Element number for bit _n */ 24 : : #define BITMAP_ELEM(_n) ((_n) / BITMAP_ELSZ) 25 : : 26 : 35 : static inline void bitmap_set_bit(bitmap_t map, unsigned int bit) 27 : : { 28 : 35 : map[BITMAP_ELEM(bit)] |= BITMAP_MASK(bit); 29 : 35 : } 30 : : 31 : 29 : static inline void bitmap_clr_bit(bitmap_t map, unsigned int bit) 32 : : { 33 : 29 : map[BITMAP_ELEM(bit)] &= ~BITMAP_MASK(bit); 34 : 29 : } 35 : : 36 : 2186 : static inline bool bitmap_tst_bit(bitmap_t map, unsigned int bit) 37 : : { 38 : 2186 : return map[BITMAP_ELEM(bit)] & BITMAP_MASK(bit); 39 : : } 40 : : 41 : : extern int bitmap_find_zero_bit(bitmap_t map, unsigned int start, 42 : : unsigned int count); 43 : : extern int bitmap_find_one_bit(bitmap_t map, unsigned int start, 44 : : unsigned int count); 45 : : 46 : : #define bitmap_for_each_zero(map, size, bit) \ 47 : : for (bit = bitmap_find_zero_bit(map, 0, size); \ 48 : : bit >= 0; \ 49 : : bit = bitmap_find_zero_bit(map, (bit) + 1, (size) - (bit) - 1)) 50 : : 51 : : #define bitmap_for_each_one(map, size, bit) \ 52 : : for (bit = bitmap_find_one_bit(map, 0, size); \ 53 : : bit >= 0; \ 54 : : bit = bitmap_find_one_bit(map, (bit) + 1, (size) - (bit) - 1)) 55 : : 56 : : #endif /* __BITMAP_H */