From 8cde8ba511eaa0678b23cb7fdad25091bd219030 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Dec 2020 13:14:59 +0100 Subject: [PATCH] Kernel: Add KBuffer::try_create_with_size() We need to stop assuming that KBuffer allocation always succeeds. This patch adds the following API: - static OwnPtr KBuffer::create_with_size(size_t); All KBuffer clients should move towards using this (and handling any failures with grace.) --- Kernel/KBuffer.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index b6369f1839..b57ca20581 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -48,13 +48,21 @@ namespace Kernel { class KBufferImpl : public RefCounted { public: - static NonnullRefPtr create_with_size(size_t size, u8 access, const char* name) + static RefPtr try_create_with_size(size_t size, u8 access, const char* name) { auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(size), name, access, false, false); - ASSERT(region); + if (!region) + return nullptr; return adopt(*new KBufferImpl(region.release_nonnull(), size)); } + static NonnullRefPtr create_with_size(size_t size, u8 access, const char* name) + { + auto impl = try_create_with_size(size, access, name); + ASSERT(impl); + return impl.release_nonnull(); + } + static NonnullRefPtr copy(const void* data, size_t size, u8 access, const char* name) { auto buffer = create_with_size(size, access, name); @@ -90,6 +98,14 @@ private: class KBuffer { public: + static OwnPtr try_create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") + { + auto impl = KBufferImpl::try_create_with_size(size, access, name); + if (!impl) + return nullptr; + return adopt_own(*new KBuffer(impl.release_nonnull())); + } + static KBuffer create_with_size(size_t size, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer") { return KBuffer(KBufferImpl::create_with_size(size, access, name));