sysbus: Add new platform bus helper device
[qemu.git] / include / qemu / bitmap.h
blobf0273c965f263278ed4e83877b076099ffc1aa32
1 /*
2 * Bitmap Module
4 * Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
6 * Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
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.
12 #ifndef BITMAP_H
13 #define BITMAP_H
15 #include <glib.h>
16 #include <string.h>
17 #include <stdlib.h>
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
23 * The available bitmap operations and their rough meaning in the
24 * case that the bitmap is a single unsigned long are thus:
26 * Note that nbits should be always a compile time evaluable constant.
27 * Otherwise many inlines will generate horrible code.
29 * bitmap_zero(dst, nbits) *dst = 0UL
30 * bitmap_fill(dst, nbits) *dst = ~0UL
31 * bitmap_copy(dst, src, nbits) *dst = *src
32 * bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
33 * bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
34 * bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
35 * bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
36 * bitmap_complement(dst, src, nbits) *dst = ~(*src)
37 * bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
38 * bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
39 * bitmap_empty(src, nbits) Are all bits zero in *src?
40 * bitmap_full(src, nbits) Are all bits set in *src?
41 * bitmap_set(dst, pos, nbits) Set specified bit area
42 * bitmap_clear(dst, pos, nbits) Clear specified bit area
43 * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
47 * Also the following operations apply to bitmaps.
49 * set_bit(bit, addr) *addr |= bit
50 * clear_bit(bit, addr) *addr &= ~bit
51 * change_bit(bit, addr) *addr ^= bit
52 * test_bit(bit, addr) Is bit set in *addr?
53 * test_and_set_bit(bit, addr) Set bit and return old value
54 * test_and_clear_bit(bit, addr) Clear bit and return old value
55 * test_and_change_bit(bit, addr) Change bit and return old value
56 * find_first_zero_bit(addr, nbits) Position first zero bit in *addr
57 * find_first_bit(addr, nbits) Position first set bit in *addr
58 * find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
59 * find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
62 #define BITMAP_LAST_WORD_MASK(nbits) \
63 ( \
64 ((nbits) % BITS_PER_LONG) ? \
65 (1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
68 #define DECLARE_BITMAP(name,bits) \
69 unsigned long name[BITS_TO_LONGS(bits)]
71 #define small_nbits(nbits) \
72 ((nbits) <= BITS_PER_LONG)
74 int slow_bitmap_empty(const unsigned long *bitmap, long bits);
75 int slow_bitmap_full(const unsigned long *bitmap, long bits);
76 int slow_bitmap_equal(const unsigned long *bitmap1,
77 const unsigned long *bitmap2, long bits);
78 void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
79 long bits);
80 void slow_bitmap_shift_right(unsigned long *dst,
81 const unsigned long *src, int shift, long bits);
82 void slow_bitmap_shift_left(unsigned long *dst,
83 const unsigned long *src, int shift, long bits);
84 int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
85 const unsigned long *bitmap2, long bits);
86 void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
87 const unsigned long *bitmap2, long bits);
88 void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
89 const unsigned long *bitmap2, long bits);
90 int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
91 const unsigned long *bitmap2, long bits);
92 int slow_bitmap_intersects(const unsigned long *bitmap1,
93 const unsigned long *bitmap2, long bits);
95 static inline unsigned long *bitmap_try_new(long nbits)
97 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
98 return g_try_malloc0(len);
101 static inline unsigned long *bitmap_new(long nbits)
103 unsigned long *ptr = bitmap_try_new(nbits);
104 if (ptr == NULL) {
105 abort();
107 return ptr;
110 static inline void bitmap_zero(unsigned long *dst, long nbits)
112 if (small_nbits(nbits)) {
113 *dst = 0UL;
114 } else {
115 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
116 memset(dst, 0, len);
120 static inline void bitmap_fill(unsigned long *dst, long nbits)
122 size_t nlongs = BITS_TO_LONGS(nbits);
123 if (!small_nbits(nbits)) {
124 long len = (nlongs - 1) * sizeof(unsigned long);
125 memset(dst, 0xff, len);
127 dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
130 static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
131 long nbits)
133 if (small_nbits(nbits)) {
134 *dst = *src;
135 } else {
136 long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
137 memcpy(dst, src, len);
141 static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
142 const unsigned long *src2, long nbits)
144 if (small_nbits(nbits)) {
145 return (*dst = *src1 & *src2) != 0;
147 return slow_bitmap_and(dst, src1, src2, nbits);
150 static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
151 const unsigned long *src2, long nbits)
153 if (small_nbits(nbits)) {
154 *dst = *src1 | *src2;
155 } else {
156 slow_bitmap_or(dst, src1, src2, nbits);
160 static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
161 const unsigned long *src2, long nbits)
163 if (small_nbits(nbits)) {
164 *dst = *src1 ^ *src2;
165 } else {
166 slow_bitmap_xor(dst, src1, src2, nbits);
170 static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
171 const unsigned long *src2, long nbits)
173 if (small_nbits(nbits)) {
174 return (*dst = *src1 & ~(*src2)) != 0;
176 return slow_bitmap_andnot(dst, src1, src2, nbits);
179 static inline void bitmap_complement(unsigned long *dst,
180 const unsigned long *src,
181 long nbits)
183 if (small_nbits(nbits)) {
184 *dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
185 } else {
186 slow_bitmap_complement(dst, src, nbits);
190 static inline int bitmap_equal(const unsigned long *src1,
191 const unsigned long *src2, long nbits)
193 if (small_nbits(nbits)) {
194 return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
195 } else {
196 return slow_bitmap_equal(src1, src2, nbits);
200 static inline int bitmap_empty(const unsigned long *src, long nbits)
202 if (small_nbits(nbits)) {
203 return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
204 } else {
205 return slow_bitmap_empty(src, nbits);
209 static inline int bitmap_full(const unsigned long *src, long nbits)
211 if (small_nbits(nbits)) {
212 return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
213 } else {
214 return slow_bitmap_full(src, nbits);
218 static inline int bitmap_intersects(const unsigned long *src1,
219 const unsigned long *src2, long nbits)
221 if (small_nbits(nbits)) {
222 return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
223 } else {
224 return slow_bitmap_intersects(src1, src2, nbits);
228 void bitmap_set(unsigned long *map, long i, long len);
229 void bitmap_clear(unsigned long *map, long start, long nr);
230 unsigned long bitmap_find_next_zero_area(unsigned long *map,
231 unsigned long size,
232 unsigned long start,
233 unsigned long nr,
234 unsigned long align_mask);
236 static inline unsigned long *bitmap_zero_extend(unsigned long *old,
237 long old_nbits, long new_nbits)
239 long new_len = BITS_TO_LONGS(new_nbits) * sizeof(unsigned long);
240 unsigned long *new = g_realloc(old, new_len);
241 bitmap_clear(new, old_nbits, new_nbits - old_nbits);
242 return new;
245 #endif /* BITMAP_H */