diff --git a/CMakeLists.txt b/CMakeLists.txt index 01c3b03c..731ef023 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,6 +349,8 @@ list ( matrix3.cpp matrix4.cpp matrix.hpp + memory/buffer/simple.cpp + memory/buffer/simple.hpp memory/deleter.cpp memory/deleter.hpp nocopy.hpp @@ -567,6 +569,7 @@ if (TESTS) maths/fast matrix memory/deleter + memory/buffer/simple parallel/queue parse point diff --git a/memory/buffer/simple.cpp b/memory/buffer/simple.cpp new file mode 100644 index 00000000..29de296d --- /dev/null +++ b/memory/buffer/simple.cpp @@ -0,0 +1,28 @@ +/* + * 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/. + * + * Copyright 2018 Danny Robson + */ + +#include "simple.hpp" + +using cruft::memory::buffer::simple; + + +/////////////////////////////////////////////////////////////////////////////// +simple::simple (size_t _size) + : m_base (new u08[_size]) + , m_size (_size) +{ ; } + + +/////////////////////////////////////////////////////////////////////////////// +simple::value_type* simple::begin (void)& { return m_base.get (); } +simple::value_type* simple::end (void)& { return begin () + m_size; } + + +//----------------------------------------------------------------------------- +simple::value_type const* simple::begin (void) const& { return m_base.get (); } +simple::value_type const* simple::end (void) const& { return begin () + m_size; } diff --git a/memory/buffer/simple.hpp b/memory/buffer/simple.hpp new file mode 100644 index 00000000..a515bb5a --- /dev/null +++ b/memory/buffer/simple.hpp @@ -0,0 +1,54 @@ +/* + * 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/. + * + * Copyright 2018 Danny Robson + */ + +#pragma once + +#include "../../std.hpp" + +#include +#include + + +namespace cruft::memory::buffer { + /// Implements a trivial statically allocated memory buffer with a fixed + /// construction time and no other fancy tricks (just something trivial + /// like new/delete). + /// + /// The actual allocation size is guaranteed to be _at least_ the + /// specified size. It may be larger if the underlying implementation + /// requires page alignment or other system specific details. + /// + /// Useful as a trivial universal implementation of the buffer concept. + class simple { + public: + using value_type = u08; + + explicit simple (size_t _size); + + simple (const simple&) = delete; + simple (simple &&) = default; + simple& operator= (const simple&) = delete; + simple& operator= (simple &&); + + value_type* begin (void)&; + value_type* end (void)&; + + value_type const* cbegin (void)&; + value_type const* cend (void)&; + + value_type const* begin (void) const&; + value_type const* end (void) const&; + + std::size_t size (void) const; + std::size_t capacity (void) const; + + private: + std::unique_ptr m_base; + std::size_t m_size; + }; +} diff --git a/test/memory/buffer/simple.cpp b/test/memory/buffer/simple.cpp new file mode 100644 index 00000000..17fd239a --- /dev/null +++ b/test/memory/buffer/simple.cpp @@ -0,0 +1,26 @@ +/* + * 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/. + * + * Copyright 2018 Danny Robson + */ + +#include "memory/buffer/simple.hpp" + +#include +#include + + +/////////////////////////////////////////////////////////////////////////////// +int +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); + std::fill (std::begin (buf), std::end (buf), 0xff); + + return EXIT_SUCCESS; +} \ No newline at end of file