2022-12-04 14:14:07 +00:00
|
|
|
#include <luna/Bitmap.h>
|
2022-12-16 19:40:04 +00:00
|
|
|
#include <luna/CString.h>
|
2022-12-04 14:14:07 +00:00
|
|
|
#include <luna/Check.h>
|
|
|
|
|
|
|
|
Bitmap::Bitmap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:47:46 +00:00
|
|
|
Bitmap::Bitmap(void* location, usize size_in_bytes) : m_location((u8*)location), m_size_in_bytes(size_in_bytes)
|
2022-12-04 14:14:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::initialize(void* location, usize size_in_bytes)
|
|
|
|
{
|
2022-12-07 10:47:46 +00:00
|
|
|
m_location = (u8*)location;
|
2022-12-04 14:14:07 +00:00
|
|
|
m_size_in_bytes = size_in_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* Bitmap::move(void* new_location, usize new_location_size_in_bytes)
|
|
|
|
{
|
|
|
|
expect(initialized(), "Bitmap was never initialized");
|
|
|
|
|
|
|
|
if (new_location_size_in_bytes > m_size_in_bytes) memcpy(new_location, m_location, m_size_in_bytes);
|
|
|
|
else
|
|
|
|
memcpy(new_location, m_location, new_location_size_in_bytes);
|
|
|
|
|
|
|
|
void* old_location = (void*)m_location;
|
|
|
|
|
2022-12-07 10:47:46 +00:00
|
|
|
m_location = (u8*)new_location;
|
2022-12-04 14:14:07 +00:00
|
|
|
m_size_in_bytes = new_location_size_in_bytes;
|
|
|
|
|
|
|
|
return old_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::set(usize index, bool value)
|
|
|
|
{
|
|
|
|
expect(initialized(), "Bitmap was never initialized");
|
|
|
|
expect(index < size(), "Bitmap access out of range");
|
|
|
|
u64 byte_index = index / 8;
|
2022-12-07 10:47:46 +00:00
|
|
|
u8 bit_mask = (u8)(0b10000000 >> (index % 8));
|
2022-12-04 14:14:07 +00:00
|
|
|
m_location[byte_index] &= (u8)(~bit_mask);
|
|
|
|
if (value) { m_location[byte_index] |= bit_mask; }
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:50:30 +00:00
|
|
|
bool Bitmap::get(usize index) const
|
2022-12-04 14:14:07 +00:00
|
|
|
{
|
|
|
|
expect(initialized(), "Bitmap was never initialized");
|
|
|
|
expect(index < size(), "Bitmap access out of range");
|
|
|
|
usize byte_index = index / 8;
|
2022-12-07 10:47:46 +00:00
|
|
|
u8 bit_mask = (u8)(0b10000000 >> (index % 8));
|
2022-12-04 14:14:07 +00:00
|
|
|
return (m_location[byte_index] & bit_mask) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::clear(bool value)
|
|
|
|
{
|
|
|
|
expect(initialized(), "Bitmap was never initialized");
|
2022-12-05 11:53:16 +00:00
|
|
|
memset(m_location, value_byte(value), m_size_in_bytes);
|
2022-12-04 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bitmap::clear_region(usize start, usize bits, bool value)
|
|
|
|
{
|
|
|
|
expect(initialized(), "Bitmap was never initialized");
|
|
|
|
expect((start + bits) <= size(), "Bitmap clear out of range");
|
|
|
|
|
2022-12-07 13:32:41 +00:00
|
|
|
if (!bits) return;
|
|
|
|
|
2022-12-04 14:14:07 +00:00
|
|
|
// Set individual bits while not on a byte boundary.
|
2022-12-07 13:46:11 +00:00
|
|
|
while ((start % 8) && bits)
|
2022-12-04 14:14:07 +00:00
|
|
|
{
|
|
|
|
set(start, value);
|
|
|
|
start++;
|
2022-12-07 13:46:11 +00:00
|
|
|
bits--;
|
2022-12-04 14:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out the rest in bytes.
|
|
|
|
usize bytes = bits / 8;
|
|
|
|
|
2022-12-05 11:53:16 +00:00
|
|
|
memset(&m_location[start / 8], value_byte(value), bytes);
|
2022-12-04 14:14:07 +00:00
|
|
|
start += bytes * 8;
|
|
|
|
bits -= bytes * 8;
|
|
|
|
|
|
|
|
// Set the remaining individual bits.
|
|
|
|
while (bits--)
|
|
|
|
{
|
|
|
|
set(start, value);
|
|
|
|
start++;
|
|
|
|
}
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|