Skip to content

Commit

Permalink
Kernel: Add KBuffer::try_create_with_bytes()
Browse files Browse the repository at this point in the history
Here's another fallible KBuffer construction API that creates a KBuffer
and populates it with a range of bytes.
  • Loading branch information
awesomekling committed Dec 18, 2020
1 parent bcd2844 commit d936d86
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Kernel/KBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class KBufferImpl : public RefCounted<KBufferImpl> {
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);
Expand Down Expand Up @@ -106,6 +116,14 @@ class KBuffer {
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));
Expand Down

0 comments on commit d936d86

Please sign in to comment.