FreeNOS
Public Types | Public Member Functions | Private Member Functions | Private Attributes
BitArray Class Reference

Represents an array of bits. More...

#include <BitArray.h>

Public Types

enum  Result { Success, InvalidArgument, OutOfMemory }
 Result codes. More...
 

Public Member Functions

 BitArray (const Size bitCount, u8 *array=ZERO)
 Class constructor. More...
 
virtual ~BitArray ()
 Class destructor. More...
 
Size size () const
 Returns the maximum size of this Container. More...
 
Size count (const bool on) const
 Get the number of bits in the map which have the given value. More...
 
void set (const Size bit, const bool value=true)
 Sets the given bit to the given value. More...
 
void setRange (const Size from, const Size to)
 Set a range of bits inside the map to 1. More...
 
Result setNext (Size *bit, const Size count=1, const Size offset=0, const Size boundary=1)
 Sets the next unset bit(s). More...
 
void unset (const Size bit)
 Sets the given bit to zero. More...
 
void clear ()
 Set all bits to zero. More...
 
bool isSet (const Size bit) const
 Verify if a given bit is set. More...
 
u8array () const
 Retrieve a pointer to the internal BitArray. More...
 
void setArray (u8 *array, const Size bitCount=ZERO)
 Use the given pointer as the BitArray buffer. More...
 
bool operator[] (const Size bit) const
 Retrieve the value of the given bit. More...
 
bool operator[] (const int bit) const
 Retrieve the value of the given bit. More...
 

Private Member Functions

Size calculateBitmapSize (const Size bitCount) const
 Calculate required size of bitmap array in bytes. More...
 

Private Attributes

Size m_bitCount
 Total number of bits in the array. More...
 
Size m_set
 Set bits in the array. More...
 
u8m_array
 Array containing the bits. More...
 
bool m_allocated
 True if m_array was allocated interally. More...
 

Detailed Description

Represents an array of bits.

Definition at line 36 of file BitArray.h.

Member Enumeration Documentation

◆ Result

Result codes.

Enumerator
Success 
InvalidArgument 
OutOfMemory 

Definition at line 43 of file BitArray.h.

Constructor & Destructor Documentation

◆ BitArray()

BitArray::BitArray ( const Size  bitCount,
u8 array = ZERO 
)

Class constructor.

Parameters
bitCountNumber of bits to manage.
arrayOptional pointer to pre-allocated bits array to manage.

Definition at line 22 of file BitArray.cpp.

References array(), calculateBitmapSize(), clear(), m_allocated, m_array, m_bitCount, m_set, and ZERO.

◆ ~BitArray()

BitArray::~BitArray ( )
virtual

Class destructor.

Definition at line 32 of file BitArray.cpp.

References m_allocated, and m_array.

Member Function Documentation

◆ array()

u8 * BitArray::array ( ) const

Retrieve a pointer to the internal BitArray.

Returns
Internal BitArray.

Definition at line 165 of file BitArray.cpp.

References m_array.

Referenced by BitArray().

◆ calculateBitmapSize()

Size BitArray::calculateBitmapSize ( const Size  bitCount) const
inlineprivate

Calculate required size of bitmap array in bytes.

Parameters
bitCountMinimum number of bits needed to store.
Returns
Size of bitmap array in bytes

Definition at line 218 of file BitArray.cpp.

Referenced by BitArray(), and clear().

◆ clear()

void BitArray::clear ( )

Set all bits to zero.

Definition at line 199 of file BitArray.cpp.

References calculateBitmapSize(), m_array, m_bitCount, m_set, and MemoryBlock::set().

Referenced by BitArray().

◆ count()

Size BitArray::count ( const bool  on) const

Get the number of bits in the map which have the given value.

Parameters
onTrue to get the number of 1-bits, false for 0-bits.
Returns
Number of bits with the given value.

Definition at line 45 of file BitArray.cpp.

References m_bitCount, and m_set.

Referenced by BitAllocator::available(), and setNext().

◆ isSet()

