2012-07-16 08:01:56 +00:00
|
|
|
/*
|
2014-01-24 12:18:03 +00:00
|
|
|
* \brief Allocator using bitmaps
|
2012-07-16 08:01:56 +00:00
|
|
|
* \author Alexander Boettcher
|
2014-01-24 12:18:03 +00:00
|
|
|
* \author Stefan Kalkowski
|
2012-07-16 08:01:56 +00:00
|
|
|
* \date 2012-06-14
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 20:44:47 +00:00
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
2012-07-16 08:01:56 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
#ifndef _INCLUDE__UTIL__BIT_ARRAY_H_
|
|
|
|
#define _INCLUDE__UTIL__BIT_ARRAY_H_
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
#include <util/string.h>
|
2014-01-24 12:18:03 +00:00
|
|
|
#include <base/exception.h>
|
2012-07-16 08:01:56 +00:00
|
|
|
#include <base/stdint.h>
|
|
|
|
|
|
|
|
namespace Genode {
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
class Bit_array_base;
|
|
|
|
template <unsigned> class Bit_array;
|
|
|
|
}
|
2012-07-16 08:01:56 +00:00
|
|
|
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
class Genode::Bit_array_base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
class Invalid_bit_count : public Exception {};
|
|
|
|
class Invalid_index_access : public Exception {};
|
|
|
|
class Invalid_clear : public Exception {};
|
|
|
|
class Invalid_set : public Exception {};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
static constexpr size_t _BITS_PER_BYTE = 8UL;
|
|
|
|
static constexpr size_t _BITS_PER_WORD = sizeof(addr_t) *
|
|
|
|
_BITS_PER_BYTE;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
unsigned _bit_cnt;
|
|
|
|
unsigned _word_cnt;
|
|
|
|
addr_t *_words;
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
addr_t _word(addr_t index) const {
|
|
|
|
return index / _BITS_PER_WORD; }
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
void _check_range(addr_t const index,
|
|
|
|
addr_t const width) const
|
|
|
|
{
|
|
|
|
if ((index >= _word_cnt * _BITS_PER_WORD) ||
|
|
|
|
width > _word_cnt * _BITS_PER_WORD ||
|
|
|
|
_word_cnt * _BITS_PER_WORD - width < index)
|
|
|
|
throw Invalid_index_access();
|
|
|
|
}
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
addr_t _mask(addr_t const index, addr_t const width,
|
|
|
|
addr_t &rest) const
|
|
|
|
{
|
|
|
|
addr_t const shift = index - _word(index) * _BITS_PER_WORD;
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
rest = width + shift > _BITS_PER_WORD ?
|
|
|
|
width + shift - _BITS_PER_WORD : 0;
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
return (width >= _BITS_PER_WORD) ? ~0UL << shift
|
|
|
|
: ((1UL << width) - 1) << shift;
|
|
|
|
}
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
void _set(addr_t index, addr_t width, bool free)
|
|
|
|
{
|
|
|
|
_check_range(index, width);
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
addr_t rest, word, mask;
|
|
|
|
do {
|
|
|
|
word = _word(index);
|
|
|
|
mask = _mask(index, width, rest);
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
if (free) {
|
|
|
|
if ((_words[word] & mask) != mask)
|
|
|
|
throw Invalid_clear();
|
|
|
|
_words[word] &= ~mask;
|
|
|
|
} else {
|
|
|
|
if (_words[word] & mask)
|
|
|
|
throw Invalid_set();
|
|
|
|
_words[word] |= mask;
|
|
|
|
}
|
2014-02-12 12:59:41 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
index = (_word(index) + 1) * _BITS_PER_WORD;
|
|
|
|
width = rest;
|
|
|
|
} while (rest);
|
|
|
|
}
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
public:
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
Bit_array_base(unsigned bits, addr_t *addr, bool clear)
|
|
|
|
: _bit_cnt(bits),
|
|
|
|
_word_cnt(_bit_cnt / _BITS_PER_WORD),
|
|
|
|
_words(addr)
|
|
|
|
{
|
|
|
|
if (!bits || bits % _BITS_PER_WORD) throw Invalid_bit_count();
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
if (clear) memset(_words, 0, sizeof(addr_t)*_word_cnt);
|
|
|
|
}
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Return true if at least one bit is set between
|
|
|
|
* index until index + width - 1
|
|
|
|
*/
|
|
|
|
bool get(addr_t index, addr_t width) const
|
|
|
|
{
|
|
|
|
_check_range(index, width);
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
bool used = false;
|
|
|
|
addr_t rest, mask;
|
|
|
|
do {
|
|
|
|
mask = _mask(index, width, rest);
|
|
|
|
used = _words[_word(index)] & mask;
|
|
|
|
index = (_word(index) + 1) * _BITS_PER_WORD;
|
|
|
|
width = rest;
|
|
|
|
} while (!used && rest);
|
2014-02-12 12:59:41 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(addr_t const index, addr_t const width) {
|
|
|
|
_set(index, width, false); }
|
|
|
|
|
|
|
|
void clear(addr_t const index, addr_t const width) {
|
|
|
|
_set(index, width, true); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <unsigned BITS>
|
|
|
|
class Genode::Bit_array : public Bit_array_base
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
static constexpr size_t _WORDS = BITS / _BITS_PER_WORD;
|
|
|
|
|
|
|
|
static_assert(BITS % _BITS_PER_WORD == 0,
|
|
|
|
"Count of bits need to be word aligned!");
|
|
|
|
|
|
|
|
addr_t _array[_WORDS];
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Bit_array() : Bit_array_base(BITS, _array, true) { }
|
|
|
|
};
|
2014-02-12 12:59:41 +00:00
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
#endif /* _INCLUDE__UTIL__BIT_ARRAY_H_ */
|