mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:57:45 +00:00
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:
parent
0362b15895
commit
64778f9e69
1 changed files with 13 additions and 0 deletions
|
@ -55,6 +55,19 @@ public:
|
||||||
return FixedArray<T>(N, elements);
|
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
|
ErrorOr<FixedArray<T>> try_clone() const
|
||||||
{
|
{
|
||||||
if (m_size == 0)
|
if (m_size == 0)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue