AK: Add a constructor from Span for FixedArray

This is particularly useful in the Kernel, where the physical pages of
a VMObject are stored as a FixedArray but often passed around as a Span
from which a new FixedArray should be cloned.
This commit is contained in:
creator1creeper1 2022-01-12 16:39:31 +01:00 committed by Idan Horowitz
parent 0362b15895
commit 64778f9e69

View file

@ -55,6 +55,19 @@ public:
return FixedArray<T>(N, elements);
}
template<typename U>
static ErrorOr<FixedArray<T>> try_create(Span<U> span)
{
if (span.size() == 0)
return FixedArray<T>();
T* elements = static_cast<T*>(kmalloc_array(span.size(), sizeof(T)));
if (!elements)
return Error::from_errno(ENOMEM);
for (size_t i = 0; i < span.size(); ++i)
new (&elements[i]) T(span[i]);
return FixedArray<T>(span.size(), elements);
}
ErrorOr<FixedArray<T>> try_clone() const
{
if (m_size == 0)