From c1a3cf129073dfd61134dbb9c346dca90de7797f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 11 Jan 2026 00:27:24 +0100 Subject: [PATCH] AK: Add FixedBitmap template class for stack-allocated bitmaps This is a simple bitmap class that stores its data in a fixed-size array on the stack, avoiding heap allocation for small, known-size bitmaps. --- AK/FixedBitmap.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 AK/FixedBitmap.h diff --git a/AK/FixedBitmap.h b/AK/FixedBitmap.h new file mode 100644 index 0000000000..bdce1de636 --- /dev/null +++ b/AK/FixedBitmap.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2026, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace AK { + +template +class FixedBitmap { +public: + constexpr FixedBitmap(bool default_value) + { + fill(default_value); + } + + constexpr void fill(bool value) + { + __builtin_memset(m_data.data(), value ? 0xff : 0x00, size_in_bytes()); + } + + constexpr void set(size_t index, bool value) + { + VERIFY(index < Size); + if (value) + m_data[index / 8] |= static_cast(1u << (index % 8)); + else + m_data[index / 8] &= static_cast(~(1u << (index % 8))); + } + + [[nodiscard]] constexpr bool get(size_t index) const + { + VERIFY(index < Size); + return 0 != (m_data[index / 8] & (1u << (index % 8))); + } + + [[nodiscard]] constexpr size_t size() const { return Size; } + [[nodiscard]] constexpr size_t size_in_bytes() const { return ceil_div(Size, static_cast(8)); } + +private: + Array(8))> m_data; +}; + +}