buffer/paged: simplify paged buffer
This commit is contained in:
parent
3925431838
commit
4643beac82
@ -20,27 +20,20 @@ using cruft::memory::buffer::paged;
|
|||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
paged::paged (size_t bytes, size_t _window):
|
paged::paged (size_t bytes)
|
||||||
m_window (round_up (_window, pagesize ()))
|
: m_data (nullptr)
|
||||||
{
|
{
|
||||||
|
auto const allocated_bytes = round_up (bytes, pagesize ());
|
||||||
|
|
||||||
// reserve the address region with no access permissions
|
// reserve the address region with no access permissions
|
||||||
m_begin = reinterpret_cast<char*> (
|
auto ptr = reinterpret_cast<value_type*> (
|
||||||
mmap (nullptr, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
|
mmap (nullptr, allocated_bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (m_begin == MAP_FAILED)
|
if (ptr == MAP_FAILED)
|
||||||
posix::error::throw_code ();
|
posix::error::throw_code ();
|
||||||
|
|
||||||
// remap the initial window with read/write permissions
|
m_data = { ptr, allocated_bytes };
|
||||||
m_cursor = m_begin + round_up (min (m_window, bytes), pagesize ());
|
|
||||||
if (MAP_FAILED == mmap (m_begin,
|
|
||||||
m_cursor - m_begin,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0))
|
|
||||||
posix::error::throw_code ();
|
|
||||||
|
|
||||||
// record the nominal end address
|
|
||||||
m_end = m_begin + round_up (bytes, pagesize ());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -48,112 +41,59 @@ paged::paged (size_t bytes, size_t _window):
|
|||||||
paged::~paged ()
|
paged::~paged ()
|
||||||
{
|
{
|
||||||
// ignore errors in production; we don't want to double throw.
|
// ignore errors in production; we don't want to double throw.
|
||||||
auto res = munmap (m_begin, m_end - m_begin);
|
auto res = munmap (m_data.data (), capacity ());
|
||||||
(void)res;
|
(void)res;
|
||||||
CHECK_ZERO (res);
|
CHECK_ZERO (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
char*
|
paged::value_type *
|
||||||
paged::begin (void)&
|
paged::begin (void)&
|
||||||
{
|
{
|
||||||
return m_begin;
|
return m_data.begin ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
char*
|
paged::value_type *
|
||||||
paged::end (void)&
|
paged::end (void)&
|
||||||
{
|
{
|
||||||
return m_end;
|
return m_data.end ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
void
|
void
|
||||||
paged::access (char *cursor)
|
paged::commit (cruft::view<value_type*> region)
|
||||||
{
|
{
|
||||||
if (cursor < m_cursor)
|
apply_prot (region, PROT_READ | PROT_WRITE);
|
||||||
release (cursor);
|
|
||||||
else
|
|
||||||
commit (cursor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void
|
void
|
||||||
paged::commit (char *cursor)
|
paged::release (cruft::view<value_type*> region)
|
||||||
{
|
{
|
||||||
// bail if it's already mapped
|
apply_prot (region, PROT_NONE);
|
||||||
if (cursor <= m_cursor)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (cursor > m_end || cursor < m_begin)
|
|
||||||
throw std::out_of_range ("invalid commit cursor");
|
|
||||||
|
|
||||||
// bump the request up to page aligned and tack on a little to amortize
|
|
||||||
// syscall overheads
|
|
||||||
cursor = align (cursor, pagesize ()) + m_window;
|
|
||||||
cursor = min (cursor, m_end);
|
|
||||||
|
|
||||||
if (MAP_FAILED == mmap (m_cursor,
|
|
||||||
cursor - m_cursor,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
|
|
||||||
-1, 0))
|
|
||||||
posix::error::throw_code ();
|
|
||||||
|
|
||||||
m_cursor = cursor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void
|
void
|
||||||
paged::release (char *desired)
|
paged::apply_prot (cruft::view<value_type*> region, int prot)
|
||||||
{
|
{
|
||||||
if (desired > m_end || desired < m_begin)
|
if (!covers (m_data, region))
|
||||||
throw std::out_of_range ("invalid release cursor");
|
throw std::out_of_range ("invalid commit region");
|
||||||
|
|
||||||
align (desired, pagesize ());
|
// bump the request up to page aligned
|
||||||
|
static_assert (sizeof (value_type) == 1);
|
||||||
|
auto const alignment = pagesize ();
|
||||||
|
auto const first = align_down (region.begin (), alignment);
|
||||||
|
auto const last = align_up (region.end (), alignment);
|
||||||
|
|
||||||
// bail if the region is alread unmapped, or if it's not sufficiently
|
if (MAP_FAILED == mmap (first, last - first, prot,
|
||||||
// behind the current cursor.
|
|
||||||
if (desired >= m_cursor || cruft::cast::sign<size_t> (m_cursor - desired) < m_window)
|
|
||||||
return;
|
|
||||||
|
|
||||||
desired += m_window;
|
|
||||||
|
|
||||||
if (MAP_FAILED == mmap (desired,
|
|
||||||
m_end - desired,
|
|
||||||
PROT_NONE,
|
|
||||||
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
|
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
|
||||||
-1, 0))
|
-1, 0))
|
||||||
posix::error::throw_code ();
|
posix::error::throw_code ();
|
||||||
|
|
||||||
m_cursor = desired;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
size_t
|
|
||||||
paged::size (void) const
|
|
||||||
{
|
|
||||||
return m_cursor - m_begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
size_t
|
|
||||||
paged::capacity (void) const
|
|
||||||
{
|
|
||||||
return m_end - m_begin;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
|
||||||
size_t
|
|
||||||
paged::window (void) const
|
|
||||||
{
|
|
||||||
return m_window;
|
|
||||||
}
|
}
|
||||||
|
@ -6,46 +6,55 @@
|
|||||||
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __UTIL_MEMORY_BUFFER_PAGED_HPP
|
#pragma once
|
||||||
#define __UTIL_MEMORY_BUFFER_PAGED_HPP
|
|
||||||
|
#include "../../std.hpp"
|
||||||
|
#include "../../view.hpp"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
|
|
||||||
namespace cruft::memory::buffer {
|
namespace cruft::memory::buffer {
|
||||||
|
/// Implements a manually paged memory buffer of a fixed length.
|
||||||
|
///
|
||||||
|
/// It can be used to ensure a contiguous memory region of bounded size
|
||||||
|
/// is available at a later point in execution.
|
||||||
class paged {
|
class paged {
|
||||||
public:
|
public:
|
||||||
using value_type = char;
|
using value_type = u08;
|
||||||
|
|
||||||
paged (size_t bytes, size_t window);
|
paged (size_t bytes);
|
||||||
~paged ();
|
~paged ();
|
||||||
|
|
||||||
paged (const paged&) = delete;
|
paged (const paged&) = delete;
|
||||||
paged (paged &&) = delete;
|
paged (paged &&);
|
||||||
paged& operator= (const paged&) = delete;
|
paged& operator= (const paged&) = delete;
|
||||||
paged& operator= (paged &&) = delete;
|
paged& operator= (paged &&) = delete;
|
||||||
|
|
||||||
char* begin (void)&;
|
value_type* begin (void)&;
|
||||||
char* end (void)&;
|
value_type* end (void)&;
|
||||||
|
|
||||||
const char* cbegin (void)&;
|
const value_type* cbegin (void)&;
|
||||||
const char* cend (void)&;
|
const value_type* cend (void)&;
|
||||||
|
|
||||||
const char* begin (void) const&;
|
const value_type* begin (void) const&;
|
||||||
const char* end (void) const&;
|
const value_type* end (void) const&;
|
||||||
|
|
||||||
void access (char*);
|
void commit (value_type *ptr) { return commit ({ ptr, 1 }); }
|
||||||
|
void release (value_type *ptr) { return release ({ ptr, 1 }); }
|
||||||
|
|
||||||
size_t size (void) const;
|
/// Populates the memory range and ensures it is read/write.
|
||||||
size_t capacity (void) const;
|
void commit (cruft::view<value_type*>);
|
||||||
size_t window (void) const;
|
/// Returns the memory range to the system.
|
||||||
|
void release (cruft::view<value_type*>);
|
||||||
|
|
||||||
|
auto size (void) const { return m_data.size (); }
|
||||||
|
auto capacity (void) const { return m_data.size (); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void commit (char*);
|
/// Applies memory protection flags to the given memory region.
|
||||||
void release (char*);
|
void apply_prot (cruft::view<value_type*>, int prot);
|
||||||
|
|
||||||
char *m_begin, *m_end, *m_cursor;
|
cruft::view<value_type*> m_data;
|
||||||
size_t m_window;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
Loading…
Reference in New Issue
Block a user