2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-03-04 10:05:34 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
#include <AK/BitmapView.h>
|
2019-08-01 09:34:36 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2020-02-24 08:55:46 +00:00
|
|
|
#include <AK/Optional.h>
|
2020-03-08 14:53:38 +00:00
|
|
|
#include <AK/Platform.h>
|
2019-08-01 09:34:36 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class Bitmap {
|
2020-08-26 19:52:24 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Bitmap);
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2021-03-04 10:05:34 +00:00
|
|
|
Bitmap() = default;
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
Bitmap(size_t size, bool default_value)
|
|
|
|
: m_size(size)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-03-04 10:05:34 +00:00
|
|
|
VERIFY(m_size != 0);
|
|
|
|
m_data = static_cast<u8*>(kmalloc(size_in_bytes()));
|
|
|
|
fill(default_value);
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 00:00:50 +00:00
|
|
|
Bitmap(u8* data, size_t size, bool is_owning = false)
|
2021-05-17 16:29:47 +00:00
|
|
|
: m_data(data)
|
|
|
|
, m_size(size)
|
2021-05-17 00:00:50 +00:00
|
|
|
, m_is_owning(is_owning)
|
2021-05-17 16:29:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
BitmapView view() { return { m_data, m_size }; }
|
|
|
|
const BitmapView view() const { return { m_data, m_size }; }
|
|
|
|
|
2019-08-01 09:34:36 +00:00
|
|
|
Bitmap(Bitmap&& other)
|
2021-03-04 10:05:34 +00:00
|
|
|
: m_data(exchange(other.m_data, nullptr))
|
|
|
|
, m_size(exchange(other.m_size, 0))
|
2019-08-01 09:34:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap& operator=(Bitmap&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
2021-03-04 10:05:34 +00:00
|
|
|
kfree(m_data);
|
2019-08-01 09:34:36 +00:00
|
|
|
m_data = exchange(other.m_data, nullptr);
|
|
|
|
m_size = exchange(other.m_size, 0);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
~Bitmap()
|
|
|
|
{
|
2021-05-17 00:00:50 +00:00
|
|
|
if (m_is_owning) {
|
|
|
|
kfree(m_data);
|
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:55:46 +00:00
|
|
|
size_t size() const { return m_size; }
|
2021-02-14 11:19:08 +00:00
|
|
|
size_t size_in_bytes() const { return ceil_div(m_size, static_cast<size_t>(8)); }
|
2021-03-04 10:05:34 +00:00
|
|
|
|
2020-02-24 08:55:46 +00:00
|
|
|
bool get(size_t index) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(index < m_size);
|
2018-10-10 09:53:07 +00:00
|
|
|
return 0 != (m_data[index / 8] & (1u << (index % 8)));
|
|
|
|
}
|
2021-03-04 10:05:34 +00:00
|
|
|
|
2020-02-24 08:55:46 +00:00
|
|
|
void set(size_t index, bool value) const
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(index < m_size);
|
2018-10-10 09:53:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
size_t count_slow(bool value) const { return count_in_range(0, m_size, value); }
|
|
|
|
size_t count_in_range(size_t start, size_t len, bool value) const { return view().count_in_range(start, len, value); }
|
2020-09-06 14:06:35 +00:00
|
|
|
|
2021-03-04 09:05:07 +00:00
|
|
|
bool is_null() const { return !m_data; }
|
|
|
|
|
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
|
|
|
|
2020-02-24 08:55:46 +00:00
|
|
|
void grow(size_t size, bool default_value)
|
2019-06-11 12:50:41 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(size > m_size);
|
2019-06-11 12:50:41 +00:00
|
|
|
|
|
|
|
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) {
|
2020-03-08 10:57:24 +00:00
|
|
|
__builtin_memcpy(m_data, previous_data, previous_size_bytes);
|
2020-03-08 14:53:38 +00:00
|
|
|
if (previous_size % 8)
|
|
|
|
set_range(previous_size, 8 - previous_size % 8, default_value);
|
2019-06-11 12:50:41 +00:00
|
|
|
kfree(previous_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 00:00:50 +00:00
|
|
|
template<bool VALUE, bool verify_that_all_bits_flip = false>
|
|
|
|
void set_range(size_t start, size_t len)
|
|
|
|
{
|
|
|
|
VERIFY(start < m_size);
|
|
|
|
VERIFY(start + len <= m_size);
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
u8* first = &m_data[start / 8];
|
|
|
|
u8* last = &m_data[(start + len) / 8];
|
|
|
|
u8 byte_mask = bitmask_first_byte[start % 8];
|
|
|
|
if (first == last) {
|
|
|
|
byte_mask &= bitmask_last_byte[(start + len) % 8];
|
|
|
|
if constexpr (verify_that_all_bits_flip) {
|
|
|
|
if constexpr (VALUE) {
|
|
|
|
VERIFY((*first & byte_mask) == 0);
|
|
|
|
} else {
|
|
|
|
VERIFY((*first & byte_mask) == byte_mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if constexpr (VALUE)
|
|
|
|
*first |= byte_mask;
|
|
|
|
else
|
|
|
|
*first &= ~byte_mask;
|
|
|
|
} else {
|
|
|
|
if constexpr (verify_that_all_bits_flip) {
|
|
|
|
if constexpr (VALUE) {
|
|
|
|
VERIFY((*first & byte_mask) == 0);
|
|
|
|
} else {
|
|
|
|
VERIFY((*first & byte_mask) == byte_mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if constexpr (VALUE)
|
|
|
|
*first |= byte_mask;
|
|
|
|
else
|
|
|
|
*first &= ~byte_mask;
|
|
|
|
byte_mask = bitmask_last_byte[(start + len) % 8];
|
|
|
|
if constexpr (verify_that_all_bits_flip) {
|
|
|
|
if constexpr (VALUE) {
|
|
|
|
VERIFY((*last & byte_mask) == 0);
|
|
|
|
} else {
|
|
|
|
VERIFY((*last & byte_mask) == byte_mask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if constexpr (VALUE)
|
|
|
|
*last |= byte_mask;
|
|
|
|
else
|
|
|
|
*last &= ~byte_mask;
|
|
|
|
if (++first < last) {
|
|
|
|
if constexpr (VALUE)
|
|
|
|
__builtin_memset(first, 0xFF, last - first);
|
|
|
|
else
|
|
|
|
__builtin_memset(first, 0x0, last - first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 14:06:35 +00:00
|
|
|
|
2021-05-17 00:00:50 +00:00
|
|
|
void set_range(size_t start, size_t len, bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
set_range<true, false>(start, len);
|
|
|
|
else
|
|
|
|
set_range<false, false>(start, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_range_and_verify_that_all_bits_flip(size_t start, size_t len, bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
set_range<true, true>(start, len);
|
|
|
|
else
|
|
|
|
set_range<false, true>(start, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill(bool value)
|
|
|
|
{
|
|
|
|
__builtin_memset(m_data, value ? 0xff : 0x00, size_in_bytes());
|
|
|
|
}
|
2020-09-06 14:06:35 +00:00
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
Optional<size_t> find_one_anywhere_set(size_t hint = 0) const { return view().find_one_anywhere<true>(hint); }
|
|
|
|
Optional<size_t> find_one_anywhere_unset(size_t hint = 0) const { return view().find_one_anywhere<false>(hint); }
|
2019-06-11 12:50:41 +00:00
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
Optional<size_t> find_first_set() const { return view().find_first<true>(); }
|
|
|
|
Optional<size_t> find_first_unset() const { return view().find_first<false>(); }
|
2019-06-11 12:50:41 +00:00
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
Optional<size_t> find_next_range_of_unset_bits(size_t& from, size_t min_length = 1, size_t max_length = max_size) const
|
2020-01-26 08:48:24 +00:00
|
|
|
{
|
2021-03-04 10:05:34 +00:00
|
|
|
return view().find_next_range_of_unset_bits(from, min_length, max_length);
|
2020-03-08 14:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Optional<size_t> find_longest_range_of_unset_bits(size_t max_length, size_t& found_range_size) const
|
|
|
|
{
|
2021-03-04 10:05:34 +00:00
|
|
|
return view().find_longest_range_of_unset_bits(max_length, found_range_size);
|
2020-03-10 11:36:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
Optional<size_t> find_first_fit(size_t minimum_length) const { return view().find_first_fit(minimum_length); }
|
|
|
|
Optional<size_t> find_best_fit(size_t minimum_length) const { return view().find_best_fit(minimum_length); }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-03-04 10:05:34 +00:00
|
|
|
static constexpr size_t max_size = 0xffffffff;
|
2020-03-08 14:53:38 +00:00
|
|
|
|
2019-10-01 17:58:07 +00:00
|
|
|
private:
|
2019-07-03 19:17:35 +00:00
|
|
|
u8* m_data { nullptr };
|
2020-02-24 08:55:46 +00:00
|
|
|
size_t m_size { 0 };
|
2021-05-17 00:00:50 +00:00
|
|
|
bool m_is_owning { true };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Bitmap;
|