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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,7 +6,8 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#include "memory/buffer/circular.hpp" #include "std.hpp"
#include "buffer/circular.hpp"
#include "tap.hpp" #include "tap.hpp"
#include <algorithm> #include <algorithm>
@ -20,22 +21,22 @@ main (void)
// provoke usage of the smallest size buffer we can get away with so we // provoke usage of the smallest size buffer we can get away with so we
// might detect caching issues or similar. // might detect caching issues or similar.
constexpr size_t CAPACITY = 1; 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 // 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 // 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 // check a near overrun is replicated
buffer.end ()[0] = 1; data.end ()[0] = 1;
tap.expect_eq (buffer.begin ()[0], buffer.end ()[0], "near overrun is replicated"); tap.expect_eq (data.begin ()[0], data.end ()[0], "near overrun is replicated");
// check a far overrun is replicated // check a far overrun is replicated
buffer.end ()[buffer.size () - 1] = 2; data.end ()[data.size () - 1] = 2;
tap.expect_eq (buffer.begin ()[buffer.size () - 1], tap.expect_eq (data.begin ()[data.size () - 1],
buffer.end ()[buffer.size () - 1], data.end ()[data.size () - 1],
"far overrun is replicated"); "far overrun is replicated");
return tap.status (); return tap.status ();

View File

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

View File

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