2016-10-02 16:18:41 +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/.
|
2016-10-02 16:18:41 +11:00
|
|
|
*
|
|
|
|
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "map.hpp"
|
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include "./except.hpp"
|
2016-10-02 16:18:41 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::posix::map;
|
2016-10-02 16:18:41 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
map::map (size_t size, int prot, int flags):
|
|
|
|
m_addr (mmap (nullptr, size, prot, flags, -1, 0)),
|
|
|
|
m_size (size)
|
|
|
|
{
|
|
|
|
if (MAP_FAILED == m_addr)
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::throw_code ();
|
2016-10-02 16:18:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
map::map (size_t size, int prot, int flags, const fd &src, off_t offset):
|
|
|
|
m_addr (mmap (nullptr, size, prot, flags, src, offset)),
|
|
|
|
m_size (size)
|
|
|
|
{
|
|
|
|
if (MAP_FAILED == m_addr)
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::throw_code ();
|
2016-10-02 16:18:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-07-24 15:48:50 +10:00
|
|
|
map::map (map &&rhs) noexcept:
|
2016-10-02 16:18:41 +11:00
|
|
|
m_addr (MAP_FAILED),
|
|
|
|
m_size (rhs.m_size)
|
|
|
|
{
|
|
|
|
std::swap (m_addr, rhs.m_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
map::~map ()
|
|
|
|
{
|
|
|
|
if (MAP_FAILED == m_addr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (munmap (m_addr, m_size))
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::throw_code ();
|
2016-10-02 16:18:41 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
map&
|
2018-07-24 15:48:50 +10:00
|
|
|
map::operator= (map &&rhs) noexcept
|
2016-10-02 16:18:41 +11:00
|
|
|
{
|
|
|
|
std::swap (m_addr, rhs.m_addr);
|
|
|
|
std::swap (m_size, rhs.m_size);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
uint8_t*
|
|
|
|
map::begin (void)
|
|
|
|
{
|
|
|
|
return static_cast<uint8_t*> (m_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
uint8_t*
|
|
|
|
map::end (void)
|
|
|
|
{
|
|
|
|
return static_cast<uint8_t*> (m_addr) + m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void*
|
|
|
|
map::data (void)
|
|
|
|
{
|
|
|
|
return m_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const void*
|
|
|
|
map::data (void) const
|
|
|
|
{
|
|
|
|
return m_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-10-02 16:39:47 +11:00
|
|
|
bool
|
|
|
|
map::empty (void) const
|
|
|
|
{
|
|
|
|
return size () == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-10-02 16:18:41 +11:00
|
|
|
size_t
|
|
|
|
map::size (void) const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
map::resize (size_t newsize, resize_t op)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
switch (op) {
|
|
|
|
case resize_t::MOVE:
|
|
|
|
flags = flags | MREMAP_MAYMOVE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case resize_t::NOMOVE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto res = mremap (m_addr, m_size, newsize, flags);
|
|
|
|
if (res == MAP_FAILED)
|
2018-08-05 14:42:02 +10:00
|
|
|
::cruft::posix::error::throw_code ();
|
2016-10-02 16:18:41 +11:00
|
|
|
}
|