2015-11-11 16:56:35 +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/.
|
2015-11-11 16:56:35 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "paged.hpp"
|
2015-11-11 16:56:35 +11:00
|
|
|
|
2018-12-19 20:22:18 +11:00
|
|
|
#include "../cast.hpp"
|
|
|
|
#include "../maths.hpp"
|
|
|
|
#include "../pointer.hpp"
|
|
|
|
#include "../posix/except.hpp"
|
|
|
|
#include "../memory/system.hpp"
|
2015-11-11 16:56:35 +11:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2018-12-19 20:22:18 +11:00
|
|
|
using cruft::buffer::paged;
|
2015-11-11 16:56:35 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::paged (size_t bytes)
|
|
|
|
: m_data (nullptr)
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-19 20:22:18 +11:00
|
|
|
auto const allocated_bytes = round_up (bytes, memory::pagesize ());
|
2018-12-17 14:47:11 +11:00
|
|
|
|
2015-11-11 16:56:35 +11:00
|
|
|
// reserve the address region with no access permissions
|
2018-12-17 14:47:11 +11:00
|
|
|
auto ptr = reinterpret_cast<value_type*> (
|
|
|
|
mmap (nullptr, allocated_bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
|
2015-11-11 16:56:35 +11:00
|
|
|
);
|
|
|
|
|
2018-12-17 14:47:11 +11:00
|
|
|
if (ptr == MAP_FAILED)
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::throw_code ();
|
2015-11-11 16:56:35 +11:00
|
|
|
|
2018-12-17 14:47:11 +11:00
|
|
|
m_data = { ptr, allocated_bytes };
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
paged::~paged ()
|
|
|
|
{
|
|
|
|
// ignore errors in production; we don't want to double throw.
|
2018-12-17 14:47:11 +11:00
|
|
|
auto res = munmap (m_data.data (), capacity ());
|
2015-11-11 16:56:35 +11:00
|
|
|
(void)res;
|
|
|
|
CHECK_ZERO (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::value_type *
|
2016-03-17 18:05:28 +11:00
|
|
|
paged::begin (void)&
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-17 14:47:11 +11:00
|
|
|
return m_data.begin ();
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::value_type *
|
2016-03-17 18:05:28 +11:00
|
|
|
paged::end (void)&
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-17 14:47:11 +11:00
|
|
|
return m_data.end ();
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::commit (cruft::view<value_type*> region)
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-17 14:47:11 +11:00
|
|
|
apply_prot (region, PROT_READ | PROT_WRITE);
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::release (cruft::view<value_type*> region)
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-17 14:47:11 +11:00
|
|
|
apply_prot (region, PROT_NONE);
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
2018-12-17 14:47:11 +11:00
|
|
|
paged::apply_prot (cruft::view<value_type*> region, int prot)
|
2015-11-11 16:56:35 +11:00
|
|
|
{
|
2018-12-17 14:47:11 +11:00
|
|
|
if (!covers (m_data, region))
|
|
|
|
throw std::out_of_range ("invalid commit region");
|
2015-11-11 16:56:35 +11:00
|
|
|
|
2018-12-17 14:47:11 +11:00
|
|
|
// bump the request up to page aligned
|
|
|
|
static_assert (sizeof (value_type) == 1);
|
2018-12-19 20:22:18 +11:00
|
|
|
auto const alignment = memory::pagesize ();
|
2018-12-19 17:16:57 +11:00
|
|
|
auto const first = align::down (region.begin (), alignment);
|
|
|
|
auto const last = align::up (region.end (), alignment);
|
2015-11-11 16:56:35 +11:00
|
|
|
|
2018-12-17 14:47:11 +11:00
|
|
|
if (MAP_FAILED == mmap (first, last - first, prot,
|
2015-11-11 16:56:35 +11:00
|
|
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
|
2016-05-12 17:42:21 +10:00
|
|
|
-1, 0))
|
2017-12-18 15:46:52 +11:00
|
|
|
posix::error::throw_code ();
|
2015-11-11 16:56:35 +11:00
|
|
|
}
|