bool BitArray::isSet ( const Size  bit) const

Verify if a given bit is set.

Parameters
bitBit number to check.
Returns
True if marked, false otherwise.

Definition at line 82 of file BitArray.cpp.

References assert, m_array, and m_bitCount.

Referenced by BitAllocator::isAllocated(), operator[](), setArray(), and setNext().

◆ operator[]() [1/2]

bool BitArray::operator[] ( const int  bit) const

Retrieve the value of the given bit.

Parameters
bitBit number to find
Returns
True if set, false otherwise

Definition at line 213 of file BitArray.cpp.

References isSet().

◆ operator[]() [2/2]

bool BitArray::operator[] ( const Size  bit) const

Retrieve the value of the given bit.

Parameters
bitBit number to find
Returns
True if set, false otherwise

Definition at line 208 of file BitArray.cpp.

References isSet().

◆ set()

void BitArray::set ( const Size  bit,
const bool  value = true 
)

Sets the given bit to the given value.

Parameters
bitBit number to set.
valueValue to set.

Definition at line 50 of file BitArray.cpp.

References m_array, m_bitCount, and m_set.

Referenced by BitAllocator::allocateAt(), LinnCreate::create(), LinnCreate::createInode(), setRange(), and unset().

◆ setArray()

void BitArray::setArray ( u8 array,
const Size  bitCount = ZERO 
)

Use the given pointer as the BitArray buffer.

Parameters
arrayNew bits array pointer.
bitCountNew number of bits. ZERO to keep the old value.

Definition at line 170 of file BitArray.cpp.

References isSet(), m_allocated, m_array, m_bitCount, and m_set.

Referenced by LinnCreate::create(), and LinnCreate::createInode().

◆ setNext()

BitArray::Result BitArray::setNext ( Size bit,
const Size  count = 1,
const Size  offset = 0,
const Size  boundary = 1 
)

Sets the next unset bit(s).

Parameters
bitStart bit number on success.
countNumber of consequetive bits required.
offsetStart bit number to start searching at inside the BitArray.
boundaryFirst bit number must be on the given alignment boundary.
Returns
Result code.

Definition at line 97 of file BitArray.cpp.

References count(), isSet(), m_array, m_bitCount, OutOfMemory, setRange(), and Success.

Referenced by BitAllocator::allocateFrom(), and LinnCreate::createInode().

◆ setRange()

void BitArray::setRange ( const Size  from,
const Size  to 
)

Set a range of bits inside the map to 1.

Parameters
fromBit to start with.
toEnd bit (inclusive).

Definition at line 89 of file BitArray.cpp.

References set().

Referenced by setNext().

◆ size()

Size BitArray::size ( ) const

Returns the maximum size of this Container.

Returns
size The maximum size of this Container.

Definition at line 40 of file BitArray.cpp.

References m_bitCount.

◆ unset()

void BitArray::unset ( const Size  bit)

Sets the given bit to zero.

Parameters
bitBit number to unset.

Definition at line 77 of file BitArray.cpp.

References set().

Referenced by BitAllocator::release().

Field Documentation

◆ m_allocated

bool BitArray::m_allocated
private

True if m_array was allocated interally.

Definition at line 189 of file BitArray.h.

Referenced by BitArray(), setArray(), and ~BitArray().

◆ m_array

u8* BitArray::m_array
private

Array containing the bits.

Definition at line 186 of file BitArray.h.

Referenced by array(), BitArray(), clear(), isSet(), set(), setArray(), setNext(), and ~BitArray().

◆ m_bitCount

Size BitArray::m_bitCount
private

Total number of bits in the array.

Definition at line 180 of file BitArray.h.

Referenced by BitArray(), clear(), count(), isSet(), set(), setArray(), setNext(), and size().

◆ m_set

Size BitArray::m_set
private

Set bits in the array.

Definition at line 183 of file BitArray.h.

Referenced by BitArray(), clear(), count(), set(), and setArray().


The documentation for this class was generated from the following files: