From 062e0db46c7204c84d2e9b592acb84653ce9f14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 26 Sep 2023 00:54:34 +0200 Subject: [PATCH] LibCore: Make MappedFile OwnPtr-based Since it will become a stream in a little bit, it should behave like all non-trivial stream classes, who are not primarily intended to have shared ownership to make closing behavior more predictable. Across all uses of MappedFile, there is only one use case of shared mapped files in LibVideo, which now uses the thin SharedMappedFile wrapper. --- .../FileManager/PropertiesWindow.cpp | 16 ++++++------ .../FileManager/PropertiesWindow.h | 10 ++++---- .../Applications/FontEditor/MainWidget.cpp | 2 +- Userland/Applications/Help/ManualModel.h | 2 +- .../DevTools/Profiler/DisassemblyModel.cpp | 3 ++- Userland/DevTools/Profiler/Process.h | 2 +- Userland/DevTools/Profiler/Profile.cpp | 2 +- .../DevTools/UserspaceEmulator/Emulator.h | 2 +- Userland/Libraries/LibCompress/Gzip.cpp | 2 +- Userland/Libraries/LibCore/MappedFile.cpp | 8 +++--- Userland/Libraries/LibCore/MappedFile.h | 25 +++++++++++++++---- Userland/Libraries/LibCoredump/Backtrace.h | 4 +-- Userland/Libraries/LibCoredump/Inspector.cpp | 2 +- Userland/Libraries/LibCoredump/Reader.cpp | 2 +- Userland/Libraries/LibCoredump/Reader.h | 6 ++--- Userland/Libraries/LibDebug/LoadedLibrary.h | 4 +-- Userland/Libraries/LibGfx/Font/BitmapFont.cpp | 4 +-- Userland/Libraries/LibGfx/Font/BitmapFont.h | 4 +-- .../Libraries/LibGfx/Font/OpenType/Font.h | 2 +- Userland/Libraries/LibPCIDB/Database.h | 4 +-- .../LibSymbolication/Symbolication.cpp | 2 +- Userland/Libraries/LibUSBDB/Database.h | 4 +-- .../Containers/Matroska/MatroskaDemuxer.cpp | 2 +- .../Containers/Matroska/MatroskaDemuxer.h | 2 +- .../LibVideo/Containers/Matroska/Reader.cpp | 6 ++--- .../LibVideo/Containers/Matroska/Reader.h | 8 +++--- .../Libraries/LibVideo/PlaybackManager.cpp | 2 +- Userland/Libraries/LibVideo/PlaybackManager.h | 2 +- Userland/Utilities/disasm.cpp | 2 +- Userland/Utilities/fdtdump.cpp | 2 +- Userland/Utilities/image.cpp | 4 +-- Userland/Utilities/unzip.cpp | 2 +- 32 files changed, 80 insertions(+), 64 deletions(-) diff --git a/Userland/Applications/FileManager/PropertiesWindow.cpp b/Userland/Applications/FileManager/PropertiesWindow.cpp index 49817731ac..6264f6f694 100644 --- a/Userland/Applications/FileManager/PropertiesWindow.cpp +++ b/Userland/Applications/FileManager/PropertiesWindow.cpp @@ -246,7 +246,7 @@ ErrorOr PropertiesWindow::create_file_type_specific_tabs(GUI::TabWidget& t return {}; } -ErrorOr PropertiesWindow::create_archive_tab(GUI::TabWidget& tab_widget, NonnullRefPtr mapped_file) +ErrorOr PropertiesWindow::create_archive_tab(GUI::TabWidget& tab_widget, NonnullOwnPtr mapped_file) { auto maybe_zip = Archive::Zip::try_create(mapped_file->bytes()); if (!maybe_zip.has_value()) { @@ -268,7 +268,7 @@ ErrorOr PropertiesWindow::create_archive_tab(GUI::TabWidget& tab_widget, N return {}; } -ErrorOr PropertiesWindow::create_audio_tab(GUI::TabWidget& tab_widget, NonnullRefPtr mapped_file) +ErrorOr PropertiesWindow::create_audio_tab(GUI::TabWidget& tab_widget, NonnullOwnPtr mapped_file) { auto loader_or_error = Audio::Loader::create(mapped_file->bytes()); if (loader_or_error.is_error()) { @@ -317,10 +317,10 @@ struct FontInfo { Format format; NonnullRefPtr typeface; }; -static ErrorOr load_font(StringView path, StringView mime_type, NonnullRefPtr mapped_file) +static ErrorOr load_font(StringView path, StringView mime_type, NonnullOwnPtr mapped_file) { if (path.ends_with(".font"sv)) { - auto font = TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file)); + auto font = TRY(Gfx::BitmapFont::try_load_from_mapped_file(move(mapped_file))); auto typeface = TRY(try_make_ref_counted(font->family(), font->variant())); typeface->add_bitmap_font(move(font)); return FontInfo { FontInfo::Format::BitmapFont, move(typeface) }; @@ -349,9 +349,9 @@ static ErrorOr load_font(StringView path, StringView mime_type, Nonnul return Error::from_string_view("Unrecognized font format."sv); } -ErrorOr PropertiesWindow::create_font_tab(GUI::TabWidget& tab_widget, NonnullRefPtr mapped_file, StringView mime_type) +ErrorOr PropertiesWindow::create_font_tab(GUI::TabWidget& tab_widget, NonnullOwnPtr mapped_file, StringView mime_type) { - auto font_info_or_error = load_font(m_path, mime_type, mapped_file); + auto font_info_or_error = load_font(m_path, mime_type, move(mapped_file)); if (font_info_or_error.is_error()) { warnln("Failed to open '{}': {}", m_path, font_info_or_error.release_error()); return {}; @@ -398,7 +398,7 @@ ErrorOr PropertiesWindow::create_font_tab(GUI::TabWidget& tab_widget, Nonn return {}; } -ErrorOr PropertiesWindow::create_image_tab(GUI::TabWidget& tab_widget, NonnullRefPtr mapped_file, StringView mime_type) +ErrorOr PropertiesWindow::create_image_tab(GUI::TabWidget& tab_widget, NonnullOwnPtr mapped_file, StringView mime_type) { auto image_decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(mapped_file->bytes(), mime_type); if (!image_decoder) @@ -456,7 +456,7 @@ ErrorOr PropertiesWindow::create_image_tab(GUI::TabWidget& tab_widget, Non return {}; } -ErrorOr PropertiesWindow::create_pdf_tab(GUI::TabWidget& tab_widget, NonnullRefPtr mapped_file) +ErrorOr PropertiesWindow::create_pdf_tab(GUI::TabWidget& tab_widget, NonnullOwnPtr mapped_file) { auto maybe_document = PDF::Document::create(mapped_file->bytes()); if (maybe_document.is_error()) { diff --git a/Userland/Applications/FileManager/PropertiesWindow.h b/Userland/Applications/FileManager/PropertiesWindow.h index 1e3f3de996..cbf54466db 100644 --- a/Userland/Applications/FileManager/PropertiesWindow.h +++ b/Userland/Applications/FileManager/PropertiesWindow.h @@ -30,11 +30,11 @@ private: ErrorOr create_widgets(bool disable_rename); ErrorOr create_general_tab(GUI::TabWidget&, bool disable_rename); ErrorOr create_file_type_specific_tabs(GUI::TabWidget&); - ErrorOr create_archive_tab(GUI::TabWidget&, NonnullRefPtr); - ErrorOr create_audio_tab(GUI::TabWidget&, NonnullRefPtr); - ErrorOr create_font_tab(GUI::TabWidget&, NonnullRefPtr, StringView mime_type); - ErrorOr create_image_tab(GUI::TabWidget&, NonnullRefPtr, StringView mime_type); - ErrorOr create_pdf_tab(GUI::TabWidget&, NonnullRefPtr); + ErrorOr create_archive_tab(GUI::TabWidget&, NonnullOwnPtr); + ErrorOr create_audio_tab(GUI::TabWidget&, NonnullOwnPtr); + ErrorOr create_font_tab(GUI::TabWidget&, NonnullOwnPtr, StringView mime_type); + ErrorOr create_image_tab(GUI::TabWidget&, NonnullOwnPtr, StringView mime_type); + ErrorOr create_pdf_tab(GUI::TabWidget&, NonnullOwnPtr); struct PermissionMasks { mode_t read; diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index ba2a801a2c..56b016fb34 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -818,7 +818,7 @@ ErrorOr MainWidget::save_file(StringView path, NonnullOwnPtr f ErrorOr MainWidget::open_file(StringView path, NonnullOwnPtr file) { auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path)); - auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(mapped_file))->unmasked_character_set()); + auto unmasked_font = TRY(TRY(Gfx::BitmapFont::try_load_from_mapped_file(move(mapped_file)))->unmasked_character_set()); TRY(initialize(path, move(unmasked_font))); if (!path.is_empty()) GUI::Application::the()->set_most_recently_open_file(TRY(String::from_utf8(path))); diff --git a/Userland/Applications/Help/ManualModel.h b/Userland/Applications/Help/ManualModel.h index 526e8623ce..a555cbdfe8 100644 --- a/Userland/Applications/Help/ManualModel.h +++ b/Userland/Applications/Help/ManualModel.h @@ -42,5 +42,5 @@ private: GUI::Icon m_section_open_icon; GUI::Icon m_section_icon; GUI::Icon m_page_icon; - mutable HashMap> m_mapped_files; + mutable HashMap> m_mapped_files; }; diff --git a/Userland/DevTools/Profiler/DisassemblyModel.cpp b/Userland/DevTools/Profiler/DisassemblyModel.cpp index 5b4eab557e..9d8618bd79 100644 --- a/Userland/DevTools/Profiler/DisassemblyModel.cpp +++ b/Userland/DevTools/Profiler/DisassemblyModel.cpp @@ -29,7 +29,8 @@ static ELF::Image* try_load_kernel_binary() auto kernel_binary_or_error = Core::MappedFile::map("/boot/Kernel"sv); if (!kernel_binary_or_error.is_error()) { auto kernel_binary = kernel_binary_or_error.release_value(); - s_kernel_binary = { { kernel_binary, ELF::Image(kernel_binary->bytes()) } }; + auto image = ELF::Image(kernel_binary->bytes()); + s_kernel_binary = { { move(kernel_binary), image } }; return &s_kernel_binary->elf; } return nullptr; diff --git a/Userland/DevTools/Profiler/Process.h b/Userland/DevTools/Profiler/Process.h index f47f7c5d18..064ca69f2b 100644 --- a/Userland/DevTools/Profiler/Process.h +++ b/Userland/DevTools/Profiler/Process.h @@ -17,7 +17,7 @@ namespace Profiler { struct MappedObject { - NonnullRefPtr file; + NonnullOwnPtr file; ELF::Image elf; }; diff --git a/Userland/DevTools/Profiler/Profile.cpp b/Userland/DevTools/Profiler/Profile.cpp index 5fddf4abd3..f665227c15 100644 --- a/Userland/DevTools/Profiler/Profile.cpp +++ b/Userland/DevTools/Profiler/Profile.cpp @@ -280,7 +280,7 @@ ErrorOr> Profile::load_from_perfcore_file(StringView path if (!debuginfo_file_or_error.is_error()) { auto debuginfo_file = debuginfo_file_or_error.release_value(); auto debuginfo_image = ELF::Image(debuginfo_file->bytes()); - g_kernel_debuginfo_object = { { debuginfo_file, move(debuginfo_image) } }; + g_kernel_debuginfo_object = { { move(debuginfo_file), move(debuginfo_image) } }; } } diff --git a/Userland/DevTools/UserspaceEmulator/Emulator.h b/Userland/DevTools/UserspaceEmulator/Emulator.h index f867ea4434..df9f4003ea 100644 --- a/Userland/DevTools/UserspaceEmulator/Emulator.h +++ b/Userland/DevTools/UserspaceEmulator/Emulator.h @@ -286,7 +286,7 @@ private: Optional m_loader_text_size; struct CachedELF { - NonnullRefPtr mapped_file; + NonnullOwnPtr mapped_file; NonnullOwnPtr debug_info; NonnullOwnPtr image; }; diff --git a/Userland/Libraries/LibCompress/Gzip.cpp b/Userland/Libraries/LibCompress/Gzip.cpp index 5f23560bd7..89c779aa4c 100644 --- a/Userland/Libraries/LibCompress/Gzip.cpp +++ b/Userland/Libraries/LibCompress/Gzip.cpp @@ -266,7 +266,7 @@ ErrorOr GzipCompressor::compress_file(StringView input_filename, NonnullOw { // We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression) // TODO: automatically fallback to buffered streaming for very large files - RefPtr file; + OwnPtr file; ReadonlyBytes input_bytes; if (TRY(Core::System::stat(input_filename)).st_size > 0) { diff --git a/Userland/Libraries/LibCore/MappedFile.cpp b/Userland/Libraries/LibCore/MappedFile.cpp index af0b533062..125d5b0c08 100644 --- a/Userland/Libraries/LibCore/MappedFile.cpp +++ b/Userland/Libraries/LibCore/MappedFile.cpp @@ -14,18 +14,18 @@ namespace Core { -ErrorOr> MappedFile::map(StringView path) +ErrorOr> MappedFile::map(StringView path) { auto fd = TRY(Core::System::open(path, O_RDONLY | O_CLOEXEC, 0)); return map_from_fd_and_close(fd, path); } -ErrorOr> MappedFile::map_from_file(NonnullOwnPtr stream, StringView path) +ErrorOr> MappedFile::map_from_file(NonnullOwnPtr stream, StringView path) { return map_from_fd_and_close(stream->leak_fd(Badge {}), path); } -ErrorOr> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path) +ErrorOr> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] StringView path) { TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC)); @@ -38,7 +38,7 @@ ErrorOr> MappedFile::map_from_fd_and_close(int fd, [[m auto* ptr = TRY(Core::System::mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0, 0, path)); - return adopt_ref(*new MappedFile(ptr, size)); + return adopt_own(*new MappedFile(ptr, size)); } MappedFile::MappedFile(void* ptr, size_t size) diff --git a/Userland/Libraries/LibCore/MappedFile.h b/Userland/Libraries/LibCore/MappedFile.h index 7ad2b709f3..8372c6e4bd 100644 --- a/Userland/Libraries/LibCore/MappedFile.h +++ b/Userland/Libraries/LibCore/MappedFile.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2021, Andreas Kling + * Copyright (c) 2023, kleines Filmröllchen * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,20 +9,20 @@ #include #include -#include +#include #include #include namespace Core { -class MappedFile : public RefCounted { +class MappedFile { AK_MAKE_NONCOPYABLE(MappedFile); AK_MAKE_NONMOVABLE(MappedFile); public: - static ErrorOr> map(StringView path); - static ErrorOr> map_from_file(NonnullOwnPtr, StringView path); - static ErrorOr> map_from_fd_and_close(int fd, StringView path); + static ErrorOr> map(StringView path); + static ErrorOr> map_from_file(NonnullOwnPtr, StringView path); + static ErrorOr> map_from_fd_and_close(int fd, StringView path); ~MappedFile(); void* data() { return m_data; } @@ -38,4 +39,18 @@ private: size_t m_size { 0 }; }; +class SharedMappedFile : public RefCounted { +public: + explicit SharedMappedFile(NonnullOwnPtr file) + : m_file(move(file)) + { + } + + MappedFile const& operator->() const { return *m_file; } + MappedFile& operator->() { return *m_file; } + +private: + NonnullOwnPtr m_file; +}; + } diff --git a/Userland/Libraries/LibCoredump/Backtrace.h b/Userland/Libraries/LibCoredump/Backtrace.h index 9ce037f3c5..598cecccec 100644 --- a/Userland/Libraries/LibCoredump/Backtrace.h +++ b/Userland/Libraries/LibCoredump/Backtrace.h @@ -15,14 +15,14 @@ namespace Coredump { struct ELFObjectInfo { - ELFObjectInfo(NonnullRefPtr file, NonnullOwnPtr&& debug_info, NonnullOwnPtr image) + ELFObjectInfo(NonnullOwnPtr file, NonnullOwnPtr&& debug_info, NonnullOwnPtr image) : file(move(file)) , debug_info(move(debug_info)) , image(move(image)) { } - NonnullRefPtr file; + NonnullOwnPtr file; NonnullOwnPtr debug_info; NonnullOwnPtr image; }; diff --git a/Userland/Libraries/LibCoredump/Inspector.cpp b/Userland/Libraries/LibCoredump/Inspector.cpp index b5cae587a2..bad065ab63 100644 --- a/Userland/Libraries/LibCoredump/Inspector.cpp +++ b/Userland/Libraries/LibCoredump/Inspector.cpp @@ -47,7 +47,7 @@ void Inspector::parse_loaded_libraries(Function on_progress) auto image = make(file_or_error.value()->bytes()); auto debug_info = make(*image, DeprecatedString {}, library.base_address); - m_loaded_libraries.append(make(library.name, file_or_error.value(), move(image), move(debug_info), library.base_address)); + m_loaded_libraries.append(make(library.name, file_or_error.release_value(), move(image), move(debug_info), library.base_address)); }); } diff --git a/Userland/Libraries/LibCoredump/Reader.cpp b/Userland/Libraries/LibCoredump/Reader.cpp index e071f472ad..42ffb597ac 100644 --- a/Userland/Libraries/LibCoredump/Reader.cpp +++ b/Userland/Libraries/LibCoredump/Reader.cpp @@ -43,7 +43,7 @@ Reader::Reader(ByteBuffer buffer) m_coredump_buffer = move(buffer); } -Reader::Reader(NonnullRefPtr file) +Reader::Reader(NonnullOwnPtr file) : Reader(file->bytes()) { m_mapped_file = move(file); diff --git a/Userland/Libraries/LibCoredump/Reader.h b/Userland/Libraries/LibCoredump/Reader.h index e8a754bd5f..959c8f8c74 100644 --- a/Userland/Libraries/LibCoredump/Reader.h +++ b/Userland/Libraries/LibCoredump/Reader.h @@ -66,7 +66,7 @@ public: struct LibraryData { DeprecatedString name; FlatPtr base_address { 0 }; - NonnullRefPtr file; + NonnullOwnPtr file; ELF::Image lib_elf; }; LibraryData const* library_containing(FlatPtr address) const; @@ -83,7 +83,7 @@ public: private: explicit Reader(ReadonlyBytes); explicit Reader(ByteBuffer); - explicit Reader(NonnullRefPtr); + explicit Reader(NonnullOwnPtr); static Optional decompress_coredump(ReadonlyBytes); @@ -108,7 +108,7 @@ private: const JsonObject process_info() const; // For uncompressed coredumps, we keep the MappedFile - RefPtr m_mapped_file; + OwnPtr m_mapped_file; // For compressed coredumps, we decompress them into a ByteBuffer ByteBuffer m_coredump_buffer; diff --git a/Userland/Libraries/LibDebug/LoadedLibrary.h b/Userland/Libraries/LibDebug/LoadedLibrary.h index 47580b00c1..658b31b435 100644 --- a/Userland/Libraries/LibDebug/LoadedLibrary.h +++ b/Userland/Libraries/LibDebug/LoadedLibrary.h @@ -14,12 +14,12 @@ namespace Debug { struct LoadedLibrary { DeprecatedString name; - NonnullRefPtr file; + NonnullOwnPtr file; NonnullOwnPtr image; NonnullOwnPtr debug_info; FlatPtr base_address {}; - LoadedLibrary(DeprecatedString const& name, NonnullRefPtr file, NonnullOwnPtr image, NonnullOwnPtr&& debug_info, FlatPtr base_address) + LoadedLibrary(DeprecatedString const& name, NonnullOwnPtr file, NonnullOwnPtr image, NonnullOwnPtr&& debug_info, FlatPtr base_address) : name(name) , file(move(file)) , image(move(image)) diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp index 4aae9858d3..28ae2f0434 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.cpp @@ -239,10 +239,10 @@ ErrorOr> BitmapFont::try_load_from_file(DeprecatedStri return try_load_from_mapped_file(move(mapped_file)); } -ErrorOr> BitmapFont::try_load_from_mapped_file(RefPtr const& mapped_file) +ErrorOr> BitmapFont::try_load_from_mapped_file(OwnPtr mapped_file) { auto font = TRY(load_from_memory((u8 const*)mapped_file->data())); - font->m_mapped_file = mapped_file; + font->m_mapped_file = move(mapped_file); return font; } diff --git a/Userland/Libraries/LibGfx/Font/BitmapFont.h b/Userland/Libraries/LibGfx/Font/BitmapFont.h index 8f7c5e22e1..fba06b3308 100644 --- a/Userland/Libraries/LibGfx/Font/BitmapFont.h +++ b/Userland/Libraries/LibGfx/Font/BitmapFont.h @@ -32,7 +32,7 @@ public: static RefPtr load_from_file(DeprecatedString const& path); static ErrorOr> try_load_from_file(DeprecatedString const& path); - static ErrorOr> try_load_from_mapped_file(RefPtr const&); + static ErrorOr> try_load_from_mapped_file(OwnPtr); ErrorOr write_to_file(DeprecatedString const& path); ErrorOr write_to_file(NonnullOwnPtr file); @@ -156,7 +156,7 @@ private: u8* m_rows { nullptr }; u8* m_glyph_widths { nullptr }; - RefPtr m_mapped_file; + OwnPtr m_mapped_file; u8 m_glyph_width { 0 }; u8 m_glyph_height { 0 }; diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.h b/Userland/Libraries/LibGfx/Font/OpenType/Font.h index fb0a32ab20..b2b7f4cd67 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.h +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.h @@ -107,7 +107,7 @@ private: { } - RefPtr m_mapped_file; + OwnPtr m_mapped_file; ReadonlyBytes m_buffer; diff --git a/Userland/Libraries/LibPCIDB/Database.h b/Userland/Libraries/LibPCIDB/Database.h index 9353578bcf..fbc714b1ef 100644 --- a/Userland/Libraries/LibPCIDB/Database.h +++ b/Userland/Libraries/LibPCIDB/Database.h @@ -64,7 +64,7 @@ public: const StringView get_programming_interface(u8 class_id, u8 subclass_id, u8 programming_interface_id) const; private: - explicit Database(NonnullRefPtr file) + explicit Database(NonnullOwnPtr file) : m_file(move(file)) { } @@ -77,7 +77,7 @@ private: ClassMode, }; - NonnullRefPtr m_file; + NonnullOwnPtr m_file; StringView m_view {}; HashMap> m_vendors; HashMap> m_classes; diff --git a/Userland/Libraries/LibSymbolication/Symbolication.cpp b/Userland/Libraries/LibSymbolication/Symbolication.cpp index 29afeb7ed0..d2c0cff765 100644 --- a/Userland/Libraries/LibSymbolication/Symbolication.cpp +++ b/Userland/Libraries/LibSymbolication/Symbolication.cpp @@ -19,7 +19,7 @@ namespace Symbolication { struct CachedELF { - NonnullRefPtr mapped_file; + NonnullOwnPtr mapped_file; NonnullOwnPtr debug_info; NonnullOwnPtr image; }; diff --git a/Userland/Libraries/LibUSBDB/Database.h b/Userland/Libraries/LibUSBDB/Database.h index 5f0e297df0..b0028fb83a 100644 --- a/Userland/Libraries/LibUSBDB/Database.h +++ b/Userland/Libraries/LibUSBDB/Database.h @@ -63,7 +63,7 @@ public: const StringView get_protocol(u8 class_id, u8 subclass_id, u8 protocol_id) const; private: - explicit Database(NonnullRefPtr file) + explicit Database(NonnullOwnPtr file) : m_file(move(file)) { } @@ -76,7 +76,7 @@ private: ClassMode, }; - NonnullRefPtr m_file; + NonnullOwnPtr m_file; StringView m_view {}; HashMap> m_vendors; HashMap> m_classes; diff --git a/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.cpp b/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.cpp index c2b873031b..a45a9cb74c 100644 --- a/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.cpp +++ b/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.cpp @@ -14,7 +14,7 @@ DecoderErrorOr> MatroskaDemuxer::from_file(String return make(TRY(Reader::from_file(filename))); } -DecoderErrorOr> MatroskaDemuxer::from_mapped_file(NonnullRefPtr mapped_file) +DecoderErrorOr> MatroskaDemuxer::from_mapped_file(NonnullOwnPtr mapped_file) { return make(TRY(Reader::from_mapped_file(move(mapped_file)))); } diff --git a/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.h b/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.h index 225ebed06f..59bd151a27 100644 --- a/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.h +++ b/Userland/Libraries/LibVideo/Containers/Matroska/MatroskaDemuxer.h @@ -18,7 +18,7 @@ public: // FIXME: We should instead accept some abstract data streaming type so that the demuxer // can work with non-contiguous data. static DecoderErrorOr> from_file(StringView filename); - static DecoderErrorOr> from_mapped_file(NonnullRefPtr mapped_file); + static DecoderErrorOr> from_mapped_file(NonnullOwnPtr mapped_file); static DecoderErrorOr> from_data(ReadonlyBytes data); diff --git a/Userland/Libraries/LibVideo/Containers/Matroska/Reader.cpp b/Userland/Libraries/LibVideo/Containers/Matroska/Reader.cpp index 9d6ca54a18..e5091ee2d3 100644 --- a/Userland/Libraries/LibVideo/Containers/Matroska/Reader.cpp +++ b/Userland/Libraries/LibVideo/Containers/Matroska/Reader.cpp @@ -86,13 +86,13 @@ constexpr u32 CUE_REFERENCE_ID = 0xDB; DecoderErrorOr Reader::from_file(StringView path) { auto mapped_file = DECODER_TRY(DecoderErrorCategory::IO, Core::MappedFile::map(path)); - return from_mapped_file(mapped_file); + return from_mapped_file(move(mapped_file)); } -DecoderErrorOr Reader::from_mapped_file(NonnullRefPtr mapped_file) +DecoderErrorOr Reader::from_mapped_file(NonnullOwnPtr mapped_file) { auto reader = TRY(from_data(mapped_file->bytes())); - reader.m_mapped_file = move(mapped_file); + reader.m_mapped_file = make_ref_counted(move(mapped_file)); return reader; } diff --git a/Userland/Libraries/LibVideo/Containers/Matroska/Reader.h b/Userland/Libraries/LibVideo/Containers/Matroska/Reader.h index 80cf336e43..6c7e352ade 100644 --- a/Userland/Libraries/LibVideo/Containers/Matroska/Reader.h +++ b/Userland/Libraries/LibVideo/Containers/Matroska/Reader.h @@ -25,7 +25,7 @@ public: typedef Function(TrackEntry const&)> TrackEntryCallback; static DecoderErrorOr from_file(StringView path); - static DecoderErrorOr from_mapped_file(NonnullRefPtr mapped_file); + static DecoderErrorOr from_mapped_file(NonnullOwnPtr mapped_file); static DecoderErrorOr from_data(ReadonlyBytes data); @@ -60,7 +60,7 @@ private: DecoderErrorOr ensure_cues_are_parsed(); DecoderErrorOr seek_to_cue_for_timestamp(SampleIterator&, Duration const&); - RefPtr m_mapped_file; + RefPtr m_mapped_file; ReadonlyBytes m_data; Optional m_header; @@ -89,7 +89,7 @@ public: private: friend class Reader; - SampleIterator(RefPtr file, ReadonlyBytes data, TrackEntry track, u64 timestamp_scale, size_t position) + SampleIterator(RefPtr file, ReadonlyBytes data, TrackEntry track, u64 timestamp_scale, size_t position) : m_file(move(file)) , m_data(data) , m_track(move(track)) @@ -100,7 +100,7 @@ private: DecoderErrorOr seek_to_cue_point(CuePoint const& cue_point); - RefPtr m_file; + RefPtr m_file; ReadonlyBytes m_data; TrackEntry m_track; u64 m_segment_timestamp_scale { 0 }; diff --git a/Userland/Libraries/LibVideo/PlaybackManager.cpp b/Userland/Libraries/LibVideo/PlaybackManager.cpp index 45524d3308..8ef34ef3f8 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.cpp +++ b/Userland/Libraries/LibVideo/PlaybackManager.cpp @@ -31,7 +31,7 @@ DecoderErrorOr> PlaybackManager::from_file(String return create(move(demuxer)); } -DecoderErrorOr> PlaybackManager::from_mapped_file(NonnullRefPtr mapped_file) +DecoderErrorOr> PlaybackManager::from_mapped_file(NonnullOwnPtr mapped_file) { auto demuxer = TRY(Matroska::MatroskaDemuxer::from_mapped_file(move(mapped_file))); return create(move(demuxer)); diff --git a/Userland/Libraries/LibVideo/PlaybackManager.h b/Userland/Libraries/LibVideo/PlaybackManager.h index b35d770b6c..f3a5bf7a94 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.h +++ b/Userland/Libraries/LibVideo/PlaybackManager.h @@ -112,7 +112,7 @@ public: static constexpr SeekMode DEFAULT_SEEK_MODE = SeekMode::Accurate; static DecoderErrorOr> from_file(StringView file); - static DecoderErrorOr> from_mapped_file(NonnullRefPtr file); + static DecoderErrorOr> from_mapped_file(NonnullOwnPtr file); static DecoderErrorOr> from_data(ReadonlyBytes data); diff --git a/Userland/Utilities/disasm.cpp b/Userland/Utilities/disasm.cpp index a8985bd744..1b00c6aa34 100644 --- a/Userland/Utilities/disasm.cpp +++ b/Userland/Utilities/disasm.cpp @@ -29,7 +29,7 @@ ErrorOr serenity_main(Main::Arguments args) args_parser.add_positional_argument(path, "Path to i386 binary file", "path"); args_parser.parse(args); - RefPtr file; + OwnPtr file; u8 const* asm_data = nullptr; size_t asm_size = 0; if ((TRY(Core::System::stat(path))).st_size > 0) { diff --git a/Userland/Utilities/fdtdump.cpp b/Userland/Utilities/fdtdump.cpp index 481bd448a3..b49e869ca8 100644 --- a/Userland/Utilities/fdtdump.cpp +++ b/Userland/Utilities/fdtdump.cpp @@ -31,7 +31,7 @@ ErrorOr serenity_main(Main::Arguments arguments) } auto const* fdt_header = reinterpret_cast(file->data()); - auto bytes = ReadonlyBytes { file->data(), file->size() }; + auto bytes = file->bytes(); TRY(DeviceTree::dump(*fdt_header, bytes)); diff --git a/Userland/Utilities/image.cpp b/Userland/Utilities/image.cpp index 4af25a94dd..7fc97e2eb6 100644 --- a/Userland/Utilities/image.cpp +++ b/Userland/Utilities/image.cpp @@ -116,7 +116,7 @@ ErrorOr serenity_main(Main::Arguments arguments) Optional icc_data = TRY(decoder->icc_data()); - RefPtr icc_file; + OwnPtr icc_file; if (!assign_color_profile_path.is_empty()) { icc_file = TRY(Core::MappedFile::map(assign_color_profile_path)); icc_data = icc_file->bytes(); @@ -128,7 +128,7 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; } - auto source_icc_file = icc_file; + auto source_icc_file = move(icc_file); auto source_icc_data = icc_data.value(); icc_file = TRY(Core::MappedFile::map(convert_color_profile_path)); icc_data = icc_file->bytes(); diff --git a/Userland/Utilities/unzip.cpp b/Userland/Utilities/unzip.cpp index 4996441344..440ba687bf 100644 --- a/Userland/Utilities/unzip.cpp +++ b/Userland/Utilities/unzip.cpp @@ -122,7 +122,7 @@ ErrorOr serenity_main(Main::Arguments arguments) // FIXME: Map file chunk-by-chunk once we have mmap() with offset. // This will require mapping some parts then unmapping them repeatedly, // but it would be significantly faster and less syscall heavy than seek()/read() at every read. - RefPtr mapped_file; + OwnPtr mapped_file; ReadonlyBytes input_bytes; if (st.st_size > 0) { mapped_file = TRY(Core::MappedFile::map(zip_file_path));