1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-09 09:34:57 +09:00

AK: Make Bitmap construction OOM-fallible

This commit is contained in:
Idan Horowitz 2022-02-10 18:53:26 +02:00
parent 1667a80ade
commit 871a53db76
Notes: sideshowbarker 2024-07-18 02:13:10 +09:00
6 changed files with 44 additions and 32 deletions

View file

@ -7,10 +7,12 @@
#pragma once
#include <AK/BitmapView.h>
#include <AK/Error.h>
#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <AK/Platform.h>
#include <AK/StdLibExtras.h>
#include <AK/Try.h>
#include <AK/Types.h>
#include <AK/kmalloc.h>
@ -20,16 +22,26 @@ class Bitmap : public BitmapView {
AK_MAKE_NONCOPYABLE(Bitmap);
public:
Bitmap() = default;
Bitmap(size_t size, bool default_value)
: BitmapView(static_cast<u8*>(kmalloc(ceil_div(size, static_cast<size_t>(8)))), size)
, m_is_owning(true)
static ErrorOr<Bitmap> try_create(size_t size, bool default_value)
{
VERIFY(size != 0);
fill(default_value);
auto* data = kmalloc(ceil_div(size, static_cast<size_t>(8)));
if (!data)
return Error::from_errno(ENOMEM);
auto bitmap = Bitmap { (u8*)data, size, true };
bitmap.fill(default_value);
return bitmap;
}
static Bitmap must_create(size_t size, bool default_value)
{
return MUST(try_create(size, default_value));
}
Bitmap() = default;
Bitmap(u8* data, size_t size, bool is_owning = false)
: BitmapView(data, size)
, m_is_owning(is_owning)