L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bitmap
1// vi:set ft=cpp: -*- Mode: C++ -*-
2/*
3 * (c) 2008-2014 Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
5 *
6 * License: see LICENSE.spdx (in this directory or the directories above)
7 */
8
9#pragma once
10
11namespace cxx {
12
19{
20protected:
24 typedef unsigned long word_type;
25
26 enum
27 {
28 W_bits = sizeof(word_type) * 8,
29 C_bits = 8,
30 };
31
36
44 static unsigned word_index(unsigned bit) { return bit / W_bits; }
45
53 static unsigned bit_index(unsigned bit) { return bit % W_bits; }
54
58 class Bit
59 {
60 Bitmap_base *_bm;
61 long _bit;
62
63 public:
64 Bit(Bitmap_base *bm, long bit) : _bm(bm), _bit(bit) {}
65 Bit &operator = (bool val) { _bm->bit(_bit, val); return *this; }
66 operator bool () const { return _bm->bit(_bit); }
67 };
68
69public:
70 explicit Bitmap_base(void *bits) noexcept : _bits(reinterpret_cast<word_type *>(bits)) {}
71
73 static long words(long bits) noexcept { return (bits + W_bits -1) / W_bits; }
74 static long bit_buffer_bytes(long bits) noexcept
75 { return words(bits) * W_bits / 8; }
76
78 template< long BITS >
79 class Word
80 {
81 public:
82 typedef unsigned long Type;
83 enum
84 {
85 Size = (BITS + W_bits - 1) / W_bits
86 };
87 };
88
90 static long chars(long bits) noexcept
91 { return (bits + C_bits -1) / C_bits; }
92
94 template< long BITS >
95 class Char
96 {
97 public:
98 typedef unsigned char Type;
99 enum
100 {
101 Size = (BITS + C_bits - 1) / C_bits
102 };
103 };
104
111 void bit(long bit, bool on) noexcept;
112
118 void clear_bit(long bit) noexcept;
119
127 void atomic_clear_bit(long bit) noexcept;
128
136 word_type atomic_get_and_clear(long bit) noexcept;
137
143 void set_bit(long bit) noexcept;
144
152 void atomic_set_bit(long bit) noexcept;
153
161 word_type atomic_get_and_set(long bit) noexcept;
162
171 word_type bit(long bit) const noexcept;
172
181 word_type operator [] (long bit) const noexcept
182 { return this->bit(bit); }
183
191 Bit operator [] (long bit) noexcept
192 { return Bit(this, bit); }
193
205 long scan_zero(long max_bit, long start_bit = 0) const noexcept;
206
207 void *bit_buffer() const noexcept { return _bits; }
208
209protected:
210 static int _bzl(unsigned long w) noexcept;
211};
212
213
219template<int BITS>
220class Bitmap : public Bitmap_base
221{
222private:
224
225public:
227 Bitmap() noexcept : Bitmap_base(_bits) {}
228 Bitmap(Bitmap<BITS> const &o) noexcept : Bitmap_base(_bits)
229 { __builtin_memcpy(_bits, o._bits, sizeof(_bits)); }
242 long scan_zero(long start_bit = 0) const noexcept;
243
244 void clear_all()
245 { __builtin_memset(_bits, 0, sizeof(_bits)); }
246};
247
248
249inline
250void
251Bitmap_base::bit(long bit, bool on) noexcept
252{
253 long idx = word_index(bit);
254 long b = bit_index(bit);
255 _bits[idx] = (_bits[idx] & ~(1UL << b)) | (static_cast<unsigned long>(on) << b);
256}
257
258inline
259void
260Bitmap_base::clear_bit(long bit) noexcept
261{
262 long idx = word_index(bit);
263 long b = bit_index(bit);
264 _bits[idx] &= ~(1UL << b);
265}
266
267inline
268void
270{
271 long idx = word_index(bit);
272 long b = bit_index(bit);
273 word_type mask = 1UL << b;
274 __atomic_and_fetch(&_bits[idx], ~mask, __ATOMIC_RELAXED);
275}
276
277inline
280{
281 long idx = word_index(bit);
282 long b = bit_index(bit);
283 word_type mask = 1UL << b;
284 return __atomic_fetch_and(&_bits[idx], ~mask, __ATOMIC_RELAXED) & mask;
285}
286
287inline
288void
289Bitmap_base::set_bit(long bit) noexcept
290{
291 long idx = word_index(bit);
292 long b = bit_index(bit);
293 _bits[idx] |= (1UL << b);
294}
295
296inline
297void
299{
300 long idx = word_index(bit);
301 long b = bit_index(bit);
302 word_type mask = 1UL << b;
303 __atomic_or_fetch(&_bits[idx], mask, __ATOMIC_RELAXED);
304}
305
306inline
309{
310 long idx = word_index(bit);
311 long b = bit_index(bit);
312 word_type mask = 1UL << b;
313 return __atomic_fetch_or(&_bits[idx], mask, __ATOMIC_RELAXED) & mask;
314}
315
316inline
318Bitmap_base::bit(long bit) const noexcept
319{
320 long idx = word_index(bit);
321 long b = bit_index(bit);
322 return _bits[idx] & (1UL << b);
323}
324
325inline
326int
327Bitmap_base::_bzl(unsigned long w) noexcept
328{
329 for (int i = 0; i < W_bits; ++i, w >>= 1)
330 {
331 if ((w & 1) == 0)
332 return i;
333 }
334 return -1;
335}
336
337inline
338long
339Bitmap_base::scan_zero(long max_bit, long start_bit) const noexcept
340{
341 if (!(operator [] (start_bit)))
342 return start_bit;
343
344 long idx = word_index(start_bit);
345
346 max_bit -= start_bit & ~(W_bits - 1);
347
348 for (; max_bit > 0; max_bit -= W_bits, ++idx)
349 {
350 if (_bits[idx] == 0)
351 return idx * W_bits;
352
353 if (_bits[idx] != ~0UL)
354 {
355 long zbit = _bzl(_bits[idx]);
356 return zbit < max_bit ? idx * W_bits + zbit : -1;
357 }
358 }
359
360 return -1;
361}
362
363template<int BITS> inline
364long
365Bitmap<BITS>::scan_zero(long start_bit) const noexcept
366{
367 return Bitmap_base::scan_zero(BITS, start_bit);
368}
369
370};
A writable bit in a bitmap.
Definition bitmap:59
Helper abstraction for a byte contained in the bitmap.
Definition bitmap:96
Helper abstraction for a word contained in the bitmap.
Definition bitmap:80
Basic bitmap abstraction.
Definition bitmap:19
static long chars(long bits) noexcept
Get the number of chars that are used for the bitmap.
Definition bitmap:90
long scan_zero(long max_bit, long start_bit=0) const noexcept
Scan for the first zero bit.
Definition bitmap:339
void atomic_clear_bit(long bit) noexcept
Clear bit bit atomically.
Definition bitmap:269
word_type atomic_get_and_clear(long bit) noexcept
Clear bit bit atomically and return old state.
Definition bitmap:279
void atomic_set_bit(long bit) noexcept
Set bit bit atomically.
Definition bitmap:298
static unsigned bit_index(unsigned bit)
Get the bit index within word_type for the given bit.
Definition bitmap:53
@ W_bits
number of bits in word_type
Definition bitmap:28
@ C_bits
number of bits in char
Definition bitmap:29
unsigned long word_type
Data type for each element of the bit buffer.
Definition bitmap:24
static long words(long bits) noexcept
Get the number of Words that are used for the bitmap.
Definition bitmap:73
static unsigned word_index(unsigned bit)
Get the word index for the given bit.
Definition bitmap:44
void clear_bit(long bit) noexcept
Clear bit bit.
Definition bitmap:260
void set_bit(long bit) noexcept
Set bit bit.
Definition bitmap:289
word_type atomic_get_and_set(long bit) noexcept
Set bit bit atomically and return old state.
Definition bitmap:308
word_type operator[](long bit) const noexcept
Get the bit at index bit.
Definition bitmap:181
void bit(long bit, bool on) noexcept
Set the value of bit bit to on.
Definition bitmap:251
word_type * _bits
Pointer to the buffer storing the bits.
Definition bitmap:35
A static bitmap.
Definition bitmap:221
Bitmap() noexcept
Create a bitmap with BITS bits.
Definition bitmap:227
long scan_zero(long start_bit=0) const noexcept
Scan for the first zero bit.
Definition bitmap:365
Our C++ library.
Definition arith:11