1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

AK: Make MappedFile heap-allocated and ref-counted

Let's adapt this class a bit better to how it's actually being used.

Instead of having valid/invalid states and storing an error in case
it's invalid, a MappedFile is now always valid, and the factory
function that creates it will return an OSError if mapping fails.
This commit is contained in:
Andreas Kling 2021-01-10 15:55:54 +01:00
parent 70fce5c4c7
commit 2f3b901f7f
36 changed files with 184 additions and 199 deletions

View file

@ -179,10 +179,10 @@ static RefPtr<Bitmap> load_bmp_impl(const u8*, size_t);
RefPtr<Gfx::Bitmap> load_bmp(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto bitmap = load_bmp_impl((const u8*)mapped_file.data(), mapped_file.size());
auto bitmap = load_bmp_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;

View file

@ -27,7 +27,6 @@
#include "BitmapFont.h"
#include "Bitmap.h"
#include "Emoji.h"
#include <AK/MappedFile.h>
#include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h>
#include <AK/Utf32View.h>
@ -174,15 +173,15 @@ size_t BitmapFont::glyph_count_by_type(FontTypes type)
RefPtr<BitmapFont> BitmapFont::load_from_file(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto font = load_from_memory((const u8*)mapped_file.data());
auto font = load_from_memory((const u8*)file_or_error.value()->data());
if (!font)
return nullptr;
font->m_mapped_file = move(mapped_file);
font->m_mapped_file = file_or_error.release_value();
return font;
}

View file

@ -128,7 +128,7 @@ private:
unsigned* m_rows { nullptr };
u8* m_glyph_widths { nullptr };
MappedFile m_mapped_file;
RefPtr<MappedFile> m_mapped_file;
u8 m_glyph_width { 0 };
u8 m_glyph_height { 0 };

View file

@ -107,10 +107,10 @@ struct GIFLoadingContext {
RefPtr<Gfx::Bitmap> load_gif(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
GIFImageDecoderPlugin gif_decoder((const u8*)mapped_file.data(), mapped_file.size());
GIFImageDecoderPlugin gif_decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
auto bitmap = gif_decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));

View file

@ -116,10 +116,10 @@ struct ICOLoadingContext {
RefPtr<Gfx::Bitmap> load_ico(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
ICOImageDecoderPlugin decoder((const u8*)mapped_file.data(), mapped_file.size());
ICOImageDecoderPlugin decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
auto bitmap = decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));

View file

@ -1320,12 +1320,10 @@ static RefPtr<Gfx::Bitmap> load_jpg_impl(const u8* data, size_t data_size)
RefPtr<Gfx::Bitmap> load_jpg(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid()) {
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
}
auto bitmap = load_jpg_impl((const u8*)mapped_file.data(), mapped_file.size());
auto bitmap = load_jpg_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded JPG: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;

View file

@ -193,10 +193,10 @@ static bool process_chunk(Streamer&, PNGLoadingContext& context);
RefPtr<Gfx::Bitmap> load_png(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto bitmap = load_png_impl((const u8*)mapped_file.data(), mapped_file.size());
auto bitmap = load_png_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;

View file

@ -294,12 +294,10 @@ static RefPtr<Gfx::Bitmap> load_impl(const u8* data, size_t data_size)
template<typename TContext>
static RefPtr<Gfx::Bitmap> load(const StringView& path)
{
MappedFile mapped_file(path);
if (!mapped_file.is_valid()) {
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
}
auto bitmap = load_impl<TContext>((const u8*)mapped_file.data(), mapped_file.size());
auto bitmap = load_impl<TContext>((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}",
bitmap->size(),