2014-12-19 18:28:50 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-12-19 18:28:50 +11:00
|
|
|
*
|
2016-06-28 14:14:23 +10:00
|
|
|
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net>
|
2014-12-19 18:28:50 +11:00
|
|
|
*/
|
|
|
|
|
2021-04-14 15:35:49 +10:00
|
|
|
#pragma once
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2016-10-02 16:13:31 +11:00
|
|
|
#include "posix/fd.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2016-06-28 14:15:19 +10:00
|
|
|
#include "view.hpp"
|
|
|
|
|
2016-06-28 15:58:41 +10:00
|
|
|
#include <type_traits>
|
2018-12-05 19:12:03 +11:00
|
|
|
#include <filesystem>
|
2016-06-28 15:58:41 +10:00
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace detail::posix {
|
2018-07-05 13:44:34 +10:00
|
|
|
// represents a memory mapped file.
|
|
|
|
//
|
|
|
|
// the data pointer may be null in the case of a zero length file. it
|
|
|
|
// is expected that the user will take measures to avoid dereferencing
|
|
|
|
// such a pointer.
|
2014-12-19 18:28:50 +11:00
|
|
|
class mapped_file {
|
2016-06-28 12:21:12 +10:00
|
|
|
public:
|
2018-01-13 13:48:58 +11:00
|
|
|
using value_type = uint8_t;
|
|
|
|
using reference = value_type&;
|
|
|
|
using const_reference = const value_type&;
|
|
|
|
using iterator = value_type*;
|
|
|
|
using const_iterator = const value_type*;
|
|
|
|
using difference_type = std::iterator_traits<iterator>::difference_type;
|
|
|
|
using size_type = size_t;
|
|
|
|
|
2018-12-05 19:12:03 +11:00
|
|
|
mapped_file (const std::filesystem::path&,
|
2017-07-19 17:20:51 +10:00
|
|
|
int fflags = O_RDONLY | O_BINARY,
|
|
|
|
int mflags = PROT_READ);
|
2018-08-05 14:42:02 +10:00
|
|
|
mapped_file (const cruft::posix::fd&,
|
2017-07-19 17:20:51 +10:00
|
|
|
int mflags = PROT_READ);
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2016-06-28 12:21:12 +10:00
|
|
|
mapped_file (const mapped_file&) = delete;
|
|
|
|
mapped_file& operator= (const mapped_file&) = delete;
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2019-01-17 19:49:45 +11:00
|
|
|
mapped_file (mapped_file&&) noexcept;
|
2018-07-05 13:44:34 +10:00
|
|
|
mapped_file& operator= (mapped_file&&) noexcept;
|
|
|
|
|
2016-06-28 12:21:12 +10:00
|
|
|
~mapped_file ();
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2016-06-28 12:21:12 +10:00
|
|
|
bool empty (void) const;
|
2016-10-25 19:57:13 +11:00
|
|
|
|
|
|
|
/// returns the total allocated mapped region in bytes.
|
|
|
|
///
|
2018-07-05 13:44:34 +10:00
|
|
|
/// result is typed size_t (rather than the native signed type)
|
|
|
|
/// because we often use this in conjunction with sizeof and
|
|
|
|
/// packed structure.
|
|
|
|
///
|
2016-10-25 19:57:13 +11:00
|
|
|
/// it is greatly simpler to cast to signed where it's actually
|
|
|
|
/// required rather than the other way around.
|
2018-01-13 13:48:58 +11:00
|
|
|
size_type size (void) const;
|
2016-06-28 14:14:23 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
const_iterator data (void) const &;
|
|
|
|
iterator data (void) &;
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
iterator begin (void) &;
|
|
|
|
iterator end (void) &;
|
2016-06-28 12:21:30 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
const_iterator begin (void) const &;
|
|
|
|
const_iterator end (void) const &;
|
2016-06-28 15:58:41 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
const_iterator cbegin (void) const &;
|
|
|
|
const_iterator cend (void) const &;
|
2016-06-28 14:15:19 +10:00
|
|
|
|
2016-06-28 12:21:30 +10:00
|
|
|
private:
|
|
|
|
uint8_t *m_data;
|
2016-10-25 19:57:13 +11:00
|
|
|
size_t m_size;
|
2014-12-19 18:28:50 +11:00
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2014-12-19 18:28:50 +11:00
|
|
|
|
|
|
|
typedef detail::posix::mapped_file mapped_file;
|
2021-04-14 15:35:49 +10:00
|
|
|
}
|