L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
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 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
9 *
10 * As a special exception, you may use this file as part of a free software
11 * library without restriction. Specifically, if other files instantiate
12 * templates or use macros or inline functions from this file, or you compile
13 * this file and link it with other files to produce an executable, this
14 * file does not by itself cause the resulting executable to be covered by
15 * the GNU General Public License. This exception does not however
16 * invalidate any other reasons why the executable file might be covered by
17 * the GNU General Public License.
18 */
19
20#pragma once
21
22namespace cxx {
23
31{
32protected:
36 typedef unsigned long word_type;
37
38 enum
39 {
40 W_bits = sizeof(word_type) * 8,
41 C_bits = 8,
42 };
43
48
54 static unsigned word_index(unsigned bit) { return bit / W_bits; }
55
61 static unsigned bit_index(unsigned bit) { return bit % W_bits; }
62
66 class Bit
67 {
68 Bitmap_base *_bm;
69 long _bit;
70
71 public:
72 Bit(Bitmap_base *bm, long bit) : _bm(bm), _bit(bit) {}
73 Bit &operator = (bool val) { _bm->bit(_bit, val); return *this; }
74 operator bool () const { return _bm->bit(_bit); }
75 };
76
77public:
78 explicit Bitmap_base(void *bits) noexcept : _bits((word_type *)bits) {}
79
81 static long words(long bits) noexcept { return (bits + W_bits -1) / W_bits; }
82 static long bit_buffer_bytes(long bits) noexcept
83 { return words(bits) * W_bits / 8; }
84
86 template< long BITS >
87 class Word
88 {
89 public:
90 typedef unsigned long Type;
91 enum
92 {
93 Size = (BITS + W_bits - 1) / W_bits
94 };
95 };
96
98 static long chars(long bits) throw ()
99 { return (bits + C_bits -1) / C_bits; }
100
102 template< long BITS >
103 class Char
104 {
105 public:
106 typedef unsigned char Type;
107 enum
108 {
109 Size = (BITS + C_bits - 1) / C_bits
110 };
111 };
112
118 void bit(long bit, bool on) noexcept;
119
124 void clear_bit(long bit) noexcept;
129 void set_bit(long bit) noexcept;
130
136 word_type bit(long bit) const noexcept;
137
143 word_type operator [] (long bit) const noexcept
144 { return this->bit(bit); }
145
151 Bit operator [] (long bit) noexcept
152 { return Bit(this, bit); }
153
165 long scan_zero(long max_bit, long start_bit = 0) const noexcept;
166
167 void *bit_buffer() const noexcept { return _bits; }
168
169protected:
170 static int _bzl(unsigned long w) noexcept;
171};
172
173
179template<int BITS>
180class Bitmap : public Bitmap_base
181{
182private:
184
185public:
187 Bitmap() noexcept : Bitmap_base(_bits) {}
188 Bitmap(Bitmap<BITS> const &o) noexcept : Bitmap_base(_bits)
189 { __builtin_memcpy(_bits, o._bits, sizeof(_bits)); }
202 long scan_zero(long start_bit = 0) const noexcept;
203
204 void clear_all()
205 { __builtin_memset(_bits, 0, sizeof(_bits)); }
206};
207
208
209inline
210void
211Bitmap_base::bit(long bit, bool on) noexcept
212{
213 long idx = word_index(bit);
214 long b = bit_index(bit);
215 _bits[idx] = (_bits[idx] & ~(1UL << b)) | ((unsigned long)on << b);
216}
217
218inline
219void
220Bitmap_base::clear_bit(long bit) noexcept
221{
222 long idx = word_index(bit);
223 long b = bit_index(bit);
224 _bits[idx] &= ~(1UL << b);
225}
226
227inline
228void
229Bitmap_base::set_bit(long bit) noexcept
230{
231 long idx = word_index(bit);
232 long b = bit_index(bit);
233 _bits[idx] |= (1UL << b);
234}
235
236inline
238Bitmap_base::bit(long bit) const noexcept
239{
240 long idx = word_index(bit);
241 long b = bit_index(bit);
242 return _bits[idx] & (1UL << b);
243}
244
245inline
246int
247Bitmap_base::_bzl(unsigned long w) noexcept
248{
249 for (int i = 0; i < W_bits; ++i, w >>= 1)
250 {
251 if ((w & 1) == 0)
252 return i;
253 }
254 return -1;
255}
256
257inline
258long
259Bitmap_base::scan_zero(long max_bit, long start_bit) const noexcept
260{
261 if (!(operator [] (start_bit)))
262 return start_bit;
263
264 long idx = word_index(start_bit);
265
266 max_bit -= start_bit & ~(W_bits - 1);
267
268 for (; max_bit > 0; max_bit -= W_bits, ++idx)
269 {
270 if (_bits[idx] == 0)
271 return idx * W_bits;
272
273 if (_bits[idx] != ~0UL)
274 {
275 long zbit = _bzl(_bits[idx]);
276 return zbit < max_bit ? idx * W_bits + zbit : -1;
277 }
278 }
279
280 return -1;
281}
282
283template<int BITS> inline
284long
285Bitmap<BITS>::scan_zero(long start_bit) const noexcept
286{
287 return Bitmap_base::scan_zero(BITS, start_bit);
288}
289
290};
291
A writeable bit in a bitmap.
Definition bitmap:67
Helper abstraction for a byte contained in the bitmap.
Definition bitmap:104
Helper abstraction for a word contained in the bitmap.
Definition bitmap:88
Basic bitmap abstraction.
Definition bitmap:31
long scan_zero(long max_bit, long start_bit=0) const noexcept
Scan for the first zero bit.
Definition bitmap:259
@ W_bits
number of bits in word_type
Definition bitmap:40
@ C_bits
number of bits in char
Definition bitmap:41
static unsigned bit_index(unsigned bit)
Get the bit index within word_type for the given bit.
Definition bitmap:61
unsigned long word_type
Data type for each element of the bit buffer.
Definition bitmap:36
static long words(long bits) noexcept
Get the number of Words that are used for the bitmap.
Definition bitmap:81
static unsigned word_index(unsigned bit)
Get the word index for the given bit.
Definition bitmap:54
void clear_bit(long bit) noexcept
Clear bit bit.
Definition bitmap:220
void set_bit(long bit) noexcept
Set bit bit.
Definition bitmap:229
word_type operator[](long bit) const noexcept
Get the bit at index bit.
Definition bitmap:143
static long chars(long bits)
Get the number of chars that are used for the bitmap.
Definition bitmap:98
void bit(long bit, bool on) noexcept
Set the value of bit bit to on.
Definition bitmap:211
word_type * _bits
Pointer to the buffer storing the bits.
Definition bitmap:47
A static bit map.
Definition bitmap:181
Bitmap() noexcept
Create a bitmap with BITS bits.
Definition bitmap:187
long scan_zero(long start_bit=0) const noexcept
Scan for the first zero bit.
Definition bitmap:285
Our C++ library.
Definition arith:22