diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-10 15:55:54 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-10 16:49:13 +0100 |
commit | 2f3b901f7fe230cbbf489f5a0f38fbb34a07af8c (patch) | |
tree | ceff5568f544c8bc2ef10857a914c17044863339 /Libraries | |
parent | 70fce5c4c7279c045e593a8a3a6f79ebc28e2972 (diff) | |
download | serenity-2f3b901f7fe230cbbf489f5a0f38fbb34a07af8c.zip |
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.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibCoreDump/Backtrace.cpp | 7 | ||||
-rw-r--r-- | Libraries/LibCoreDump/Backtrace.h | 4 | ||||
-rw-r--r-- | Libraries/LibCoreDump/Reader.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibCoreDump/Reader.h | 9 | ||||
-rw-r--r-- | Libraries/LibDebug/DebugSession.cpp | 15 | ||||
-rw-r--r-- | Libraries/LibDebug/DebugSession.h | 6 | ||||
-rw-r--r-- | Libraries/LibELF/Image.cpp | 11 | ||||
-rw-r--r-- | Libraries/LibELF/Image.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/FileIconProvider.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGUI/ImageWidget.cpp | 5 | ||||
-rw-r--r-- | Libraries/LibGfx/BMPLoader.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGfx/BitmapFont.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibGfx/BitmapFont.h | 2 | ||||
-rw-r--r-- | Libraries/LibGfx/GIFLoader.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGfx/ICOLoader.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGfx/JPGLoader.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibGfx/PNGLoader.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGfx/PortableImageLoaderCommon.h | 8 | ||||
-rw-r--r-- | Libraries/LibPCIDB/Database.cpp | 10 | ||||
-rw-r--r-- | Libraries/LibPCIDB/Database.h | 8 |
20 files changed, 76 insertions, 76 deletions
diff --git a/Libraries/LibCoreDump/Backtrace.cpp b/Libraries/LibCoreDump/Backtrace.cpp index 62a7ff9ce1..afa6220447 100644 --- a/Libraries/LibCoreDump/Backtrace.cpp +++ b/Libraries/LibCoreDump/Backtrace.cpp @@ -57,11 +57,12 @@ static const ELFObjectInfo* object_info_for_region(const ELF::Core::MemoryRegion if (!Core::File::exists(path.characters())) return nullptr; - MappedFile object_file(path); - if (!object_file.is_valid()) + auto file_or_error = MappedFile::map(path); + if (file_or_error.is_error()) return nullptr; - auto info = make<ELFObjectInfo>(move(object_file), Debug::DebugInfo { make<ELF::Image>((const u8*)object_file.data(), object_file.size()) }); + auto image = make<ELF::Image>(file_or_error.value()->bytes()); + auto info = make<ELFObjectInfo>(file_or_error.release_value(), Debug::DebugInfo { move(image) }); auto* info_ptr = info.ptr(); s_debug_info_cache.set(path, move(info)); return info_ptr; diff --git a/Libraries/LibCoreDump/Backtrace.h b/Libraries/LibCoreDump/Backtrace.h index b2ded64414..6915b170c2 100644 --- a/Libraries/LibCoreDump/Backtrace.h +++ b/Libraries/LibCoreDump/Backtrace.h @@ -33,13 +33,13 @@ namespace CoreDump { struct ELFObjectInfo { - ELFObjectInfo(MappedFile&& file, Debug::DebugInfo&& debug_info) + ELFObjectInfo(NonnullRefPtr<MappedFile> file, Debug::DebugInfo&& debug_info) : file(move(file)) , debug_info(move(debug_info)) { } - MappedFile file; + NonnullRefPtr<MappedFile> file; Debug::DebugInfo debug_info; }; diff --git a/Libraries/LibCoreDump/Reader.cpp b/Libraries/LibCoreDump/Reader.cpp index 6646faee48..da7eeabe85 100644 --- a/Libraries/LibCoreDump/Reader.cpp +++ b/Libraries/LibCoreDump/Reader.cpp @@ -35,15 +35,15 @@ namespace CoreDump { OwnPtr<Reader> Reader::create(const String& path) { - auto mapped_file = make<MappedFile>(path); - if (!mapped_file->is_valid()) + auto file_or_error = MappedFile::map(path); + if (file_or_error.is_error()) return nullptr; - return make<Reader>(move(mapped_file)); + return adopt_own(*new Reader(file_or_error.release_value())); } -Reader::Reader(OwnPtr<MappedFile>&& coredump_file) +Reader::Reader(NonnullRefPtr<MappedFile> coredump_file) : m_coredump_file(move(coredump_file)) - , m_coredump_image((u8*)m_coredump_file->data(), m_coredump_file->size()) + , m_coredump_image(m_coredump_file->bytes()) { size_t index = 0; m_coredump_image.for_each_program_header([this, &index](auto pheader) { @@ -201,11 +201,11 @@ const Reader::LibraryData* Reader::library_containing(FlatPtr address) const } if (!cached_libs.contains(path)) { - auto lib_file = make<MappedFile>(path); - if (!lib_file->is_valid()) + auto file_or_error = MappedFile::map(path); + if (file_or_error.is_error()) return {}; - auto image = ELF::Image((const u8*)lib_file->data(), lib_file->size()); - cached_libs.set(path, make<LibraryData>(name, region->region_start, move(lib_file), move(image))); + auto image = ELF::Image(file_or_error.value()->bytes()); + cached_libs.set(path, make<LibraryData>(name, region->region_start, file_or_error.release_value(), move(image))); } auto lib_data = cached_libs.get(path).value(); diff --git a/Libraries/LibCoreDump/Reader.h b/Libraries/LibCoreDump/Reader.h index 6ca90aba14..9ac411a204 100644 --- a/Libraries/LibCoreDump/Reader.h +++ b/Libraries/LibCoreDump/Reader.h @@ -38,13 +38,12 @@ namespace CoreDump { class Reader { AK_MAKE_NONCOPYABLE(Reader); + AK_MAKE_NONMOVABLE(Reader); public: static OwnPtr<Reader> create(const String&); ~Reader(); - Reader(OwnPtr<MappedFile>&&); - const ELF::Core::ProcessInfo& process_info() const; template<typename Func> @@ -61,7 +60,7 @@ public: struct LibraryData { String name; FlatPtr base_address { 0 }; - OwnPtr<MappedFile> file; + NonnullRefPtr<MappedFile> file; ELF::Image lib_elf; }; const LibraryData* library_containing(FlatPtr address) const; @@ -70,6 +69,8 @@ public: const HashMap<String, String> metadata() const; private: + Reader(NonnullRefPtr<MappedFile>); + class NotesEntryIterator { public: NotesEntryIterator(const u8* notes_data); @@ -85,7 +86,7 @@ private: const u8* start { nullptr }; }; - OwnPtr<MappedFile> m_coredump_file; + NonnullRefPtr<MappedFile> m_coredump_file; ELF::Image m_coredump_image; ssize_t m_notes_segment_index { -1 }; }; diff --git a/Libraries/LibDebug/DebugSession.cpp b/Libraries/LibDebug/DebugSession.cpp index d77fbbe0cc..d231d243e3 100644 --- a/Libraries/LibDebug/DebugSession.cpp +++ b/Libraries/LibDebug/DebugSession.cpp @@ -42,13 +42,6 @@ DebugSession::DebugSession(pid_t pid, String source_root) { } -MappedFile DebugSession::map_executable_for_process(pid_t pid) -{ - MappedFile executable(String::formatted("/proc/{}/exe", pid)); - ASSERT(executable.is_valid()); - return executable; -} - DebugSession::~DebugSession() { if (m_is_debuggee_dead) @@ -373,13 +366,13 @@ void DebugSession::update_loaded_libs() if (m_loaded_libraries.contains(lib_name)) return IterationDecision::Continue; - MappedFile lib_file(object_path.value()); - if (!lib_file.is_valid()) + auto file_or_error = MappedFile ::map(object_path.value()); + if (file_or_error.is_error()) return IterationDecision::Continue; FlatPtr base_address = entry.as_object().get("address").as_u32(); - auto debug_info = make<DebugInfo>(make<ELF::Image>(reinterpret_cast<const u8*>(lib_file.data()), lib_file.size()), m_source_root, base_address); - auto lib = make<LoadedLibrary>(lib_name, move(lib_file), move(debug_info), base_address); + auto debug_info = make<DebugInfo>(make<ELF::Image>(file_or_error.value()->bytes()), m_source_root, base_address); + auto lib = make<LoadedLibrary>(lib_name, file_or_error.release_value(), move(debug_info), base_address); m_loaded_libraries.set(lib_name, move(lib)); return IterationDecision::Continue; diff --git a/Libraries/LibDebug/DebugSession.h b/Libraries/LibDebug/DebugSession.h index bc8bde54f7..f30174a740 100644 --- a/Libraries/LibDebug/DebugSession.h +++ b/Libraries/LibDebug/DebugSession.h @@ -134,11 +134,11 @@ public: struct LoadedLibrary { String name; - MappedFile file; + NonnullRefPtr<MappedFile> file; NonnullOwnPtr<DebugInfo> debug_info; FlatPtr base_address; - LoadedLibrary(const String& name, MappedFile&& file, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address) + LoadedLibrary(const String& name, NonnullRefPtr<MappedFile> file, NonnullOwnPtr<DebugInfo>&& debug_info, FlatPtr base_address) : name(name) , file(move(file)) , debug_info(move(debug_info)) @@ -175,8 +175,6 @@ private: // x86 breakpoint instruction "int3" static constexpr u8 BREAKPOINT_INSTRUCTION = 0xcc; - static MappedFile map_executable_for_process(pid_t); - void update_loaded_libs(); int m_debuggee_pid { -1 }; diff --git a/Libraries/LibELF/Image.cpp b/Libraries/LibELF/Image.cpp index 42c5583750..27bf519353 100644 --- a/Libraries/LibELF/Image.cpp +++ b/Libraries/LibELF/Image.cpp @@ -36,14 +36,19 @@ namespace ELF { -Image::Image(const u8* buffer, size_t size, bool verbose_logging) - : m_buffer(buffer) - , m_size(size) +Image::Image(ReadonlyBytes bytes, bool verbose_logging) + : m_buffer(bytes.data()) + , m_size(bytes.size()) , m_verbose_logging(verbose_logging) { parse(); } +Image::Image(const u8* buffer, size_t size, bool verbose_logging) + : Image(ReadonlyBytes { buffer, size }, verbose_logging) +{ +} + Image::~Image() { } diff --git a/Libraries/LibELF/Image.h b/Libraries/LibELF/Image.h index 7752f7f3e3..3250cf4deb 100644 --- a/Libraries/LibELF/Image.h +++ b/Libraries/LibELF/Image.h @@ -37,7 +37,9 @@ namespace ELF { class Image { public: + explicit Image(ReadonlyBytes, bool verbose_logging = true); explicit Image(const u8*, size_t, bool verbose_logging = true); + ~Image(); void dump() const; bool is_valid() const { return m_valid; } diff --git a/Libraries/LibGUI/FileIconProvider.cpp b/Libraries/LibGUI/FileIconProvider.cpp index 08fc827620..9d2ec26ba0 100644 --- a/Libraries/LibGUI/FileIconProvider.cpp +++ b/Libraries/LibGUI/FileIconProvider.cpp @@ -150,12 +150,14 @@ Icon FileIconProvider::icon_for_executable(const String& path) // If the icon for an app isn't in the cache we attempt to load the file as an ELF image and extract // the serenity_app_icon_* sections which should contain the icons as raw PNG data. In the future it would // be better if the binary signalled the image format being used or we deduced it, e.g. using magic bytes. - auto mapped_file = make<MappedFile>(path); - if (!mapped_file->is_valid()) { + auto file_or_error = MappedFile::map(path); + if (file_or_error.is_error()) { app_icon_cache.set(path, s_executable_icon); return s_executable_icon; } + auto& mapped_file = file_or_error.value(); + if (mapped_file->size() < SELFMAG) { app_icon_cache.set(path, s_executable_icon); return s_executable_icon; diff --git a/Libraries/LibGUI/ImageWidget.cpp b/Libraries/LibGUI/ImageWidget.cpp index ca3a736ad6..a77ca3d1b7 100644 --- a/Libraries/LibGUI/ImageWidget.cpp +++ b/Libraries/LibGUI/ImageWidget.cpp @@ -92,10 +92,11 @@ void ImageWidget::animate() void ImageWidget::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; + auto& mapped_file = *file_or_error.value(); m_image_decoder = Gfx::ImageDecoder::create((const u8*)mapped_file.data(), mapped_file.size()); auto bitmap = m_image_decoder->bitmap(); ASSERT(bitmap); diff --git a/Libraries/LibGfx/BMPLoader.cpp b/Libraries/LibGfx/BMPLoader.cpp index 8f8ccc4d1c..2b178f0257 100644 --- a/Libraries/LibGfx/BMPLoader.cpp +++ b/Libraries/LibGfx/BMPLoader.cpp @@ -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; diff --git a/Libraries/LibGfx/BitmapFont.cpp b/Libraries/LibGfx/BitmapFont.cpp index 26c596cf00..42fefc1530 100644 --- a/Libraries/LibGfx/BitmapFont.cpp +++ b/Libraries/LibGfx/BitmapFont.cpp @@ -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; } diff --git a/Libraries/LibGfx/BitmapFont.h b/Libraries/LibGfx/BitmapFont.h index ef62a63da6..c3ca9f4a1d 100644 --- a/Libraries/LibGfx/BitmapFont.h +++ b/Libraries/LibGfx/BitmapFont.h @@ -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 }; diff --git a/Libraries/LibGfx/GIFLoader.cpp b/Libraries/LibGfx/GIFLoader.cpp index aad9c6d6ed..d29c5969e3 100644 --- a/Libraries/LibGfx/GIFLoader.cpp +++ b/Libraries/LibGfx/GIFLoader.cpp @@ -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))); diff --git a/Libraries/LibGfx/ICOLoader.cpp b/Libraries/LibGfx/ICOLoader.cpp index 6fc0f790e5..031f4e657e 100644 --- a/Libraries/LibGfx/ICOLoader.cpp +++ b/Libraries/LibGfx/ICOLoader.cpp @@ -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))); diff --git a/Libraries/LibGfx/JPGLoader.cpp b/Libraries/LibGfx/JPGLoader.cpp index 124dadae4d..ded73a59fd 100644 --- a/Libraries/LibGfx/JPGLoader.cpp +++ b/Libraries/LibGfx/JPGLoader.cpp @@ -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; diff --git a/Libraries/LibGfx/PNGLoader.cpp b/Libraries/LibGfx/PNGLoader.cpp index 687d0231c7..48405f8bea 100644 --- a/Libraries/LibGfx/PNGLoader.cpp +++ b/Libraries/LibGfx/PNGLoader.cpp @@ -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; diff --git a/Libraries/LibGfx/PortableImageLoaderCommon.h b/Libraries/LibGfx/PortableImageLoaderCommon.h index b5b1c5e91f..5f3c51679c 100644 --- a/Libraries/LibGfx/PortableImageLoaderCommon.h +++ b/Libraries/LibGfx/PortableImageLoaderCommon.h @@ -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(), diff --git a/Libraries/LibPCIDB/Database.cpp b/Libraries/LibPCIDB/Database.cpp index 83c65abb6b..8afc5a2f2d 100644 --- a/Libraries/LibPCIDB/Database.cpp +++ b/Libraries/LibPCIDB/Database.cpp @@ -34,7 +34,10 @@ namespace PCIDB { RefPtr<Database> Database::open(const StringView& file_name) { - auto res = adopt(*new Database(file_name)); + auto file_or_error = MappedFile::map(file_name); + if (file_or_error.is_error()) + return nullptr; + auto res = adopt(*new Database(file_or_error.release_value())); if (res->init() != 0) return nullptr; return res; @@ -131,10 +134,7 @@ int Database::init() if (m_ready) return 0; - if (!m_file.is_valid()) - return -1; - - m_view = StringView((const char*)m_file.data(), m_file.size()); + m_view = StringView { m_file->bytes() }; ParseMode mode = ParseMode::UnknownMode; diff --git a/Libraries/LibPCIDB/Database.h b/Libraries/LibPCIDB/Database.h index 62817c4c7d..dbbe8c9482 100644 --- a/Libraries/LibPCIDB/Database.h +++ b/Libraries/LibPCIDB/Database.h @@ -83,8 +83,10 @@ public: const StringView get_programming_interface(u8 class_id, u8 subclass_id, u8 programming_interface_id) const; private: - Database(const StringView& file_name) - : m_file(file_name) {}; + explicit Database(NonnullRefPtr<MappedFile> file) + : m_file(move(file)) + { + } int init(); @@ -94,7 +96,7 @@ private: ClassMode, }; - MappedFile m_file {}; + NonnullRefPtr<MappedFile> m_file; StringView m_view {}; HashMap<int, NonnullOwnPtr<Vendor>> m_vendors; HashMap<int, NonnullOwnPtr<Class>> m_classes; |