2014-12-19 18:28:50 +11:00
|
|
|
/*
|
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
|
2014-12-19 18:28:50 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-12-19 18:28:50 +11:00
|
|
|
*
|
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.
|
2014-12-19 18:28:50 +11:00
|
|
|
*
|
|
|
|
* Copyright 2010-2014 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "io.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "except.hpp"
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
using util::detail::posix::mapped_file;
|
|
|
|
|
2015-10-29 10:48:11 +11:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
mapped_file::mapped_file (const char *_path, int fflags, int mflags):
|
|
|
|
m_fd (_path, fflags)
|
|
|
|
{
|
2016-04-27 17:02:27 +10:00
|
|
|
try {
|
|
|
|
load_fd (mflags);
|
|
|
|
} catch (const errno_error &e) {
|
|
|
|
// ignore zero length mapping error
|
|
|
|
if (e.code () == EINVAL && m_size == 0)
|
|
|
|
return;
|
|
|
|
}
|
2015-10-29 10:48:11 +11:00
|
|
|
}
|
2014-12-19 18:28:50 +11:00
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2015-10-29 10:48:54 +11:00
|
|
|
mapped_file::~mapped_file ()
|
|
|
|
{
|
2014-12-19 18:28:50 +11:00
|
|
|
CHECK (m_data != NULL);
|
|
|
|
munmap (m_data, m_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +11:00
|
|
|
void
|
2015-10-29 10:48:11 +11:00
|
|
|
mapped_file::load_fd (int mflags) {
|
2014-12-19 18:28:50 +11:00
|
|
|
struct stat meta;
|
|
|
|
if (fstat (m_fd, &meta) < 0)
|
|
|
|
throw errno_error ();
|
|
|
|
|
|
|
|
m_size = (size_t)meta.st_size;
|
2015-10-29 10:48:11 +11:00
|
|
|
m_data = (uint8_t *)mmap (NULL, m_size, mflags, MAP_SHARED, m_fd, 0);
|
2014-12-19 18:28:50 +11:00
|
|
|
if (m_data == MAP_FAILED)
|
|
|
|
throw errno_error ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-07 16:00:12 +11:00
|
|
|
//----------------------------------------------------------------------------
|
2014-12-19 18:28:50 +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
|
|
|
CHECK (m_size > 0);
|
|
|
|
CHECK (m_data != NULL);
|
|
|
|
|
|
|
|
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
|
|
|
CHECK (m_size > 0);
|
|
|
|
CHECK (m_data != NULL);
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
|