summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-10 18:53:26 +0200
committerIdan Horowitz <idan.horowitz@gmail.com>2022-02-11 17:49:46 +0200
commit871a53db760a975a6aa3510b1a5b355152eaa2ef (patch)
tree3983ff3aca3816ceff3fafae2bf8a94ed07a78fc /AK
parent1667a80ade44d1751d3c4e664c9aab825848465e (diff)
downloadserenity-871a53db760a975a6aa3510b1a5b355152eaa2ef.zip
AK: Make Bitmap construction OOM-fallible
Diffstat (limited to 'AK')
-rw-r--r--AK/Bitmap.h24
1 files changed, 18 insertions, 6 deletions
diff --git a/AK/Bitmap.h b/AK/Bitmap.h
index 2aeb381910..93a14dc1c3 100644
--- a/AK/Bitmap.h
+++ b/AK/Bitmap.h
@@ -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)