2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-11-23 10:32:25 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@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
|
|
|
|
|
2021-11-06 23:37:07 +00:00
|
|
|
#include <AK/Error.h>
|
2019-07-11 13:14:30 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-01-10 14:55:54 +00:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/Result.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
|
|
|
|
2021-01-10 14:55:54 +00:00
|
|
|
class MappedFile : public RefCounted<MappedFile> {
|
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:
|
2022-06-13 15:08:18 +00:00
|
|
|
static ErrorOr<NonnullRefPtr<MappedFile>> map(StringView path);
|
|
|
|
static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path);
|
2018-10-10 09:53:07 +00:00
|
|
|
~MappedFile();
|
|
|
|
|
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; }
|
2019-04-03 11:51:49 +00:00
|
|
|
|
2019-04-03 12:15:35 +00:00
|
|
|
size_t size() const { return m_size; }
|
2018-10-10 09:53:07 +00:00
|
|
|
|
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:
|
2021-01-10 14:55:54 +00:00
|
|
|
explicit MappedFile(void*, size_t);
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
}
|