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

LibCore: Make MappedFile a Stream

The internal reuse of FixedMemoryStream makes this straightforward.
There alread is one user of the new API, demonstrating the need for this
change beyond what I said out to use it for :^)
This commit is contained in:
kleines Filmröllchen 2023-09-12 20:21:23 +02:00 committed by Tim Schumacher
parent 062e0db46c
commit d6571f54d8
8 changed files with 266 additions and 25 deletions

View file

@ -7,7 +7,9 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/Error.h>
#include <AK/MemoryStream.h>
#include <AK/Noncopyable.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
@ -15,25 +17,29 @@
namespace Core {
class MappedFile {
class MappedFile : public FixedMemoryStream {
AK_MAKE_NONCOPYABLE(MappedFile);
AK_MAKE_NONMOVABLE(MappedFile);
public:
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path);
~MappedFile();
// Reflects a simplified version of mmap protection and flags.
enum class OpenMode {
ReadOnly,
ReadWrite,
};
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path, OpenMode mode = OpenMode::ReadOnly);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path, OpenMode mode = OpenMode::ReadOnly);
virtual ~MappedFile();
// Non-stream APIs for using MappedFile as a simple POSIX API wrapper.
void* data() { return m_data; }
void const* data() const { return m_data; }
size_t size() const { return m_size; }
ReadonlyBytes bytes() const { return { m_data, m_size }; }
private:
explicit MappedFile(void*, size_t);
explicit MappedFile(void*, size_t, OpenMode);
void* m_data { nullptr };
size_t m_size { 0 };