1 /* 2 * Bitmap Module 3 * 4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com> 5 * 6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h 7 * 8 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 9 * See the COPYING.LIB file in the top-level directory. 10 */ 11 12 #ifndef BITMAP_H 13 #define BITMAP_H 14 15 16 #include "qemu/bitops.h" 17 18 /* 19 * The available bitmap operations and their rough meaning in the 20 * case that the bitmap is a single unsigned long are thus: 21 * 22 * Note that nbits should be always a compile time evaluable constant. 23 * Otherwise many inlines will generate horrible code. 24 * 25 * bitmap_zero(dst, nbits) *dst = 0UL 26 * bitmap_fill(dst, nbits) *dst = ~0UL 27 * bitmap_copy(dst, src, nbits) *dst = *src 28 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2 29 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2 30 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2 31 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2) 32 * bitmap_complement(dst, src, nbits) *dst = ~(*src) 33 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal? 34 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap? 35 * bitmap_empty(src, nbits) Are all bits zero in *src? 36 * bitmap_full(src, nbits) Are all bits set in *src? 37 * bitmap_set(dst, pos, nbits) Set specified bit area 38 * bitmap_set_atomic(dst, pos, nbits) Set specified bit area with atomic ops 39 * bitmap_clear(dst, pos, nbits) Clear specified bit area 40 * bitmap_test_and_clear_atomic(dst, pos, nbits) Test and clear area 41 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area 42 */ 43 44 /* 45 * Also the following operations apply to bitmaps. 46 * 47 * set_bit(bit, addr) *addr |= bit 48 * clear_bit(bit, addr) *addr &= ~bit 49 * change_bit(bit, addr) *addr ^= bit 50 * test_bit(bit, addr) Is bit set in *addr? 51 * test_and_set_bit(bit, addr) Set bit and return old value 52 * test_and_clear_bit(bit, addr) Clear bit and return old value 53 * test_and_change_bit(bit, addr) Change bit and return old value 54 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr 55 * find_first_bit(addr, nbits) Position first set bit in *addr 56 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit 57 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit 58 */ 59 60 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) 61 #define BITMAP_LAST_WORD_MASK(nbits) (~0UL >> (-(nbits) & (BITS_PER_LONG - 1))) 62 63 #define DECLARE_BITMAP(name,bits) \ 64 unsigned long name[BITS_TO_LONGS(bits)] 65 66 #define small_nbits(nbits) \ 67 ((nbits) <= BITS_PER_LONG) 68 69 int slow_bitmap_empty(const unsigned long *bitmap, long bits); 70 int slow_bitmap_full(const unsigned long *bitmap, long bits); 71 int slow_bitmap_equal(const unsigned long *bitmap1, 72 const unsigned long *bitmap2, long bits); 73 void slow_bitmap_complement(unsigned long *dst, const unsigned long *src, 74 long bits); 75 int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1, 76 const unsigned long *bitmap2, long bits); 77 void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1, 78 const unsigned long *bitmap2, long bits); 79 void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, 80 const unsigned long *bitmap2, long bits); 81 int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, 82 const unsigned long *bitmap2, long bits); 83 int slow_bitmap_intersects(const unsigned long *bitmap1, 84 const unsigned long *bitmap2, long bits); 85 86 static inline unsigned long *bitmap_try_new(long nbits) 87 { 88 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 89 return g_try_malloc0(len); 90 } 91 92 static inline unsigned long *bitmap_new(long nbits) 93 { 94 unsigned long *ptr = bitmap_try_new(nbits); 95 if (ptr == NULL) { 96 abort(); 97 } 98 return ptr; 99 } 100 101 static inline void bitmap_zero(unsigned long *dst, long nbits) 102 { 103 if (small_nbits(nbits)) { 104 *dst = 0UL; 105 } else { 106 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 107 memset(dst, 0, len); 108 } 109 } 110 111 static inline void bitmap_fill(unsigned long *dst, long nbits) 112 { 113 size_t nlongs = BITS_TO_LONGS(nbits); 114 if (!small_nbits(nbits)) { 115 long len = (nlongs - 1) * sizeof(unsigned long); 116 memset(dst, 0xff, len); 117 } 118 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits); 119 } 120 121 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src, 122 long nbits) 123 { 124 if (small_nbits(nbits)) { 125 *dst = *src; 126 } else { 127 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); 128 memcpy(dst, src, len); 129 } 130 } 131 132 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1, 133 const unsigned long *src2, long nbits) 134 { 135 if (small_nbits(nbits)) { 136 return (*dst = *src1 & *src2) != 0; 137 } 138 return slow_bitmap_and(dst, src1, src2, nbits); 139 } 140 141 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1, 142 const unsigned long *src2, long nbits) 143 { 144 if (small_nbits(nbits)) { 145 *dst = *src1 | *src2; 146 } else { 147 slow_bitmap_or(dst, src1, src2, nbits); 148 } 149 } 150 151 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1, 152 const unsigned long *src2, long nbits) 153 { 154 if (small_nbits(nbits)) { 155 *dst = *src1 ^ *src2; 156 } else { 157 slow_bitmap_xor(dst, src1, src2, nbits); 158 } 159 } 160 161 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1, 162 const unsigned long *src2, long nbits) 163 { 164 if (small_nbits(nbits)) { 165 return (*dst = *src1 & ~(*src2)) != 0; 166 } 167 return slow_bitmap_andnot(dst, src1, src2, nbits); 168 } 169 170 static inline void bitmap_complement(unsigned long *dst, 171 const unsigned long *src, 172 long nbits) 173 { 174 if (small_nbits(nbits)) { 175 *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits); 176 } else { 177 slow_bitmap_complement(dst, src, nbits); 178 } 179 } 180 181 static inline int bitmap_equal(const unsigned long *src1, 182 const unsigned long *src2, long nbits) 183 { 184 if (small_nbits(nbits)) { 185 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); 186 } else { 187 return slow_bitmap_equal(src1, src2, nbits); 188 } 189 } 190 191 static inline int bitmap_empty(const unsigned long *src, long nbits) 192 { 193 if (small_nbits(nbits)) { 194 return ! (*src & BITMAP_LAST_WORD_MASK(nbits)); 195 } else { 196 return slow_bitmap_empty(src, nbits); 197 } 198 } 199 200 static inline int bitmap_full(const unsigned long *src, long nbits) 201 { 202 if (small_nbits(nbits)) { 203 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits)); 204 } else { 205 return slow_bitmap_full(src, nbits); 206 } 207 } 208 209 static inline int bitmap_intersects(const unsigned long *src1, 210 const unsigned long *src2, long nbits) 211 { 212 if (small_nbits(nbits)) { 213 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0; 214 } else { 215 return slow_bitmap_intersects(src1, src2, nbits); 216 } 217 } 218 219 void bitmap_set(unsigned long *map, long i, long len); 220 void bitmap_set_atomic(unsigned long *map, long i, long len); 221 void bitmap_clear(unsigned long *map, long start, long nr); 222 bool bitmap_test_and_clear_atomic(unsigned long *map, long start, long nr); 223 void bitmap_copy_and_clear_atomic(unsigned long *dst, unsigned long *src, 224 long nr); 225 unsigned long bitmap_find_next_zero_area(unsigned long *map, 226 unsigned long size, 227 unsigned long start, 228 unsigned long nr, 229 unsigned long align_mask); 230 231 static inline unsigned long *bitmap_zero_extend(unsigned long *old, 232 long old_nbits, long new_nbits) 233 { 234 long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long); 235 unsigned long *new = g_realloc(old, new_len); 236 bitmap_clear(new, old_nbits, new_nbits - old_nbits); 237 return new; 238 } 239 240 #endif /* BITMAP_H */ 241