m/b/simple: add simple buffer implementation

This commit is contained in:
Danny Robson 2018-12-17 15:20:55 +11:00
parent 1dc4b7b530
commit 37d68d5586
4 changed files with 111 additions and 0 deletions

View File

@ -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

28
memory/buffer/simple.cpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#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; }

54
memory/buffer/simple.hpp Normal file
View File

@ -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 <danny@nerdcruft.net>
*/
#pragma once
#include "../../std.hpp"
#include <cstddef>
#include <memory>
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<u08> m_base;
std::size_t m_size;
};
}

View File

@ -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 <danny@nerdcruft.net>
*/
#include "memory/buffer/simple.hpp"
#include <cstdlib>
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////
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;
}