mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 12:27:35 +00:00
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> KBuffer::create_with_size(size_t); All KBuffer clients should move towards using this (and handling any failures with grace.)
This commit is contained in:
parent
5d1425718e
commit
8cde8ba511
1 changed files with 18 additions and 2 deletions
|
@ -48,13 +48,21 @@ namespace Kernel {
|
||||||
|
|
||||||
class KBufferImpl : public RefCounted<KBufferImpl> {
|
class KBufferImpl : public RefCounted<KBufferImpl> {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<KBufferImpl> create_with_size(size_t size, u8 access, const char* name)
|
static RefPtr<KBufferImpl> 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);
|
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));
|
return adopt(*new KBufferImpl(region.release_nonnull(), size));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NonnullRefPtr<KBufferImpl> 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<KBufferImpl> copy(const void* data, size_t size, u8 access, const char* name)
|
static NonnullRefPtr<KBufferImpl> copy(const void* data, size_t size, u8 access, const char* name)
|
||||||
{
|
{
|
||||||
auto buffer = create_with_size(size, access, name);
|
auto buffer = create_with_size(size, access, name);
|
||||||
|
@ -90,6 +98,14 @@ private:
|
||||||
|
|
||||||
class KBuffer {
|
class KBuffer {
|
||||||
public:
|
public:
|
||||||
|
static OwnPtr<KBuffer> 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")
|
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));
|
return KBuffer(KBufferImpl::create_with_size(size, access, name));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue