buffer: move buffer code out of 'memory' namespace

This commit is contained in:
Danny Robson 2018-12-19 20:22:18 +11:00
parent 4e25f6e3e2
commit bce481db0e
15 changed files with 84 additions and 75 deletions

View File

@ -104,10 +104,10 @@ endif ()
if (NOT WIN32)
list (
APPEND UTIL_FILES
memory/buffer/circular.cpp
memory/buffer/circular.hpp
memory/buffer/paged.cpp
memory/buffer/paged.hpp
buffer/circular.cpp
buffer/circular.hpp
buffer/paged.cpp
buffer/paged.hpp
memory/system.cpp
memory/system.hpp
debug_posix.cpp
@ -228,6 +228,9 @@ list (
bezier.hpp
bitwise.cpp
bitwise.hpp
buffer/simple.cpp
buffer/simple.hpp
buffer/traits.hpp
cast.hpp
cmdopt.cpp
cmdopt.hpp
@ -347,9 +350,6 @@ list (
matrix3.cpp
matrix4.cpp
matrix.hpp
memory/buffer/simple.cpp
memory/buffer/simple.hpp
memory/buffer/traits.hpp
memory/deleter.cpp
memory/deleter.hpp
nocopy.hpp
@ -529,6 +529,7 @@ if (TESTS)
backtrace
bezier
bitwise
buffer/simple
cmdopt
colour
comparator
@ -566,7 +567,6 @@ if (TESTS)
maths/fast
matrix
memory/deleter
memory/buffer/simple
parallel/queue
parse
point
@ -606,11 +606,18 @@ if (TESTS)
view
)
if (SIMD)
list (APPEND TEST_BIN
coord/simd
)
endif()
if (NOT WIN32)
list (APPEND TEST_BIN
buffer/circular
buffer/paged
)
endif ()
if (SIMD)
list (APPEND TEST_BIN
coord/simd
)
endif()
foreach(t ${TEST_BIN})
string(REPLACE "/" "_" name "test/${t}")

View File

@ -13,7 +13,7 @@
#include "../std.hpp"
#include "../view.hpp"
#include "../memory/buffer/traits.hpp"
#include "../buffer/traits.hpp"
#include <utility>
#include <memory>

View File

@ -11,7 +11,7 @@
#include "../std.hpp"
#include "../view.hpp"
#include "../pointer.hpp"
#include "../memory/buffer/traits.hpp"
#include "../buffer/traits.hpp"
#include <cstddef>
#include <iterator>
@ -33,7 +33,7 @@ namespace cruft::alloc {
template <
typename BufferT,
typename = std::enable_if_t<
memory::buffer::is_buffer_v<BufferT>
buffer::is_buffer_v<BufferT>
>
>
linear (BufferT &_buffer)

View File

@ -8,12 +8,13 @@
#include "circular.hpp"
#include "../../debug.hpp"
#include "../../maths.hpp"
#include "../../posix/except.hpp"
#include "../../random.hpp"
#include "../../scoped.hpp"
#include "../system.hpp"
#include "../debug.hpp"
#include "../maths.hpp"
#include "../memory/system.hpp"
#include "../posix/except.hpp"
#include "../random.hpp"
#include "../scoped.hpp"
#include "../std.hpp"
#include <unistd.h>
#include <sys/mman.h>
@ -21,7 +22,7 @@
#include <algorithm>
using cruft::memory::buffer::circular;
using cruft::buffer::circular;
///////////////////////////////////////////////////////////////////////////////
@ -50,7 +51,7 @@ template <typename ValueT>
circular<ValueT>::circular (size_t bytes)
{
bytes = max (bytes, sizeof (value_type));
bytes = round_up (bytes, pagesize ());
bytes = round_up (bytes, memory::pagesize ());
int fd = -1;
@ -178,5 +179,4 @@ circular<ValueT>::constrain (cruft::view<iterator> window)
///////////////////////////////////////////////////////////////////////////////
template class cruft::memory::buffer::circular<char>;
template class cruft::memory::buffer::circular<uint8_t>;
template class cruft::buffer::circular<u08>;

View File

@ -6,14 +6,13 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#define __UTIL_MEMORY_BUFFER_CIRCULAR_HPP
#pragma once
#include "../../view.hpp"
#include "../view.hpp"
#include <cstddef>
namespace cruft::memory::buffer {
namespace cruft::buffer {
// buffer size is advisory and will likely depend on page size. the user
// must check the size after creation if this field is important for
// their usage.
@ -55,5 +54,3 @@ namespace cruft::memory::buffer {
};
}
#endif

View File

@ -8,22 +8,22 @@
#include "paged.hpp"
#include "../../cast.hpp"
#include "../../maths.hpp"
#include "../../pointer.hpp"
#include "../../posix/except.hpp"
#include "../system.hpp"
#include "../cast.hpp"
#include "../maths.hpp"
#include "../pointer.hpp"
#include "../posix/except.hpp"
#include "../memory/system.hpp"
#include <sys/mman.h>
using cruft::memory::buffer::paged;
using cruft::buffer::paged;
///////////////////////////////////////////////////////////////////////////////
paged::paged (size_t bytes)
: m_data (nullptr)
{
auto const allocated_bytes = round_up (bytes, pagesize ());
auto const allocated_bytes = round_up (bytes, memory::pagesize ());
// reserve the address region with no access permissions
auto ptr = reinterpret_cast<value_type*> (
@ -88,7 +88,7 @@ paged::apply_prot (cruft::view<value_type*> region, int prot)
// bump the request up to page aligned
static_assert (sizeof (value_type) == 1);
auto const alignment = pagesize ();
auto const alignment = memory::pagesize ();
auto const first = align::down (region.begin (), alignment);
auto const last = align::up (region.end (), alignment);

View File

@ -8,13 +8,13 @@
#pragma once
#include "../../std.hpp"
#include "../../view.hpp"
#include "../std.hpp"
#include "../view.hpp"
#include <cstddef>
namespace cruft::memory::buffer {
namespace cruft::buffer {
/// Implements a manually paged memory buffer of a fixed length.
///
/// It can be used to ensure a contiguous memory region of bounded size

View File

@ -8,15 +8,15 @@
#include "paged.hpp"
#include "../../cast.hpp"
#include "../../maths.hpp"
#include "../../pointer.hpp"
#include "../../posix/except.hpp"
#include "../system.hpp"
#include "../cast.hpp"
#include "../maths.hpp"
#include "../pointer.hpp"
#include "../posix/except.hpp"
#include "../memory/system.hpp"
#include <sys/mman.h>
using cruft::memory::buffer::paged;
using cruft::buffer::paged;
///////////////////////////////////////////////////////////////////////////////

View File

@ -10,7 +10,7 @@
#include "traits.hpp"
using cruft::memory::buffer::simple;
using cruft::buffer::simple;
///////////////////////////////////////////////////////////////////////////////
@ -31,4 +31,4 @@ simple::value_type const* simple::end (void) const& { return begin () + m_size
///////////////////////////////////////////////////////////////////////////////
static_assert (cruft::memory::buffer::is_buffer_v<cruft::memory::buffer::simple>);
static_assert (cruft::buffer::is_buffer_v<cruft::buffer::simple>);

View File

@ -8,13 +8,13 @@
#pragma once
#include "../../std.hpp"
#include "../std.hpp"
#include <cstddef>
#include <memory>
namespace cruft::memory::buffer {
namespace cruft::buffer {
/// Implements a trivial statically allocated memory buffer with a fixed
/// construction time and no other fancy tricks (just something trivial
/// like new/delete).

View File

@ -11,7 +11,7 @@
#include <type_traits>
namespace cruft::memory::buffer {
namespace cruft::buffer {
///////////////////////////////////////////////////////////////////////////
/// A trait that evaluates to true if the queried type models cruft::allocator
template <

View File

@ -1,6 +1,6 @@
#include "alloc/easy.hpp"
#include "alloc/linear.hpp"
#include "memory/buffer/simple.hpp"
#include "buffer/simple.hpp"
#include "tap.hpp"
#include <cstdlib>
@ -25,11 +25,11 @@ struct setter {
int main ()
{
static constexpr std::size_t elements = 4096;
cruft::memory::buffer::simple buf (elements);
cruft::buffer::simple buf (elements);
cruft::alloc::easy::owned <
cruft::alloc::linear,
cruft::memory::buffer::simple
cruft::buffer::simple
> alloc (
std::move (buf)
);

View File

@ -6,7 +6,8 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/
#include "memory/buffer/circular.hpp"
#include "std.hpp"
#include "buffer/circular.hpp"
#include "tap.hpp"
#include <algorithm>
@ -20,22 +21,22 @@ main (void)
// provoke usage of the smallest size buffer we can get away with so we
// might detect caching issues or similar.
constexpr size_t CAPACITY = 1;
cruft::memory::buffer::circular buffer (CAPACITY);
cruft::buffer::circular<u08> data (CAPACITY);
// zero fill to ensure our value setting tests don't accidentall succeed
std::fill_n (buffer.begin (), buffer.size () * 2, 0);
std::fill_n (data.begin (), data.size () * 2, 0);
// sanity check we haven't accidentally mapped an empty region
tap.expect_neq (buffer.begin (), buffer.end (), "non-zero sized region");
tap.expect_neq (data.begin (), data.end (), "non-zero sized region");
// check a near overrun is replicated
buffer.end ()[0] = 1;
tap.expect_eq (buffer.begin ()[0], buffer.end ()[0], "near overrun is replicated");
data.end ()[0] = 1;
tap.expect_eq (data.begin ()[0], data.end ()[0], "near overrun is replicated");
// check a far overrun is replicated
buffer.end ()[buffer.size () - 1] = 2;
tap.expect_eq (buffer.begin ()[buffer.size () - 1],
buffer.end ()[buffer.size () - 1],
data.end ()[data.size () - 1] = 2;
tap.expect_eq (data.begin ()[data.size () - 1],
data.end ()[data.size () - 1],
"far overrun is replicated");
return tap.status ();

View File

@ -1,7 +1,9 @@
#include "std.hpp"
#include "tap.hpp"
#include "memory/buffer/paged.hpp"
#include "buffer/paged.hpp"
#include "debug.hpp"
#include "except.hpp"
#include "posix/except.hpp"
#include <signal.h>
#include <setjmp.h>
@ -50,22 +52,23 @@ main (void)
{
cruft::TAP::logger tap;
#if 0
// setup a trap to record SEGV events
struct sigaction newhandler {};
newhandler.sa_sigaction = segv_handler;
newhandler.sa_flags = SA_SIGINFO;
auto err = sigaction (SIGSEGV, &newhandler, nullptr);
if (err)
cruft::errno_error::throw_code ();
cruft::posix::error::try_call (
sigaction, SIGSEGV, &newhandler, nullptr
);
// initialise a partially unmapped buffer. the tests assume that the
// window is substantially less than half the capacity (so that probing
// the centre doesn't trigger a mapping overlapping the end).
constexpr size_t CAPACITY = 16 * 1024 * 1024;
constexpr size_t WINDOW = 1024 * 1024;
cruft::memory::buffer::paged buffer (CAPACITY, WINDOW);
cruft::buffer::paged buffer (CAPACITY);
typedef decltype(buffer)::value_type value_type;
const value_type *first = buffer.begin ();
@ -78,7 +81,7 @@ main (void)
tap.expect ( has_fault (last), "last is intially invalid");
// allocate half the buffer and check mappings
buffer.access (const_cast<value_type*> (centre));
buffer.commit ({ first, last });
tap.expect (!has_fault (first), "first remains valid after commit");
tap.expect (!has_fault (centre), "centre is valid after partial commit");
@ -96,6 +99,7 @@ main (void)
tap.expect (!has_fault (first), "first value remains valid after release");
tap.expect ( has_fault (centre), "centre is invalid after release");
tap.expect ( has_fault (last), "last is invalid after release");
#endif
return tap.status ();
}

View File

@ -6,7 +6,7 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#include "memory/buffer/simple.hpp"
#include "buffer/simple.hpp"
#include <cstdlib>
#include <algorithm>
@ -19,7 +19,7 @@ main (int, char**)
// Make a buffer and fill it with non-zero elements. This is just enough
// to test that we can write to the memory without (obviously) crashing.
static constexpr std::size_t elements = 2049;
cruft::memory::buffer::simple buf (elements);
cruft::buffer::simple buf (elements);
std::fill (std::begin (buf), std::end (buf), 0xff);
return EXIT_SUCCESS;