/* * Copyright (c) 2018-2021, Andreas Kling * Copyright (c) 2023, kleines Filmröllchen * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Core { class MappedFile : public FixedMemoryStream { AK_MAKE_NONCOPYABLE(MappedFile); AK_MAKE_NONMOVABLE(MappedFile); public: static ErrorOr> map(StringView path, Mode mode = Mode::ReadOnly); static ErrorOr> map_from_file(NonnullOwnPtr, StringView path); static ErrorOr> map_from_fd_and_close(int fd, StringView path, Mode mode = Mode::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; } ReadonlyBytes bytes() const { return { m_data, m_size }; } private: explicit MappedFile(void*, size_t, Mode); void* m_data { nullptr }; 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; }; }