2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-11-23 07:32:25 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-11-06 20:37:07 -03:00
|
|
|
#include <AK/Error.h>
|
2019-07-11 10:14:30 -03:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-01-10 11:55:54 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/Result.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2021-11-23 07:32:25 -03:00
|
|
|
namespace Core {
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2021-01-10 11:55:54 -03:00
|
|
|
class MappedFile : public RefCounted<MappedFile> {
|
2019-07-11 10:14:30 -03:00
|
|
|
AK_MAKE_NONCOPYABLE(MappedFile);
|
2021-01-10 11:55:54 -03:00
|
|
|
AK_MAKE_NONMOVABLE(MappedFile);
|
2020-07-22 12:19:47 -03:00
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
public:
|
2022-06-13 12:08:18 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<MappedFile>> map(StringView path);
|
|
|
|
|
static ErrorOr<NonnullRefPtr<MappedFile>> map_from_fd_and_close(int fd, StringView path);
|
2018-10-10 06:53:07 -03:00
|
|
|
~MappedFile();
|
|
|
|
|
|
2021-01-10 11:55:54 -03:00
|
|
|
void* data() { return m_data; }
|
2022-04-01 14:58:27 -03:00
|
|
|
void const* data() const { return m_data; }
|
2019-04-03 08:51:49 -03:00
|
|
|
|
2019-04-03 09:15:35 -03:00
|
|
|
size_t size() const { return m_size; }
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2021-01-10 11:55:54 -03:00
|
|
|
ReadonlyBytes bytes() const { return { m_data, m_size }; }
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
private:
|
2021-01-10 11:55:54 -03:00
|
|
|
explicit MappedFile(void*, size_t);
|
|
|
|
|
|
|
|
|
|
void* m_data { nullptr };
|
2019-04-03 09:15:35 -03:00
|
|
|
size_t m_size { 0 };
|
2018-10-10 06:53:07 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|