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 {
|
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
class Bit_array_base
|
2012-07-16 08:01:56 +00:00
|
|
|
{
|
2014-01-24 12:18:03 +00:00
|
|
|
public:
|
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
class Invalid_bit_count : public Exception {};
|
2014-01-24 12:18:03 +00:00
|
|
|
class Invalid_index_access : public Exception {};
|
|
|
|
class Invalid_clear : public Exception {};
|
|
|
|
class Invalid_set : public Exception {};
|
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
protected:
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
static constexpr size_t _BITS_PER_BYTE = 8UL;
|
|
|
|
static constexpr size_t _BITS_PER_WORD = sizeof(addr_t) *
|
|
|
|
_BITS_PER_BYTE;
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
private:
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
unsigned _bit_cnt;
|
|
|
|
unsigned _word_cnt;
|
|
|
|
addr_t *_words;
|
2014-01-24 12:18:03 +00:00
|
|
|
|
|
|
|
addr_t _word(addr_t index) const {
|
|
|
|
return index / _BITS_PER_WORD; }
|
2012-07-16 08:01:56 +00:00
|
|
|
|
|
|
|
void _check_range(addr_t const index,
|
|
|
|
addr_t const width) const
|
|
|
|
{
|
2014-02-12 12:59:41 +00:00
|
|
|
if ((index >= _word_cnt * _BITS_PER_WORD) ||
|
|
|
|
width > _word_cnt * _BITS_PER_WORD ||
|
|
|
|
_word_cnt * _BITS_PER_WORD - width < index)
|
2014-01-24 12:18:03 +00:00
|
|
|
throw Invalid_index_access();
|
2012-07-16 08:01:56 +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;
|
|
|
|
|
|
|
|
rest = width + shift > _BITS_PER_WORD ?
|
|
|
|
width + shift - _BITS_PER_WORD : 0;
|
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
return (width >= _BITS_PER_WORD) ? ~0UL << shift
|
|
|
|
: ((1UL << width) - 1) << shift;
|
2012-07-16 08:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _set(addr_t index, addr_t width, bool free)
|
|
|
|
{
|
|
|
|
_check_range(index, width);
|
|
|
|
|
|
|
|
addr_t rest, word, mask;
|
|
|
|
do {
|
|
|
|
word = _word(index);
|
|
|
|
mask = _mask(index, width, rest);
|
|
|
|
|
|
|
|
if (free) {
|
|
|
|
if ((_words[word] & mask) != mask)
|
2014-01-24 12:18:03 +00:00
|
|
|
throw Invalid_clear();
|
2012-07-16 08:01:56 +00:00
|
|
|
_words[word] &= ~mask;
|
|
|
|
} else {
|
|
|
|
if (_words[word] & mask)
|
2014-01-24 12:18:03 +00:00
|
|
|
throw Invalid_set();
|
2012-07-16 08:01:56 +00:00
|
|
|
_words[word] |= mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
index = (_word(index) + 1) * _BITS_PER_WORD;
|
|
|
|
width = rest;
|
|
|
|
} while (rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-06-11 14:30:30 +00:00
|
|
|
Bit_array_base(unsigned bits, addr_t *addr, bool clear)
|
2014-02-12 12:59:41 +00:00
|
|
|
: _bit_cnt(bits),
|
|
|
|
_word_cnt(_bit_cnt / _BITS_PER_WORD),
|
|
|
|
_words(addr)
|
|
|
|
{
|
2014-06-19 10:14:48 +00:00
|
|
|
if (!bits || bits % _BITS_PER_WORD) throw Invalid_bit_count();
|
2014-02-12 12:59:41 +00:00
|
|
|
|
2014-06-11 14:30:30 +00:00
|
|
|
if (clear) memset(_words, 0, sizeof(addr_t)*_word_cnt);
|
2014-02-12 12:59:41 +00:00
|
|
|
}
|
2012-07-16 08:01:56 +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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return used;
|
|
|
|
}
|
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
void set(addr_t const index, addr_t const width) {
|
|
|
|
_set(index, width, false); }
|
2012-07-16 08:01:56 +00:00
|
|
|
|
2014-01-24 12:18:03 +00:00
|
|
|
void clear(addr_t const index, addr_t const width) {
|
|
|
|
_set(index, width, true); }
|
2012-07-16 08:01:56 +00:00
|
|
|
};
|
|
|
|
|
2014-02-12 12:59:41 +00:00
|
|
|
|
|
|
|
template <unsigned BITS>
|
|
|
|
class 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:
|
|
|
|
|
2014-06-11 14:30:30 +00:00
|
|
|
Bit_array() : Bit_array_base(BITS, _array, true) { }
|
2014-02-12 12:59:41 +00:00
|
|
|
};
|
|
|
|
|
2012-07-16 08:01:56 +00:00
|
|
|
}
|
2014-01-24 12:18:03 +00:00
|
|
|
#endif /* _INCLUDE__UTIL__BIT_ARRAY_H_ */
|