5 template< bool LARGE, unsigned BITS >
8 // Implementation for bitmaps bigger than sizeof(unsigned long) * 8
9 // Derived classes have to make sure to provide the storage space for
10 // holding the bitmap.
11 template<unsigned BITS>
12 class Bitmap_base< true, BITS >
16 Bpl = sizeof(unsigned long) * 8,
17 Nr_elems = (BITS + Bpl - 1) / Bpl,
20 void bit(unsigned long bit, bool on)
22 unsigned long idx = bit / Bpl;
23 unsigned long b = bit % Bpl;
24 _bits[idx] = (_bits[idx] & ~(1UL << b)) | ((unsigned long)on << b);
27 void clear_bit(unsigned long bit)
29 unsigned long idx = bit / Bpl;
30 unsigned long b = bit % Bpl;
31 _bits[idx] &= ~(1UL << b);
34 void set_bit(unsigned long bit)
36 unsigned long idx = bit / Bpl;
37 unsigned long b = bit % Bpl;
38 _bits[idx] |= (1UL << b);
41 unsigned long operator [] (unsigned long bit) const
43 unsigned long idx = bit / Bpl;
44 unsigned long b = bit % Bpl;
45 return _bits[idx] & (1UL << b);
48 bool atomic_get_and_clear(unsigned long bit)
50 unsigned long idx = bit / Bpl;
51 unsigned long b = bit % Bpl;
54 if (!(_bits[idx] & (1UL << b)))
61 while (!mp_cas(&_bits[idx], v, v & ~(1UL << b)));
63 return v & (1UL << b);
66 void atomic_set_bit(unsigned long bit)
68 unsigned long idx = bit / Bpl;
69 unsigned long b = bit % Bpl;
70 atomic_mp_or(&_bits[idx], 1UL << b);
73 void atomic_clear_bit(unsigned long bit)
75 unsigned long idx = bit / Bpl;
76 unsigned long b = bit % Bpl;
77 atomic_mp_and(&_bits[idx], ~(1UL << b));
82 for (unsigned i = 0; i < Nr_elems; ++i)
88 for (unsigned i = 0; i < Nr_elems; ++i)
94 void atomic_or(Bitmap_base const &r)
96 for (unsigned i = 0; i < Nr_elems; ++i)
97 atomic_mp_or(&_bits[i], r._bits[i]);
102 Bitmap_base(Bitmap_base const &) = delete;
103 Bitmap_base &operator = (Bitmap_base const &) = delete;
106 void _or(Bitmap_base const &r)
108 for (unsigned i = 0; i < Nr_elems; ++i)
109 _bits[i] |= r._bits[i];
112 void _copy(Bitmap_base const &s)
113 { __builtin_memcpy(_bits, s._bits, Nr_elems * sizeof(_bits[0])); }
115 unsigned long _bits[Nr_elems];
118 // Implementation for a bitmap up to sizeof(unsigned long) * 8 bits
119 template<unsigned BITS>
120 class Bitmap_base<false, BITS>
123 void bit(unsigned long bit, bool on)
125 _bits = (_bits & ~(1UL << bit)) | ((unsigned long)on << bit);
128 void clear_bit(unsigned long bit)
130 _bits &= ~(1UL << bit);
133 void set_bit(unsigned long bit)
138 unsigned long operator [] (unsigned long bit) const
140 return _bits & (1UL << bit);
143 bool atomic_get_and_clear(unsigned long bit)
145 if (!(_bits & (1UL << bit)))
153 while (!mp_cas(&_bits, v, v & ~(1UL << bit)));
155 return v & (1UL << bit);
158 void atomic_set_bit(unsigned long bit)
160 atomic_mp_or(&_bits, 1UL << bit);
163 void atomic_clear_bit(unsigned long bit)
165 atomic_mp_and(&_bits, ~(1UL << bit));
173 bool is_empty() const
178 void atomic_or(Bitmap_base const &r)
180 atomic_mp_or(&_bits, r._bits);
188 Bpl = sizeof(unsigned long) * 8,
194 Bitmap_base(Bitmap_base const &) = delete;
195 Bitmap_base &operator = (Bitmap_base const &) = delete;
197 void _or(Bitmap_base const &r)
202 void _copy(Bitmap_base const &s)
207 class Bitmap : public Bitmap_base< (BITS > sizeof(unsigned long) * 8), BITS >
211 Bitmap(Bitmap const &o)
214 Bitmap &operator = (Bitmap const &o)
220 Bitmap &operator |= (Bitmap const &o)