mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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
Notes:
sideshowbarker
2024-07-17 20:50:25 +09:00
Author: https://github.com/creator1creeper1 Commit: https://github.com/SerenityOS/serenity/commit/64778f9e69b Pull-request: https://github.com/SerenityOS/serenity/pull/11843 Reviewed-by: https://github.com/IdanHo ✅ Reviewed-by: https://github.com/bgianfo
1 changed files with 13 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue