Serenity Operating System
at master 41 lines 1.0 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/Error.h> 10#include <AK/Noncopyable.h> 11#include <AK/NonnullRefPtr.h> 12#include <AK/RefCounted.h> 13#include <LibCore/Forward.h> 14 15namespace Core { 16 17class MappedFile : public RefCounted<MappedFile> { 18 AK_MAKE_NONCOPYABLE(MappedFile); 19 AK_MAKE_NONMOVABLE(MappedFile); 20 21public: 22 static ErrorOr<NonnullRefPtr<MappedFile>> map(StringView path); 23 static ErrorOr<NonnullRefPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path); 24 static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path); 25 ~MappedFile(); 26 27 void* data() { return m_data; } 28 void const* data() const { return m_data; } 29 30 size_t size() const { return m_size; } 31 32 ReadonlyBytes bytes() const { return { m_data, m_size }; } 33 34private: 35 explicit MappedFile(void*, size_t); 36 37 void* m_data { nullptr }; 38 size_t m_size { 0 }; 39}; 40 41}