libcruft-util/io_posix.cpp

143 lines
3.3 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 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 util::detail::posix::mapped_file;
2015-10-29 10:48:11 +11:00
//////////////////////////////////////////////////////////////////////////////
mapped_file::mapped_file (const std::experimental::filesystem::path &path,
int fflags,
int mflags):
mapped_file (util::posix::fd (path, fflags), mflags)
{ ; }
//-----------------------------------------------------------------------------
mapped_file::mapped_file (const ::util::posix::fd &src, int mflags)
2015-10-29 10:48:11 +11:00
{
struct stat meta;
::util::posix::error::try_value (fstat (src, &meta));
m_size = util::cast::sign<size_t> (meta.st_size);
2018-05-03 18:32:08 +10:00
m_data = static_cast<uint8_t*> (mmap (nullptr, m_size, mflags, MAP_SHARED, src, 0));
if (m_data == MAP_FAILED)
::util::posix::error::throw_code ();
2015-10-29 10:48:11 +11:00
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
2015-10-29 10:48:54 +11:00
mapped_file::~mapped_file ()
{
CHECK (m_data != NULL);
munmap (m_data, m_size);
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
size_t
mapped_file::size (void) const
{
return m_size;
}
//-----------------------------------------------------------------------------
bool
mapped_file::empty (void) const
{
return size () == 0;
}
2016-06-28 14:14:23 +10:00
//////////////////////////////////////////////////////////////////////////////
uint8_t*
mapped_file::data (void) &
{
CHECK (m_size > 0);
CHECK (m_data != NULL);
return m_data;
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::data (void) const &
{
CHECK (m_size > 0);
CHECK (m_data != NULL);
return m_data;
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
uint8_t*
mapped_file::begin (void) &
{
return data ();
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
uint8_t*
mapped_file::end (void) &
{
return data () + size ();
}
2016-06-28 14:14:23 +10:00
//-----------------------------------------------------------------------------
const uint8_t*
mapped_file::begin (void) const &
2016-06-28 14:14:23 +10:00
{
return data ();
}
//-----------------------------------------------------------------------------
const uint8_t*
mapped_file::end (void) const &
2016-06-28 14:14:23 +10:00
{
return data () + size ();
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::cbegin (void) const &
{
return data ();
}
2015-01-07 16:00:12 +11:00
//----------------------------------------------------------------------------
const uint8_t*
mapped_file::cend (void) const &
{
return data () + size ();
}