mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:38:11 +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:
parent
70fce5c4c7
commit
2f3b901f7f
36 changed files with 184 additions and 199 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -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)));
|
||||
|
|
|
@ -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)));
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue