2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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
|
|
|
|
|
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/OSError.h>
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
#include <AK/Result.h>
|
2018-10-10 09:53:07 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
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:
|
2021-08-06 01:49:56 +00:00
|
|
|
static Result<NonnullRefPtr<MappedFile>, OSError> map(String const& path);
|
2021-08-06 01:47:55 +00:00
|
|
|
static Result<NonnullRefPtr<MappedFile>, OSError> map_from_fd_and_close(int fd, String const& path);
|
2018-10-10 09:53:07 +00:00
|
|
|
~MappedFile();
|
|
|
|
|
2021-01-10 14:55:54 +00:00
|
|
|
void* data() { return m_data; }
|
|
|
|
const void* 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::MappedFile;
|