2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-28 09:53:16 +00:00
|
|
|
#include "Assertions.h"
|
2018-12-03 23:27:16 +00:00
|
|
|
#include "StdLibExtras.h"
|
2018-10-10 09:53:07 +00:00
|
|
|
#include "Types.h"
|
|
|
|
#include "kmalloc.h"
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class Bitmap {
|
|
|
|
public:
|
|
|
|
// NOTE: A wrapping Bitmap won't try to free the wrapped data.
|
2019-07-03 19:17:35 +00:00
|
|
|
static Bitmap wrap(u8* data, int size)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
return Bitmap(data, size);
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
static Bitmap create(int size, bool default_value = 0)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2018-11-05 12:48:07 +00:00
|
|
|
return Bitmap(size, default_value);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:50:41 +00:00
|
|
|
static Bitmap create()
|
|
|
|
{
|
|
|
|
return Bitmap();
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
~Bitmap()
|
|
|
|
{
|
|
|
|
if (m_owned)
|
|
|
|
kfree(m_data);
|
|
|
|
m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
int size() const { return m_size; }
|
|
|
|
bool get(int index) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(index < m_size);
|
|
|
|
return 0 != (m_data[index / 8] & (1u << (index % 8)));
|
|
|
|
}
|
2019-02-25 21:06:55 +00:00
|
|
|
void set(int index, bool value) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
|
|
|
ASSERT(index < m_size);
|
|
|
|
if (value)
|
2019-07-03 19:17:35 +00:00
|
|
|
m_data[index / 8] |= static_cast<u8>((1u << (index % 8)));
|
2018-10-10 09:53:07 +00:00
|
|
|
else
|
2019-07-03 19:17:35 +00:00
|
|
|
m_data[index / 8] &= static_cast<u8>(~(1u << (index % 8)));
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u8* data() { return m_data; }
|
|
|
|
const u8* data() const { return m_data; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2019-06-11 12:50:41 +00:00
|
|
|
void grow(int size, bool default_value)
|
|
|
|
{
|
|
|
|
ASSERT(size > m_size);
|
|
|
|
|
|
|
|
auto previous_size_bytes = size_in_bytes();
|
|
|
|
auto previous_size = m_size;
|
|
|
|
auto previous_data = m_data;
|
|
|
|
|
|
|
|
m_size = size;
|
2019-07-03 19:17:35 +00:00
|
|
|
m_data = reinterpret_cast<u8*>(kmalloc(size_in_bytes()));
|
2019-06-11 12:50:41 +00:00
|
|
|
|
|
|
|
fill(default_value);
|
|
|
|
|
|
|
|
if (previous_data != nullptr) {
|
|
|
|
memcpy(m_data, previous_data, previous_size_bytes);
|
|
|
|
|
|
|
|
if ((previous_size % 8) != 0) {
|
|
|
|
if (default_value)
|
|
|
|
m_data[previous_size_bytes - 1] |= (0xff >> (previous_size % 8));
|
|
|
|
else
|
|
|
|
m_data[previous_size_bytes - 1] &= ~(0xff >> (previous_size % 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(previous_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:31:57 +00:00
|
|
|
void fill(bool value)
|
|
|
|
{
|
|
|
|
memset(m_data, value ? 0xff : 0x00, size_in_bytes());
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:50:41 +00:00
|
|
|
int find_first_set() const
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (i < m_size / 8 && m_data[i] == 0x00)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
for (j = i * 8; j < m_size; j++)
|
|
|
|
if (get(j))
|
|
|
|
return j;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int find_first_unset() const
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while (i < m_size / 8 && m_data[i] == 0xff)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
int j = 0;
|
|
|
|
for (j = i * 8; j < m_size; j++)
|
|
|
|
if (!get(j))
|
|
|
|
return j;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2019-06-11 12:50:41 +00:00
|
|
|
explicit Bitmap()
|
|
|
|
: m_size(0)
|
|
|
|
, m_owned(true)
|
|
|
|
{
|
|
|
|
m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-25 21:06:55 +00:00
|
|
|
explicit Bitmap(int size, bool default_value)
|
2018-10-10 09:53:07 +00:00
|
|
|
: m_size(size)
|
|
|
|
, m_owned(true)
|
|
|
|
{
|
|
|
|
ASSERT(m_size != 0);
|
2019-07-03 19:17:35 +00:00
|
|
|
m_data = reinterpret_cast<u8*>(kmalloc(size_in_bytes()));
|
2019-05-14 15:31:57 +00:00
|
|
|
fill(default_value);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
Bitmap(u8* data, int size)
|
2018-10-10 09:53:07 +00:00
|
|
|
: m_data(data)
|
|
|
|
, m_size(size)
|
|
|
|
, m_owned(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:31:57 +00:00
|
|
|
int size_in_bytes() const { return ceil_div(m_size, 8); }
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u8* m_data { nullptr };
|
2019-02-25 21:06:55 +00:00
|
|
|
int m_size { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
bool m_owned { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Bitmap;
|