libcruft-util/io_posix.cpp

143 lines
3.1 KiB
C++

/*
* 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/.
*
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
*/
#include "io.hpp"
#include "cast.hpp"
#include "debug.hpp"
#include "posix/fd.hpp"
#include "posix/except.hpp"
#include <sys/stat.h>
using cruft::detail::posix::mapped_file;
//////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const std::filesystem::path &path,
int fflags,
int mflags):
mapped_file (cruft::posix::fd (path, fflags), mflags)
{ ; }
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const ::cruft::posix::fd &src, int mflags)
{
struct stat meta;
::cruft::posix::error::try_value (fstat (src, &meta));
m_size = cruft::cast::sign<size_t> (meta.st_size);
if (!m_size) {
m_data = nullptr;
return;
}
m_data = static_cast<uint8_t*> (mmap (nullptr, m_size, mflags, MAP_SHARED, src, 0));
if (m_data == MAP_FAILED)
::cruft::posix::error::throw_code ();
}
//-----------------------------------------------------------------------------
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))
{ ; }
//----------------------------------------------------------------------------
mapped_file::~mapped_file ()
{
if (!m_data)
return;
munmap (m_data, m_size);
}
//----------------------------------------------------------------------------
size_t
mapped_file::size (void) const
{
return m_size;
}
//-----------------------------------------------------------------------------
bool
mapped_file::empty (void) const
{
return size () == 0;
}
//////////////////////////////////////////////////////////////////////////////
uint8_t*
mapped_file::data (void) &
{
return m_data;
}
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::data (void) const &
{
return m_data;
}
//----------------------------------------------------------------------------
uint8_t*
mapped_file::begin (void) &
{
return data ();
}
//----------------------------------------------------------------------------
uint8_t*
mapped_file::end (void) &
{
return data () + size ();
}
//-----------------------------------------------------------------------------
const uint8_t*
mapped_file::begin (void) const &
{
return data ();
}
//-----------------------------------------------------------------------------
const uint8_t*
mapped_file::end (void) const &
{
return data () + size ();
}
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::cbegin (void) const &
{
return data ();
}
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::cend (void) const &
{
return data () + size ();
}