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>
|
2022-02-10 16:53:26 +00:00
|
|
|
#include <AK/Error.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>
|
2022-02-10 16:53:26 +00:00
|
|
|
#include <AK/Try.h>
|
2019-08-01 09:34:36 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-11-06 15:20:18 +00:00
|
|
|
class Bitmap : public BitmapView {
|
2020-08-26 19:52:24 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Bitmap);
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2022-12-20 16:34:13 +00:00
|
|
|
static ErrorOr<Bitmap> create(size_t size, bool default_value)
|
2018-10-10 09:53:07 +00:00
|
|
|
{
|
2021-11-06 15:20:18 +00:00
|
|
|
VERIFY(size != 0);
|
2022-02-10 16:53:26 +00:00
|
|
|
|
|
|
|
auto* data = kmalloc(ceil_div(size, static_cast<size_t>(8)));
|
|
|
|
if (!data)
|
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
2023-03-07 14:28:21 +00:00
|
|
|
auto bitmap = Bitmap { static_cast<u8*>(data), size, true };
|
2022-02-10 16:53:26 +00:00
|
|
|
bitmap.fill(default_value);
|
|
|
|
return bitmap;
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:53:26 +00:00
|
|
|
Bitmap() = default;
|
|
|
|
|
2021-05-17 00:00:50 +00:00
|
|
|
Bitmap(u8* data, size_t size, bool is_owning = false)
|
2021-11-06 15:20:18 +00:00
|
|
|
: BitmapView(data, size)
|
2021-05-17 00:00:50 +00:00
|
|
|
, m_is_owning(is_owning)
|
2021-05-17 16:29:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-01 09:34:36 +00:00
|
|
|
Bitmap(Bitmap&& other)
|
2021-11-06 15:20:18 +00:00
|
|
|
: BitmapView(exchange(other.m_data, nullptr), exchange(other.m_size, 0))
|
2019-08-01 09:34:36 +00:00
|
|
|
{
|
2021-11-06 15:20:18 +00:00
|
|
|
m_is_owning = exchange(other.m_is_owning, false);
|
2019-08-01 09:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Bitmap& operator=(Bitmap&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
2021-07-11 11:22:27 +00:00
|
|
|
kfree_sized(m_data, size_in_bytes());
|
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) {
|
2021-07-11 11:22:27 +00:00
|
|
|
kfree_sized(m_data, size_in_bytes());
|
2021-05-17 00:00:50 +00:00
|
|
|
}
|
2018-10-10 09:53:07 +00:00
|
|
|
m_data = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-11-14 19:45:29 +00:00
|
|
|
[[nodiscard]] BitmapView view() const { return *this; }
|
2021-03-04 10:05:34 +00:00
|
|
|
|
2021-07-12 19:30:52 +00:00
|
|
|
void set(size_t index, bool value)
|
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
|
|
|
}
|
|
|
|
|
2022-02-04 20:18:49 +00:00
|
|
|
// NOTE: There's a const method variant of this method at the parent class BitmapView.
|
2021-07-21 16:08:27 +00:00
|
|
|
[[nodiscard]] u8* data() { return m_data; }
|
2021-11-06 15:20:18 +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;
|
2021-10-31 20:56:10 +00:00
|
|
|
auto* previous_data = m_data;
|
2019-06-11 12:50:41 +00:00
|
|
|
|
|
|
|
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);
|
2021-11-06 20:12:16 +00:00
|
|
|
if ((previous_size % 8) != 0)
|
2020-03-08 14:53:38 +00:00
|
|
|
set_range(previous_size, 8 - previous_size % 8, default_value);
|
2021-07-11 11:22:27 +00:00
|
|
|
kfree_sized(previous_data, previous_size_bytes);
|
2019-06-11 12:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-10-01 17:58:07 +00:00
|
|
|
private:
|
2021-05-17 00:00:50 +00:00
|
|
|
bool m_is_owning { true };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-10 09:53:07 +00:00
|
|
|
using AK::Bitmap;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|