1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

AK: Make AK::Vector expose allocation failures in API

This commit is contained in:
Sahan Fernando 2021-04-24 16:51:31 +10:00 committed by Andreas Kling
parent e6401d65bd
commit c3cf739b94

View file

@ -259,12 +259,14 @@ public:
} }
template<typename U = T> template<typename U = T>
void insert(size_t index, U&& value) [[nodiscard]] bool try_insert(size_t index, U&& value)
{ {
VERIFY(index <= size()); if (index > size())
return false;
if (index == size()) if (index == size())
return append(forward<U>(value)); return try_append(forward<U>(value));
grow_capacity(size() + 1); if (!try_grow_capacity(size() + 1))
return false;
++m_size; ++m_size;
if constexpr (Traits<T>::is_trivial()) { if constexpr (Traits<T>::is_trivial()) {
TypedTransfer<T>::move(slot(index + 1), slot(index), m_size - index - 1); TypedTransfer<T>::move(slot(index + 1), slot(index), m_size - index - 1);
@ -275,22 +277,40 @@ public:
} }
} }
new (slot(index)) T(forward<U>(value)); new (slot(index)) T(forward<U>(value));
return true;
}
template<typename U = T>
void insert(size_t index, U&& value)
{
auto did_allocate = try_insert<U>(index, forward<U>(value));
VERIFY(did_allocate);
}
template<typename C, typename U = T>
[[nodiscard]] bool try_insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
{
for (size_t i = first_index; i < size(); ++i) {
if (callback(at(i))) {
if (!try_insert(i, forward<U>(value)))
return false;
if (inserted_index)
*inserted_index = i;
return true;
}
}
if (!try_append(forward<U>(value)))
return false;
if (inserted_index)
*inserted_index = size() - 1;
return true;
} }
template<typename C, typename U = T> template<typename C, typename U = T>
void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr) void insert_before_matching(U&& value, C callback, size_t first_index = 0, size_t* inserted_index = nullptr)
{ {
for (size_t i = first_index; i < size(); ++i) { auto did_allocate = try_insert_before_matching(forward<U>(value), callback, first_index, inserted_index);
if (callback(at(i))) { VERIFY(did_allocate);
insert(i, forward<U>(value));
if (inserted_index)
*inserted_index = i;
return;
}
}
append(forward<U>(value));
if (inserted_index)
*inserted_index = size() - 1;
} }
Vector& operator=(const Vector& other) Vector& operator=(const Vector& other)
@ -314,24 +334,40 @@ public:
return *this; return *this;
} }
void append(Vector&& other) [[nodiscard]] bool try_append(Vector&& other)
{ {
if (is_empty()) { if (is_empty()) {
*this = move(other); *this = move(other);
return; return true;
} }
auto other_size = other.size(); auto other_size = other.size();
Vector tmp = move(other); Vector tmp = move(other);
grow_capacity(size() + other_size); if (!try_grow_capacity(size() + other_size))
return false;
TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size); TypedTransfer<T>::move(data() + m_size, tmp.data(), other_size);
m_size += other_size; m_size += other_size;
return true;
}
void append(Vector&& other)
{
auto did_allocate = try_append(move(other));
VERIFY(did_allocate);
}
[[nodiscard]] bool try_append(const Vector& other)
{
if (!try_grow_capacity(size() + other.size()))
return false;
TypedTransfer<T>::copy(data() + m_size, other.data(), other.size());
m_size += other.m_size;
return true;
} }
void append(const Vector& other) void append(const Vector& other)
{ {
grow_capacity(size() + other.size()); auto did_allocate = try_append(other);
TypedTransfer<T>::copy(data() + m_size, other.data(), other.size()); VERIFY(did_allocate);
m_size += other.m_size;
} }
template<typename Callback> template<typename Callback>
@ -389,43 +425,74 @@ public:
} }
template<class... Args> template<class... Args>
void empend(Args&&... args) [[nodiscard]] bool try_empend(Args&&... args)
{ {
grow_capacity(m_size + 1); if (!try_grow_capacity(m_size + 1))
return false;
new (slot(m_size)) T { forward<Args>(args)... }; new (slot(m_size)) T { forward<Args>(args)... };
++m_size; ++m_size;
return true;
}
template<class... Args>
void empend(Args&&... args)
{
auto did_allocate = try_empend(forward<Args>(args)...);
VERIFY(did_allocate);
}
[[nodiscard]] ALWAYS_INLINE bool try_append(T&& value)
{
if (!try_grow_capacity(size() + 1))
return false;
new (slot(m_size)) T(move(value));
++m_size;
return true;
} }
ALWAYS_INLINE void append(T&& value) ALWAYS_INLINE void append(T&& value)
{ {
grow_capacity(size() + 1); auto did_allocate = try_append(move(value));
new (slot(m_size)) T(move(value)); VERIFY(did_allocate);
++m_size; }
[[nodiscard]] ALWAYS_INLINE bool try_append(const T& value)
{
return try_append(T(value));
} }
ALWAYS_INLINE void append(const T& value) ALWAYS_INLINE void append(const T& value)
{ {
append(T(value)); auto did_allocate = try_append(T(value));
VERIFY(did_allocate);
}
template<typename U = T>
[[nodiscard]] bool try_prepend(U&& value)
{
return try_insert(0, forward<U>(value));
} }
template<typename U = T> template<typename U = T>
void prepend(U&& value) void prepend(U&& value)
{ {
insert(0, forward<U>(value)); auto did_allocate = try_insert(0, forward<U>(value));
VERIFY(did_allocate);
} }
void prepend(Vector&& other) [[nodiscard]] bool try_prepend(Vector&& other)
{ {
if (other.is_empty()) if (other.is_empty())
return; return true;
if (is_empty()) { if (is_empty()) {
*this = move(other); *this = move(other);
return; return true;
} }
auto other_size = other.size(); auto other_size = other.size();
grow_capacity(size() + other_size); if (!try_grow_capacity(size() + other_size))
return false;
for (size_t i = size() + other_size - 1; i >= other.size(); --i) { for (size_t i = size() + other_size - 1; i >= other.size(); --i) {
new (slot(i)) T(move(at(i - other_size))); new (slot(i)) T(move(at(i - other_size)));
@ -435,40 +502,71 @@ public:
Vector tmp = move(other); Vector tmp = move(other);
TypedTransfer<T>::move(slot(0), tmp.data(), tmp.size()); TypedTransfer<T>::move(slot(0), tmp.data(), tmp.size());
m_size += other_size; m_size += other_size;
return true;
}
void prepend(Vector&& other)
{
auto did_allocate = try_prepend(move(other));
VERIFY(did_allocate);
}
[[nodiscard]] bool try_prepend(const T* values, size_t count)
{
if (!count)
return true;
if (!try_grow_capacity(size() + count))
return false;
TypedTransfer<T>::move(slot(count), slot(0), m_size);
TypedTransfer<T>::copy(slot(0), values, count);
m_size += count;
return true;
} }
void prepend(const T* values, size_t count) void prepend(const T* values, size_t count)
{
auto did_allocate = try_prepend(values, count);
VERIFY(did_allocate);
}
[[nodiscard]] bool try_append(const T* values, size_t count)
{ {
if (!count) if (!count)
return; return true;
grow_capacity(size() + count); if (!try_grow_capacity(size() + count))
TypedTransfer<T>::move(slot(count), slot(0), m_size); return false;
TypedTransfer<T>::copy(slot(0), values, count); TypedTransfer<T>::copy(slot(m_size), values, count);
m_size += count; m_size += count;
return true;
} }
void append(const T* values, size_t count) void append(const T* values, size_t count)
{ {
if (!count) auto did_allocate = try_append(values, count);
return; VERIFY(did_allocate);
grow_capacity(size() + count); }
TypedTransfer<T>::copy(slot(m_size), values, count);
m_size += count; [[nodiscard]] bool try_grow_capacity(size_t needed_capacity)
{
if (m_capacity >= needed_capacity)
return true;
return try_ensure_capacity(padded_capacity(needed_capacity));
} }
void grow_capacity(size_t needed_capacity) void grow_capacity(size_t needed_capacity)
{ {
if (m_capacity >= needed_capacity) auto did_allocate = try_grow_capacity(needed_capacity);
return; VERIFY(did_allocate);
ensure_capacity(padded_capacity(needed_capacity));
} }
void ensure_capacity(size_t needed_capacity) [[nodiscard]] bool try_ensure_capacity(size_t needed_capacity)
{ {
if (m_capacity >= needed_capacity) if (m_capacity >= needed_capacity)
return; return true;
size_t new_capacity = needed_capacity; size_t new_capacity = needed_capacity;
auto* new_buffer = (T*)kmalloc(new_capacity * sizeof(T)); auto* new_buffer = (T*)kmalloc(new_capacity * sizeof(T));
if (new_buffer == nullptr)
return false;
if constexpr (Traits<T>::is_trivial()) { if constexpr (Traits<T>::is_trivial()) {
TypedTransfer<T>::copy(new_buffer, data(), m_size); TypedTransfer<T>::copy(new_buffer, data(), m_size);
@ -482,6 +580,13 @@ public:
kfree(m_outline_buffer); kfree(m_outline_buffer);
m_outline_buffer = new_buffer; m_outline_buffer = new_buffer;
m_capacity = new_capacity; m_capacity = new_capacity;
return true;
}
void ensure_capacity(size_t needed_capacity)
{
auto did_allocate = try_ensure_capacity(needed_capacity);
VERIFY(did_allocate);
} }
void shrink(size_t new_size, bool keep_capacity = false) void shrink(size_t new_size, bool keep_capacity = false)
@ -503,20 +608,37 @@ public:
m_size = new_size; m_size = new_size;
} }
void resize(size_t new_size, bool keep_capacity = false) [[nodiscard]] bool try_resize(size_t new_size, bool keep_capacity = false)
{ {
if (new_size <= size()) if (new_size <= size()) {
return shrink(new_size, keep_capacity); shrink(new_size, keep_capacity);
return true;
}
if (!try_ensure_capacity(new_size))
return false;
ensure_capacity(new_size);
for (size_t i = size(); i < new_size; ++i) for (size_t i = size(); i < new_size; ++i)
new (slot(i)) T; new (slot(i)) T;
m_size = new_size; m_size = new_size;
return true;
}
void resize(size_t new_size, bool keep_capacity = false)
{
auto did_allocate = try_resize(new_size, keep_capacity);
VERIFY(did_allocate);
}
[[nodiscard]] bool try_resize_and_keep_capacity(size_t new_size)
{
return try_resize(new_size, true);
} }
void resize_and_keep_capacity(size_t new_size) void resize_and_keep_capacity(size_t new_size)
{ {
return resize(new_size, true); auto did_allocate = try_resize_and_keep_capacity(new_size);
VERIFY(did_allocate);
} }
using ConstIterator = SimpleIterator<const Vector, const T>; using ConstIterator = SimpleIterator<const Vector, const T>;