2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2023-09-25 22:54:34 +00:00
|
|
|
* Copyright (c) 2023, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-09-12 18:21:23 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2021-11-06 23:37:07 +00:00
|
|
|
#include <AK/Error.h>
|
2023-09-12 18:21:23 +00:00
|
|
|
#include <AK/MemoryStream.h>
|
2019-07-11 13:14:30 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2023-09-25 22:54:34 +00:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2021-01-10 14:55:54 +00:00
|
|
|
#include <AK/RefCounted.h>
|
2023-01-15 00:57:29 +00:00
|
|
|
#include <LibCore/Forward.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2021-11-23 10:32:25 +00:00
|
|
|
namespace Core {
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-09-12 18:21:23 +00:00
|
|
|
class MappedFile : public FixedMemoryStream {
|
2019-07-11 13:14:30 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(MappedFile);
|
2021-01-10 14:55:54 +00:00
|
|
|
AK_MAKE_NONMOVABLE(MappedFile);
|
2020-07-22 15:19:47 +00:00
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
public:
|
2023-11-05 20:31:17 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<MappedFile>> map(StringView path, Mode mode = Mode::ReadOnly);
|
2023-09-25 22:54:34 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_file(NonnullOwnPtr<Core::File>, StringView path);
|
2023-11-05 20:31:17 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path, Mode mode = Mode::ReadOnly);
|
2023-09-12 18:21:23 +00:00
|
|
|
virtual ~MappedFile();
|
2018-10-10 09:53:07 +00:00
|
|
|
|
2023-09-12 18:21:23 +00:00
|
|
|
// Non-stream APIs for using MappedFile as a simple POSIX API wrapper.
|
2021-01-10 14:55:54 +00:00
|
|
|
void* data() { return m_data; }
|
2022-04-01 17:58:27 +00:00
|
|
|
void const* data() const { return m_data; }
|
2021-01-10 14:55:54 +00:00
|
|
|
ReadonlyBytes bytes() const { return { m_data, m_size }; }
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
private:
|
2023-11-05 20:31:17 +00:00
|
|
|
explicit MappedFile(void*, size_t, Mode);
|
2021-01-10 14:55:54 +00:00
|
|
|
|
|
|
|
void* m_data { nullptr };
|
2019-04-03 12:15:35 +00:00
|
|
|
size_t m_size { 0 };
|
2018-10-10 09:53:07 +00:00
|
|
|
};
|
|
|
|
|
2023-09-25 22:54:34 +00:00
|
|
|
class SharedMappedFile : public RefCounted<SharedMappedFile> {
|
|
|
|
public:
|
|
|
|
explicit SharedMappedFile(NonnullOwnPtr<MappedFile> file)
|
|
|
|
: m_file(move(file))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MappedFile const& operator->() const { return *m_file; }
|
|
|
|
MappedFile& operator->() { return *m_file; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
NonnullOwnPtr<MappedFile> m_file;
|
|
|
|
};
|
|
|
|
|
2018-10-10 09:53:07 +00:00
|
|
|
}
|