1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18: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:
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

@ -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;

View file

@ -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;
};

View file

@ -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();

View file

@ -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 };
};