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