LibGfx: Use ErrorOr<T> for Gfx::Bitmap::allocate_backing_store()

This commit is contained in:
Andreas Kling 2021-11-06 10:15:13 +01:00
parent 56992f90b7
commit af562c857e
Notes: sideshowbarker 2024-07-18 01:25:08 +09:00
2 changed files with 9 additions and 11 deletions

View file

@ -67,10 +67,10 @@ static bool size_would_overflow(BitmapFormat format, const IntSize& size, int sc
RefPtr<Bitmap> Bitmap::try_create(BitmapFormat format, const IntSize& size, int scale_factor)
{
auto backing_store = Bitmap::try_allocate_backing_store(format, size, scale_factor);
if (!backing_store.has_value())
auto backing_store_or_error = Bitmap::allocate_backing_store(format, size, scale_factor);
if (backing_store_or_error.is_error())
return nullptr;
return adopt_ref(*new Bitmap(format, size, scale_factor, backing_store.value()));
return adopt_ref(*new Bitmap(format, size, scale_factor, backing_store_or_error.release_value()));
}
RefPtr<Bitmap> Bitmap::try_create_shareable(BitmapFormat format, const IntSize& size, int scale_factor)
@ -607,10 +607,10 @@ ShareableBitmap Bitmap::to_shareable_bitmap() const
return ShareableBitmap(*bitmap);
}
Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)
ErrorOr<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor)
{
if (size_would_overflow(format, size, scale_factor))
return {};
return AK::Error::from_string_literal("Gfx::Bitmap backing store size overflow"sv);
const auto pitch = minimum_pitch(size.width() * scale_factor, format);
const auto data_size_in_bytes = size_in_bytes(pitch, size.height() * scale_factor);
@ -622,11 +622,9 @@ Optional<BackingStore> Bitmap::try_allocate_backing_store(BitmapFormat format, I
#else
void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
#endif
if (data == MAP_FAILED) {
perror("mmap");
return {};
}
return { { data, pitch, data_size_in_bytes } };
if (data == MAP_FAILED)
return AK::Error::from_errno(errno);
return BackingStore { data, pitch, data_size_in_bytes };
}
void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)

View file

@ -241,7 +241,7 @@ private:
Bitmap(BitmapFormat, const IntSize&, int, size_t pitch, void*);
Bitmap(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int, const Vector<RGBA32>& palette);
static Optional<BackingStore> try_allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor);
static ErrorOr<BackingStore> allocate_backing_store(BitmapFormat format, IntSize const& size, int scale_factor);
void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette);