2021-07-11 12:16:13 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
|
2022-01-08 16:06:03 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-07-11 12:16:13 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-01-08 16:06:03 -03:00
|
|
|
#include <AK/Error.h>
|
2021-07-11 12:16:13 -03:00
|
|
|
#include <AK/Iterator.h>
|
|
|
|
|
#include <AK/Span.h>
|
|
|
|
|
#include <AK/kmalloc.h>
|
2022-02-23 07:31:04 -03:00
|
|
|
#include <initializer_list>
|
2021-07-11 12:16:13 -03:00
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
2022-01-12 18:28:43 -03:00
|
|
|
// FixedArray is an Array with a size only known at run-time.
|
|
|
|
|
// It guarantees to only allocate when being constructed, and to only deallocate when being destructed.
|
2021-07-11 12:16:13 -03:00
|
|
|
template<typename T>
|
|
|
|
|
class FixedArray {
|
|
|
|
|
public:
|
2021-09-16 03:20:31 -03:00
|
|
|
FixedArray() = default;
|
2022-01-08 16:06:03 -03:00
|
|
|
|
2023-01-28 17:12:17 -03:00
|
|
|
static ErrorOr<FixedArray<T>> create(std::initializer_list<T> initializer)
|
2022-02-23 07:31:04 -03:00
|
|
|
{
|
2023-01-28 17:12:17 -03:00
|
|
|
auto array = TRY(create(initializer.size()));
|
2022-02-23 07:31:04 -03:00
|
|
|
auto it = initializer.begin();
|
|
|
|
|
for (size_t i = 0; i < array.size(); ++i) {
|
|
|
|
|
array[i] = move(*it);
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 17:12:17 -03:00
|
|
|
static ErrorOr<FixedArray<T>> create(size_t size)
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2022-01-08 16:06:03 -03:00
|
|
|
if (size == 0)
|
|
|
|
|
return FixedArray<T>();
|
2024-05-18 10:56:04 -03:00
|
|
|
auto* elements = reinterpret_cast<T*>(kmalloc(storage_allocation_size(size)));
|
|
|
|
|
if (!elements)
|
2022-01-08 16:06:03 -03:00
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2024-05-18 10:56:04 -03:00
|
|
|
new (&elements[i]) T();
|
|
|
|
|
return FixedArray<T>(size, elements);
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
2022-01-08 16:06:03 -03:00
|
|
|
|
|
|
|
|
static FixedArray<T> must_create_but_fixme_should_propagate_errors(size_t size)
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2023-01-28 17:12:17 -03:00
|
|
|
return MUST(create(size));
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-10 11:30:52 -03:00
|
|
|
template<size_t N>
|
2023-01-28 17:12:17 -03:00
|
|
|
static ErrorOr<FixedArray<T>> create(T (&&array)[N])
|
2022-01-10 11:30:52 -03:00
|
|
|
{
|
2023-01-28 17:12:17 -03:00
|
|
|
return create(Span(array, N));
|
2022-01-10 11:30:52 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 12:39:31 -03:00
|
|
|
template<typename U>
|
2023-01-28 17:12:17 -03:00
|
|
|
static ErrorOr<FixedArray<T>> create(Span<U> span)
|
2022-01-12 12:39:31 -03:00
|
|
|
{
|
|
|
|
|
if (span.size() == 0)
|
|
|
|
|
return FixedArray<T>();
|
2024-05-18 10:56:04 -03:00
|
|
|
auto* elements = reinterpret_cast<T*>(kmalloc(storage_allocation_size(span.size())));
|
|
|
|
|
if (!elements)
|
2022-01-12 12:39:31 -03:00
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
for (size_t i = 0; i < span.size(); ++i)
|
2024-05-18 10:56:04 -03:00
|
|
|
new (&elements[i]) T(span[i]);
|
|
|
|
|
return FixedArray<T>(span.size(), elements);
|
2022-01-12 12:39:31 -03:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 17:12:17 -03:00
|
|
|
ErrorOr<FixedArray<T>> clone() const
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2023-01-28 17:12:17 -03:00
|
|
|
return create(span());
|
2022-08-26 10:03:46 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-13 06:50:31 -03:00
|
|
|
// Nobody can ever use these functions, since it would be impossible to make them OOM-safe due to their signatures. We just explicitly delete them.
|
2022-01-08 16:06:03 -03:00
|
|
|
FixedArray(FixedArray<T> const&) = delete;
|
|
|
|
|
FixedArray<T>& operator=(FixedArray<T> const&) = delete;
|
|
|
|
|
|
|
|
|
|
FixedArray(FixedArray<T>&& other)
|
2024-05-18 10:56:04 -03:00
|
|
|
: m_size(exchange(other.m_size, 0))
|
|
|
|
|
, m_elements(exchange(other.m_elements, nullptr))
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
|
|
|
|
}
|
2023-06-20 14:25:48 -03:00
|
|
|
|
|
|
|
|
FixedArray<T>& operator=(FixedArray<T>&& other)
|
|
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
if (this != &other) {
|
|
|
|
|
this->~FixedArray();
|
|
|
|
|
new (this) FixedArray<T>(move(other));
|
|
|
|
|
}
|
2023-06-20 14:25:48 -03:00
|
|
|
return *this;
|
|
|
|
|
}
|
2021-07-11 12:16:13 -03:00
|
|
|
|
2022-01-08 16:06:03 -03:00
|
|
|
~FixedArray()
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
if (!m_elements)
|
2021-07-11 12:16:13 -03:00
|
|
|
return;
|
2024-05-18 10:56:04 -03:00
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
|
|
|
m_elements[i].~T();
|
AK: Adopt mimalloc v2 as main allocator
Use mimalloc for Ladybird-owned allocations without overriding malloc().
Route kmalloc(), kcalloc(), krealloc(), and kfree() through mimalloc,
and put the embedded Rust crates on the same allocator via a shared
shim in AK/kmalloc.cpp.
This also lets us drop kfree_sized(), since it no longer used its size
argument. StringData, Utf16StringData, JS object storage, Rust error
strings, and the CoreAudio playback helpers can all free their AK-backed
storage with plain kfree().
Sanitizer builds still use the system allocator. LeakSanitizer does not
reliably trace references stored in mimalloc-managed AK containers, so
static caches and other long-lived roots can look leaked. Pass the old
size into the Rust realloc shim so aligned fallback reallocations can
move posix_memalign-backed blocks safely.
Static builds still need a little linker help. macOS app binaries need
the Rust allocator entry points forced in from liblagom-ak.a, while
static ELF links can pull in identical allocator shim definitions from
multiple Rust staticlibs. Keep the Apple -u flags and allow those
duplicate shim symbols for LibJS and LibRegex links on Linux and BSD.
2026-04-03 12:06:58 -03:00
|
|
|
kfree(m_elements);
|
2024-05-18 10:56:04 -03:00
|
|
|
m_elements = nullptr;
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
|
|
|
|
|
2024-05-18 10:56:04 -03:00
|
|
|
size_t size() const { return m_size; }
|
2022-08-26 10:03:46 -03:00
|
|
|
bool is_empty() const { return size() == 0; }
|
2024-05-18 10:56:04 -03:00
|
|
|
T* data() { return m_elements; }
|
|
|
|
|
T const* data() const { return m_elements; }
|
2021-07-11 12:16:13 -03:00
|
|
|
|
2022-02-23 07:32:28 -03:00
|
|
|
T& at(size_t index)
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
VERIFY(index < m_size);
|
|
|
|
|
return m_elements[index];
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-30 21:27:41 -03:00
|
|
|
T& unchecked_at(size_t index)
|
|
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
return m_elements[index];
|
2023-04-30 21:27:41 -03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 07:32:28 -03:00
|
|
|
T const& at(size_t index) const
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
VERIFY(index < m_size);
|
|
|
|
|
return m_elements[index];
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 07:32:28 -03:00
|
|
|
T& operator[](size_t index)
|
|
|
|
|
{
|
|
|
|
|
return at(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T const& operator[](size_t index) const
|
|
|
|
|
{
|
|
|
|
|
return at(index);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 12:16:13 -03:00
|
|
|
bool contains_slow(T const& value) const
|
|
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
if (!m_elements)
|
2022-08-26 10:03:46 -03:00
|
|
|
return false;
|
2024-05-18 10:56:04 -03:00
|
|
|
for (size_t i = 0; i < m_size; ++i) {
|
|
|
|
|
if (m_elements[i] == value)
|
2021-07-11 12:16:13 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 16:06:03 -03:00
|
|
|
void swap(FixedArray<T>& other)
|
2021-07-11 12:16:13 -03:00
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
AK::swap(m_size, other.m_size);
|
|
|
|
|
AK::swap(m_elements, other.m_elements);
|
2021-07-11 12:16:13 -03:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 18:30:44 -03:00
|
|
|
void fill_with(T const& value)
|
|
|
|
|
{
|
2024-05-18 10:56:04 -03:00
|
|
|
if (!m_elements)
|
2022-08-26 10:03:46 -03:00
|
|
|
return;
|
2024-05-18 10:56:04 -03:00
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
|
|
|
|
m_elements[i] = value;
|
2022-06-27 18:30:44 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-11 12:16:13 -03:00
|
|
|
using Iterator = SimpleIterator<FixedArray, T>;
|
2022-01-08 16:50:34 -03:00
|
|
|
using ConstIterator = SimpleIterator<FixedArray const, T const>;
|
2021-07-11 12:16:13 -03:00
|
|
|
|
|
|
|
|
Iterator begin() { return Iterator::begin(*this); }
|
2022-01-08 16:50:34 -03:00
|
|
|
ConstIterator begin() const { return ConstIterator::begin(*this); }
|
2021-07-11 12:16:13 -03:00
|
|
|
|
|
|
|
|
Iterator end() { return Iterator::end(*this); }
|
2022-01-08 16:50:34 -03:00
|
|
|
ConstIterator end() const { return ConstIterator::end(*this); }
|
2021-07-11 12:16:13 -03:00
|
|
|
|
2021-07-11 12:38:13 -03:00
|
|
|
Span<T> span() { return { data(), size() }; }
|
2023-02-05 16:02:54 -03:00
|
|
|
ReadonlySpan<T> span() const { return { data(), size() }; }
|
2021-07-11 12:38:13 -03:00
|
|
|
|
2021-07-11 12:16:13 -03:00
|
|
|
private:
|
2024-05-18 10:56:04 -03:00
|
|
|
static size_t storage_allocation_size(size_t size)
|
|
|
|
|
{
|
|
|
|
|
return size * sizeof(T);
|
|
|
|
|
}
|
2022-08-26 10:03:46 -03:00
|
|
|
|
2024-05-18 10:56:04 -03:00
|
|
|
FixedArray(size_t size, T* elements)
|
|
|
|
|
: m_size(size)
|
|
|
|
|
, m_elements(elements)
|
2022-01-08 16:06:03 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 10:56:04 -03:00
|
|
|
size_t m_size { 0 };
|
|
|
|
|
T* m_elements { nullptr };
|
2021-07-11 12:16:13 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 08:18:30 -03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-07-11 12:16:13 -03:00
|
|
|
using AK::FixedArray;
|
2022-11-26 08:18:30 -03:00
|
|
|
#endif
|