Skip to content

Truly independent web browser

AK: Remove unused Bitmap and BitmapView methods

Jelle Raaijmakerse470b66d4f39
diff --git a/AK/Bitmap.h b/AK/Bitmap.h
index 85098ef597b..eb9971ede5b 100644
--- a/AK/Bitmap.h
+++ b/AK/Bitmap.h
@@ -65,8 +65,6 @@ public:
m_data = nullptr;
}
- [[nodiscard]] BitmapView view() const { return *this; }
-
void set(size_t index, bool value)
{
VERIFY(index < m_size);
@@ -76,103 +74,6 @@ public:
m_data[index / 8] &= static_cast<u8>(~(1u << (index % 8)));
}
- // NOTE: There's a const method variant of this method at the parent class BitmapView.
- [[nodiscard]] u8* data() { return m_data; }
-
- void grow(size_t size, bool default_value)
- {
- VERIFY(size > m_size);
-
- auto previous_size_bytes = size_in_bytes();
- auto previous_size = m_size;
- auto* previous_data = m_data;
-
- m_size = size;
- m_data = reinterpret_cast<u8*>(kmalloc(size_in_bytes()));
-
- fill(default_value);
-
- if (previous_data != nullptr) {
- __builtin_memcpy(m_data, previous_data, previous_size_bytes);
- if ((previous_size % 8) != 0)
- set_range(previous_size, 8 - previous_size % 8, default_value);
- kfree(previous_data);
- }
- }
-
- 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);
- }
- }
- }
-
- 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());
diff --git a/AK/BitmapView.h b/AK/BitmapView.h
index 8377762e791..b139e546beb 100644
--- a/AK/BitmapView.h
+++ b/AK/BitmapView.h
@@ -35,61 +35,6 @@ public:
return 0 != (m_data[index / 8] & (1u << (index % 8)));
}
- [[nodiscard]] size_t count_slow(bool value) const
- {
- return count_in_range(0, m_size, value);
- }
-
- [[nodiscard]] size_t count_in_range(size_t start, size_t len, bool value) const
- {
- VERIFY(start < m_size);
- VERIFY(start + len <= m_size);
- if (len == 0)
- return 0;
-
- size_t count;
- u8 const* first = &m_data[start / 8];
- u8 const* last = &m_data[(start + len) / 8];
- u8 byte = *first;
- byte &= bitmask_first_byte[start % 8];
- if (first == last) {
- byte &= bitmask_last_byte[(start + len) % 8];
- count = popcount(byte);
- } else {
- count = popcount(byte);
- // Don't access *last if it's out of bounds
- if (last < &m_data[size_in_bytes()]) {
- byte = *last;
- byte &= bitmask_last_byte[(start + len) % 8];
- count += popcount(byte);
- }
- if (++first < last) {
- size_t const* ptr_large = reinterpret_cast<size_t const*>((reinterpret_cast<FlatPtr>(first) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1));
- if (reinterpret_cast<u8 const*>(ptr_large) > last)
- ptr_large = reinterpret_cast<size_t const*>(last);
- while (first < reinterpret_cast<u8 const*>(ptr_large)) {
- count += popcount(*first);
- first++;
- }
- size_t const* last_large = reinterpret_cast<size_t const*>(reinterpret_cast<FlatPtr>(last) & ~(sizeof(size_t) - 1));
- while (ptr_large < last_large) {
- count += popcount(*ptr_large);
- ptr_large++;
- }
- for (first = reinterpret_cast<u8 const*>(ptr_large); first < last; first++)
- count += popcount(*first);
- }
- }
-
- if (!value)
- count = len - count;
- return count;
- }
-
- [[nodiscard]] bool is_null() const { return m_data == nullptr; }
-
- [[nodiscard]] u8 const* data() const { return m_data; }
-
template<bool VALUE>
Optional<size_t> find_one_anywhere(size_t hint = 0) const
{
@@ -180,189 +125,6 @@ public:
Optional<size_t> find_first_set() const { return find_first<true>(); }
Optional<size_t> find_first_unset() const { return find_first<false>(); }
- // The function will return the next range of unset bits starting from the
- // @from value.
- // @from: the position from which the search starts. The var will be
- // changed and new value is the offset of the found block.
- // @min_length: minimum size of the range which will be returned.
- // @max_length: maximum size of the range which will be returned.
- // This is used to increase performance, since the range of
- // unset bits can be long, and we don't need the while range,
- // so we can stop when we've reached @max_length.
- inline Optional<size_t> find_next_range_of_unset_bits(size_t& from, size_t min_length = 1, size_t max_length = max_size) const
- {
- if (min_length > max_length) {
- return {};
- }
-
- size_t bit_size = 8 * sizeof(size_t);
-
- size_t* bitmap = reinterpret_cast<size_t*>(m_data);
-
- // Calculating the start offset.
- size_t start_bucket_index = from / bit_size;
- size_t start_bucket_bit = from % bit_size;
-
- size_t* start_of_free_chunks = &from;
- size_t free_chunks = 0;
-
- for (size_t bucket_index = start_bucket_index; bucket_index < m_size / bit_size; ++bucket_index) {
- if (bitmap[bucket_index] == NumericLimits<size_t>::max()) {
- // Skip over completely full bucket of size bit_size.
- if (free_chunks >= min_length) {
- return min(free_chunks, max_length);
- }
- free_chunks = 0;
- start_bucket_bit = 0;
- continue;
- }
- if (bitmap[bucket_index] == 0x0) {
- // Skip over completely empty bucket of size bit_size.
- if (free_chunks == 0) {
- *start_of_free_chunks = bucket_index * bit_size;
- }
- free_chunks += bit_size;
- if (free_chunks >= max_length) {
- return max_length;
- }
- start_bucket_bit = 0;
- continue;
- }
-
- size_t bucket = bitmap[bucket_index];
- u8 viewed_bits = start_bucket_bit;
- u32 trailing_zeroes = 0;
-
- bucket >>= viewed_bits;
- start_bucket_bit = 0;
-
- while (viewed_bits < bit_size) {
- if (bucket == 0) {
- if (free_chunks == 0) {
- *start_of_free_chunks = bucket_index * bit_size + viewed_bits;
- }
- free_chunks += bit_size - viewed_bits;
- viewed_bits = bit_size;
- } else {
- trailing_zeroes = count_trailing_zeroes(bucket);
- bucket >>= trailing_zeroes;
-
- if (free_chunks == 0) {
- *start_of_free_chunks = bucket_index * bit_size + viewed_bits;
- }
- free_chunks += trailing_zeroes;
- viewed_bits += trailing_zeroes;
-
- if (free_chunks >= min_length) {
- return min(free_chunks, max_length);
- }
-
- // Deleting trailing ones.
- u32 trailing_ones = count_trailing_zeroes(~bucket);
- bucket >>= trailing_ones;
- viewed_bits += trailing_ones;
- free_chunks = 0;
- }
- }
- }
-
- if (free_chunks < min_length) {
- size_t first_trailing_bit = (m_size / bit_size) * bit_size;
- size_t trailing_bits = size() % bit_size;
- for (size_t i = 0; i < trailing_bits; ++i) {
- if (!get(first_trailing_bit + i)) {
- if (free_chunks == 0)
- *start_of_free_chunks = first_trailing_bit + i;
- if (++free_chunks >= min_length)
- return min(free_chunks, max_length);
- } else {
- free_chunks = 0;
- }
- }
- return {};
- }
-
- return min(free_chunks, max_length);
- }
-
- Optional<size_t> find_longest_range_of_unset_bits(size_t max_length, size_t& found_range_size) const
- {
- size_t start = 0;
- size_t max_region_start = 0;
- size_t max_region_size = 0;
-
- while (true) {
- // Look for the next block which is bigger than currunt.
- auto length_of_found_range = find_next_range_of_unset_bits(start, max_region_size + 1, max_length);
- if (length_of_found_range.has_value()) {
- max_region_start = start;
- max_region_size = length_of_found_range.value();
- start += max_region_size;
- } else {
- // No ranges which are bigger than current were found.
- break;
- }
- }
-
- found_range_size = max_region_size;
- if (max_region_size != 0) {
- return max_region_start;
- }
- return {};
- }
-
- Optional<size_t> find_first_fit(size_t minimum_length) const
- {
- size_t start = 0;
- auto length_of_found_range = find_next_range_of_unset_bits(start, minimum_length, minimum_length);
- if (length_of_found_range.has_value()) {
- return start;
- }
- return {};
- }
-
- Optional<size_t> find_best_fit(size_t minimum_length) const
- {
- size_t start = 0;
- size_t best_region_start = 0;
- size_t best_region_size = max_size;
- bool found = false;
-
- while (true) {
- // Look for the next block which is bigger than requested length.
- auto length_of_found_range = find_next_range_of_unset_bits(start, minimum_length, best_region_size);
- if (length_of_found_range.has_value()) {
- if (best_region_size > length_of_found_range.value() || !found) {
- best_region_start = start;
- best_region_size = length_of_found_range.value();
- found = true;
- }
- start += length_of_found_range.value();
- } else {
- // There are no ranges which can fit requested length.
- break;
- }
- }
-
- if (found) {
- return best_region_start;
- }
- return {};
- }
-
- static constexpr size_t max_size = 0xffffffff;
-
- [[nodiscard]] bool operator==(BitmapView const& other) const
- {
- if (size() != other.size())
- return false;
- for (size_t i = 0; i < size_in_bytes(); ++i) {
- if (m_data[i] != other.m_data[i])
- return false;
- }
- return true;
- }
-
protected:
u8* m_data { nullptr };
size_t m_size { 0 };
diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp
index 7c4a0002a21..d8353b0759c 100644
--- a/Libraries/LibGfx/Bitmap.cpp
+++ b/Libraries/LibGfx/Bitmap.cpp
@@ -6,7 +6,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/Bitmap.h>
#include <AK/Checked.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h>
diff --git a/Libraries/LibJS/Runtime/DeclarativeEnvironment.h b/Libraries/LibJS/Runtime/DeclarativeEnvironment.h
index 477bb678381..61c62ff39f9 100644
--- a/Libraries/LibJS/Runtime/DeclarativeEnvironment.h
+++ b/Libraries/LibJS/Runtime/DeclarativeEnvironment.h
@@ -6,7 +6,6 @@
#pragma once
-#include <AK/Bitmap.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Utf16FlyString.h>
diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
index ca787894ebe..ef064ca1237 100644
--- a/Libraries/LibWeb/Animations/KeyframeEffect.cpp
+++ b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
@@ -5,7 +5,6 @@
*/
#include <AK/Atomic.h>
-#include <AK/Bitmap.h>
#include <AK/InsertionSort.h>
#include <AK/QuickSort.h>
#include <LibGC/Heap.h>
diff --git a/Libraries/LibWeb/CSS/DevToolsAppliedStyleRules.cpp b/Libraries/LibWeb/CSS/DevToolsAppliedStyleRules.cpp
index 01058f45372..3e75fe207f2 100644
--- a/Libraries/LibWeb/CSS/DevToolsAppliedStyleRules.cpp
+++ b/Libraries/LibWeb/CSS/DevToolsAppliedStyleRules.cpp
@@ -6,7 +6,6 @@
#include <AK/Array.h>
#include <AK/BinarySearch.h>
-#include <AK/Bitmap.h>
#include <AK/BuiltinWrappers.h>
#include <AK/Debug.h>
#include <AK/Error.h>
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index d521066a4a4..1e64d09790f 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -9,7 +9,6 @@
#include <AK/Array.h>
#include <AK/BinarySearch.h>
-#include <AK/Bitmap.h>
#include <AK/BuiltinWrappers.h>
#include <AK/Debug.h>
#include <AK/Error.h>
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index a679cc32d75..3c5d05ea8c8 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -11,7 +11,6 @@
*/
#include <AK/AnyOf.h>
-#include <AK/Bitmap.h>
#include <AK/CharacterTypes.h>
#include <AK/Debug.h>
#include <AK/GenericLexer.h>
diff --git a/Tests/AK/TestBitmap.cpp b/Tests/AK/TestBitmap.cpp
index fda937767bb..0a4fab6855a 100644
--- a/Tests/AK/TestBitmap.cpp
+++ b/Tests/AK/TestBitmap.cpp
@@ -222,183 +222,6 @@ TEST_CASE(find_one_anywhere_unset)
}
}
-TEST_CASE(find_first_range)
-{
- auto bitmap = MUST(Bitmap::create(128, true));
- bitmap.set(47, false);
- bitmap.set(48, false);
- bitmap.set(49, false);
- bitmap.set(50, false);
- bitmap.set(51, false);
- size_t found_range_size = 0;
- auto result = bitmap.find_longest_range_of_unset_bits(5, found_range_size);
- EXPECT_EQ(result.has_value(), true);
- EXPECT_EQ(found_range_size, 5u);
- EXPECT_EQ(result.value(), 47u);
-}
-
-TEST_CASE(set_range)
-{
- {
- auto bitmap = MUST(Bitmap::create(128, false));
- bitmap.set_range(41, 10, true);
- EXPECT_EQ(bitmap.get(40), false);
- EXPECT_EQ(bitmap.get(41), true);
- EXPECT_EQ(bitmap.get(42), true);
- EXPECT_EQ(bitmap.get(43), true);
- EXPECT_EQ(bitmap.get(44), true);
- EXPECT_EQ(bitmap.get(45), true);
- EXPECT_EQ(bitmap.get(46), true);
- EXPECT_EQ(bitmap.get(47), true);
- EXPECT_EQ(bitmap.get(48), true);
- EXPECT_EQ(bitmap.get(49), true);
- EXPECT_EQ(bitmap.get(50), true);
- EXPECT_EQ(bitmap.get(51), false);
- }
- {
- auto bitmap = MUST(Bitmap::create(288, false));
- bitmap.set_range(48, 32, true);
- bitmap.set_range(94, 39, true);
- bitmap.set_range(190, 71, true);
- bitmap.set_range(190 + 71 - 7, 21, false); // slightly overlapping clear
- for (size_t i = 0; i < bitmap.size(); i++) {
- bool should_be_set = (i >= 48 && i < 48 + 32)
- || (i >= 94 && i < 94 + 39)
- || ((i >= 190 && i < 190 + 71) && !(i >= 190 + 71 - 7 && i < 190 + 71 - 7 + 21));
- EXPECT_EQ(bitmap.get(i), should_be_set);
- }
- EXPECT_EQ(bitmap.count_slow(true), 32u + 39u + 71u - 7u);
- }
-}
-
-TEST_CASE(find_first_fit)
-{
- {
- auto bitmap = MUST(Bitmap::create(32, true));
- auto fit = bitmap.find_first_fit(1);
- EXPECT_EQ(fit.has_value(), false);
- }
- {
- auto bitmap = MUST(Bitmap::create(32, true));
- bitmap.set(31, false);
- auto fit = bitmap.find_first_fit(1);
- EXPECT_EQ(fit.has_value(), true);
- EXPECT_EQ(fit.value(), 31u);
- }
-
- for (size_t i = 0; i < 128; ++i) {
- auto bitmap = MUST(Bitmap::create(128, true));
- bitmap.set(i, false);
- auto fit = bitmap.find_first_fit(1);
- EXPECT_EQ(fit.has_value(), true);
- EXPECT_EQ(fit.value(), i);
- }
-
- for (size_t i = 0; i < 127; ++i) {
- auto bitmap = MUST(Bitmap::create(128, true));
- bitmap.set(i, false);
- bitmap.set(i + 1, false);
- auto fit = bitmap.find_first_fit(2);
- EXPECT_EQ(fit.has_value(), true);
- EXPECT_EQ(fit.value(), i);
- }
-
- size_t bitmap_size = 1024;
- for (size_t chunk_size = 1; chunk_size < 64; ++chunk_size) {
- for (size_t i = 0; i < bitmap_size - chunk_size; ++i) {
- auto bitmap = MUST(Bitmap::create(bitmap_size, true));
- for (size_t c = 0; c < chunk_size; ++c)
- bitmap.set(i + c, false);
- auto fit = bitmap.find_first_fit(chunk_size);
- EXPECT_EQ(fit.has_value(), true);
- EXPECT_EQ(fit.value(), i);
- }
- }
-}
-
-TEST_CASE(find_longest_range_of_unset_bits_edge)
-{
- auto bitmap = MUST(Bitmap::create(36, true));
- bitmap.set_range(32, 4, false);
- size_t found_range_size = 0;
- auto result = bitmap.find_longest_range_of_unset_bits(1, found_range_size);
- EXPECT_EQ(result.has_value(), true);
- EXPECT_EQ(result.value(), 32u);
-}
-
-TEST_CASE(count_in_range)
-{
- auto bitmap = MUST(Bitmap::create(256, false));
- bitmap.set(14, true);
- bitmap.set(17, true);
- bitmap.set(19, true);
- bitmap.set(20, true);
- for (size_t i = 34; i < 250; i++) {
- if (i < 130 || i > 183)
- bitmap.set(i, true);
- }
-
- auto count_bits_slow = [](Bitmap const& b, size_t start, size_t len, bool value) -> size_t {
- size_t count = 0;
- for (size_t i = start; i < start + len; i++) {
- if (b.get(i) == value)
- count++;
- }
- return count;
- };
- auto test_with_value = [&](bool value) {
- auto do_test = [&](size_t start, size_t len) {
- EXPECT_EQ(bitmap.count_in_range(start, len, value), count_bits_slow(bitmap, start, len, value));
- };
- do_test(16, 2);
- do_test(16, 3);
- do_test(16, 4);
-
- for (size_t start = 8; start < 24; start++) {
- for (size_t end = 9; end < 25; end++) {
- if (start >= end)
- continue;
- do_test(start, end - start);
- }
- }
-
- for (size_t start = 1; start <= 9; start++) {
- for (size_t i = start + 1; i < bitmap.size() - start + 1; i++)
- do_test(start, i - start);
- }
- };
- test_with_value(true);
- test_with_value(false);
-}
-
-TEST_CASE(byte_aligned_access)
-{
- {
- auto bitmap = MUST(Bitmap::create(16, true));
- EXPECT_EQ(bitmap.count_in_range(0, 16, true), 16u);
- EXPECT_EQ(bitmap.count_in_range(8, 8, true), 8u);
- EXPECT_EQ(bitmap.count_in_range(0, 8, true), 8u);
- EXPECT_EQ(bitmap.count_in_range(4, 8, true), 8u);
- }
- {
- auto bitmap = MUST(Bitmap::create(16, false));
- bitmap.set_range(4, 8, true);
- EXPECT_EQ(bitmap.count_in_range(0, 16, true), 8u);
- EXPECT_EQ(bitmap.count_in_range(8, 8, true), 4u);
- EXPECT_EQ(bitmap.count_in_range(0, 8, true), 4u);
- EXPECT_EQ(bitmap.count_in_range(4, 8, true), 8u);
- }
- {
- auto bitmap = MUST(Bitmap::create(8, false));
- bitmap.set(2, true);
- bitmap.set(4, true);
- EXPECT_EQ(bitmap.count_in_range(0, 2, true), 0u);
- EXPECT_EQ(bitmap.count_in_range(0, 4, true), 1u);
- EXPECT_EQ(bitmap.count_in_range(0, 8, true), 2u);
- EXPECT_EQ(bitmap.count_in_range(4, 4, true), 1u);
- }
-}
-
RANDOMIZED_TEST_CASE(set_get)
{
GEN(init, Gen::boolean());
@@ -412,24 +235,6 @@ RANDOMIZED_TEST_CASE(set_get)
EXPECT_EQ(bitmap.get(i), new_value);
}
-RANDOMIZED_TEST_CASE(set_range)
-{
- GEN(init, Gen::boolean());
- GEN(size, Gen::number_u64(1, 64));
- GEN(new_value, Gen::boolean());
-
- GEN(start, Gen::number_u64(size - 1));
- GEN(len, Gen::number_u64(size - start - 1));
-
- auto bitmap = MUST(Bitmap::create(size, init));
- bitmap.set_range(start, len, new_value);
-
- for (size_t i = start; i < start + len; ++i)
- EXPECT_EQ(bitmap.get(i), new_value);
-
- EXPECT_EQ(bitmap.count_in_range(start, len, new_value), len);
-}
-
RANDOMIZED_TEST_CASE(fill)
{
GEN(init, Gen::boolean());
@@ -439,7 +244,8 @@ RANDOMIZED_TEST_CASE(fill)
auto bitmap = MUST(Bitmap::create(size, init));
bitmap.fill(new_value);
- EXPECT_EQ(bitmap.count_slow(new_value), size);
+ for (size_t i = 0; i < size; ++i)
+ EXPECT_EQ(bitmap.get(i), new_value);
}
TEST_CASE(find_one_anywhere_edge_case)