Kernel: Add KBuffer::try_create_with_bytes()

Here's another fallible KBuffer construction API that creates a KBuffer
and populates it with a range of bytes.
This commit is contained in:
Andreas Kling 2020-12-18 14:09:14 +01:00
parent bcd2844439
commit d936d86332
Notes: sideshowbarker 2024-07-19 00:45:13 +09:00

View File

@ -56,6 +56,16 @@ public:
return adopt(*new KBufferImpl(region.release_nonnull(), size));
}
static RefPtr<KBufferImpl> try_create_with_bytes(ReadonlyBytes bytes, u8 access, const char* name)
{
auto region = MM.allocate_kernel_region(PAGE_ROUND_UP(bytes.size()), name, access, false, false);
if (!region)
return nullptr;
if (!region->commit())
return nullptr;
return adopt(*new KBufferImpl(region.release_nonnull(), bytes.size()));
}
static NonnullRefPtr<KBufferImpl> create_with_size(size_t size, u8 access, const char* name)
{
auto impl = try_create_with_size(size, access, name);
@ -106,6 +116,14 @@ public:
return adopt_own(*new KBuffer(impl.release_nonnull()));
}
static OwnPtr<KBuffer> try_create_with_bytes(ReadonlyBytes bytes, u8 access = Region::Access::Read | Region::Access::Write, const char* name = "KBuffer")
{
auto impl = KBufferImpl::try_create_with_bytes(bytes, 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));