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
|
|
|
*
|
|
|
|
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "io.hpp"
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "cast.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
#include "debug.hpp"
|
2016-10-02 15:50:13 +11:00
|
|
|
#include "posix/fd.hpp"
|
2017-12-18 15:46:52 +11:00
|
|
|
#include "posix/except.hpp"
|
2014-12-19 18:28:50 +11:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::detail::posix::mapped_file;
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2016-10-08 17:18:04 +11:00
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2018-12-05 19:12:03 +11:00
|
|
|
mapped_file::mapped_file (const std::filesystem::path &path,
|
2017-07-19 17:20:51 +10:00
|
|
|
int fflags,
|
|
|
|
int mflags):
|
2018-08-05 14:42:02 +10:00
|
|
|
mapped_file (cruft::posix::fd (path, fflags), mflags)
|
2016-08-02 18:51:04 +10:00
|
|
|
{ ; }
|
2014-12-19 18:28:50 +11:00
|
|
|
|
|
|
|
|
2016-08-02 18:51:04 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2018-08-05 14:42:02 +10:00
|
|
|
mapped_file::mapped_file (const ::cruft::posix::fd &src, int mflags)
|
2015-10-29 10:48:11 +11:00
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
struct stat meta;
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::try_value (fstat (src, &meta));
|
2014-12-19 18:28:50 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
m_size = cruft::cast::sign<size_t> (meta.st_size);
|
2018-07-05 13:44:34 +10:00
|
|
|
if (!m_size) {
|
|
|
|
m_data = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:32:08 +10:00
|
|
|
m_data = static_cast<uint8_t*> (mmap (nullptr, m_size, mflags, MAP_SHARED, src, 0));
|
2014-12-19 18:28:50 +11:00
|
|
|
if (m_data == MAP_FAILED)
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::throw_code ();
|
2015-10-29 10:48:11 +11:00
|
|
|
}
|
2014-12-19 18:28:50 +11:00
|
|
|
|
|
|
|
|
2019-01-17 19:49:45 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
mapped_file::mapped_file (cruft::mapped_file &&rhs) noexcept
|
|
|
|
: m_data (std::exchange (rhs.m_data, nullptr))
|
|
|
|
, m_size (std::exchange (rhs.m_size, 0))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2015-10-29 10:48:54 +11:00
|
|
|
mapped_file::~mapped_file ()
|
|
|
|
{
|
2018-07-05 13:44:34 +10:00
|
|
|
if (!m_data)
|
|
|
|
return;
|
|
|
|
|
2014-12-19 18:28:50 +11:00
|
|
|
munmap (m_data, m_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2016-10-25 19:57:13 +11:00
|
|
|
size_t
|
2016-04-27 17:06:09 +10:00
|
|
|
mapped_file::size (void) const
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-27 17:06:09 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool
|
|
|
|
mapped_file::empty (void) const
|
|
|
|
{
|
|
|
|
return size () == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-28 14:14:23 +10:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2014-12-19 18:28:50 +11:00
|
|
|
uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::data (void) &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
const uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::data (void) const &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::begin (void) &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return data ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::end (void) &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return data () + size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-28 14:14:23 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::begin (void) const &
|
2016-06-28 14:14:23 +10:00
|
|
|
{
|
|
|
|
return data ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::end (void) const &
|
2016-06-28 14:14:23 +10:00
|
|
|
{
|
|
|
|
return data () + size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
const uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::cbegin (void) const &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return data ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
const uint8_t*
|
2016-06-28 14:23:42 +10:00
|
|
|
mapped_file::cend (void) const &
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
return data () + size ();
|
|
|
|
}
|
|
|
|
|