From 64778f9e69bbdc40f62ba10e78c635147de31014 Mon Sep 17 00:00:00 2001 From: creator1creeper1 Date: Wed, 12 Jan 2022 16:39:31 +0100 Subject: [PATCH] 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. --- AK/FixedArray.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/AK/FixedArray.h b/AK/FixedArray.h index 8f0d1a9b418..92593e5ffaf 100644 --- a/AK/FixedArray.h +++ b/AK/FixedArray.h @@ -55,6 +55,19 @@ public: return FixedArray(N, elements); } + template + static ErrorOr> try_create(Span span) + { + if (span.size() == 0) + return FixedArray(); + T* elements = static_cast(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(span.size(), elements); + } + ErrorOr> try_clone() const { if (m_size == 0)