diff options
author | Andreas Kling <kling@serenityos.org> | 2020-12-18 14:09:14 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-18 19:22:26 +0100 |
commit | d936d86332241221151cc765404f1b383ac0fd0e (patch) | |
tree | a0b2393be3c1f797cc8cf081f5f283312f2b1e1c | |
parent | bcd2844439948e89c55068e953c1ce463a0fb865 (diff) | |
download | serenity-d936d86332241221151cc765404f1b383ac0fd0e.zip |
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.
-rw-r--r-- | Kernel/KBuffer.h | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index b57ca20581..ade540b5fd 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -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)); |