alloc: remove unused allocators

This commit is contained in:
Danny Robson 2018-12-19 17:34:35 +11:00
parent 10d5c141f2
commit 39ed7b27e8
9 changed files with 0 additions and 523 deletions

View File

@ -195,17 +195,12 @@ list (
alloc/fwd.hpp
alloc/allocator.cpp
alloc/allocator.hpp
#alloc/arena.cpp
#alloc/arena.hpp
alloc/easy.hpp
#alloc/forwarding.cpp
#alloc/forwarding.hpp
alloc/raw/traits.hpp
alloc/raw/affix.cpp
alloc/raw/affix.hpp
alloc/raw/aligned/direct.hpp
alloc/raw/aligned/foreign.hpp
#alloc/raw/dynamic.hpp
alloc/raw/fallback.cpp
alloc/raw/fallback.hpp
alloc/raw/linear.cpp
@ -524,12 +519,9 @@ if (TESTS)
algo/sort
alloc/aligned/foreign
alloc/aligned/direct
#alloc/arena
#alloc/dynamic
alloc/easy
alloc/linear
alloc/stack
#alloc/forwarding
affine
array/darray
array/sarray

View File

@ -1 +0,0 @@
#include "arena.hpp"

View File

@ -1,140 +0,0 @@
/*
* 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 2015-2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_ALLOC_ARENA_HPP
#define CRUFT_UTIL_ALLOC_ARENA_HPP
#include "../memory/deleter.hpp"
#include "../cast.hpp"
#include "../view.hpp"
#include <memory>
#include <utility>
namespace cruft::alloc {
/// wraps a block allocator with an interface suitable for allocating
/// individual objects.
template <class T>
class arena {
public:
explicit arena (T &store):
m_store (store)
{ ; }
//---------------------------------------------------------------------
template <typename U, typename ...Args>
U*
acquire (Args&&... args)
{
U *data = m_store.template allocate<U> (1).data ();
try {
new (data) U (std::forward<Args> (args)...);
} catch (...) {
m_store.template deallocate<U> ({data,1});
throw;
}
return data;
}
//---------------------------------------------------------------------
template <typename U>
void
release (U *u)
{
u->~U ();
m_store.template deallocate<U> (cruft::view {u,1u});
}
//---------------------------------------------------------------------
template <typename U>
using deleter_t = cruft::memory::owner_deleter<
U,arena<T>,&arena::release
>;
template <typename U>
using unique_t = std::unique_ptr<U,deleter_t<U>>;
// the return type must be auto and the implementation must be inline
// otherwise we trigger an internal compiler error in gcc-5.2.0
// "sorry, unimplemented: mangling offset_ref"
template <typename U, typename ...Args>
auto
unique (Args&& ...args)
{
return unique_t<U> {
acquire<U> (std::forward<Args> (args)...),
deleter_t<U> (*this)
};
}
private:
T &m_store;
};
/// A simple allocator that contains a raw allocator and a forwarded
/// allocator.
///
/// The raw allocator handles the memory allocation, the forwarded
/// allocator performs the initialisation, and we control the construction
/// of both.
///
/// Ideally we wouldn't forward calls manually and instead do something
/// like inherit from arena<T>, but that makes it difficult to initialise
/// the raw allocator before we have to supply the reference to the arena.
template <typename AllocT>
class owned {
public:
template <typename ...ArgsT>
explicit owned (ArgsT &&...args)
: m_store (std::forward<ArgsT> (args)...)
, m_arena {m_store}
{ ; }
owned (owned &&rhs)
: m_store (std::move (rhs.m_store))
, m_arena (m_store)
{ ; }
owned& operator= (owned &&rhs)
{
m_store = std::move (rhs.m_store);
}
owned (owned const&) = delete;
owned& operator= (owned const&) = delete;
template <typename T, typename ...ArgsT>
decltype(auto) acquire (ArgsT &&...args)
{ return m_arena.template acquire<T,ArgsT...> (std::forward<ArgsT> (args)...); }
template <typename ...ArgsT>
decltype(auto) release (ArgsT &&...args)
{ return m_arena.release (std::forward<ArgsT> (args)...); }
template <typename T, typename ...ArgsT>
decltype(auto) unique (ArgsT &&...args)
{ return m_arena.template unique<T,ArgsT...> (std::forward<ArgsT> (args)...); }
template <typename ...Args>
decltype(auto) reset (Args&&...args)
{ return m_store.reset (std::forward<Args> (args)...); }
auto const& store (void) const& { return m_store; }
auto & store (void) & { return m_store; }
private:
AllocT m_store;
arena<AllocT> m_arena;
};
}
#endif

View File

@ -1,9 +0,0 @@
/*
* 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 "forwarding.hpp"

View File

@ -1,51 +0,0 @@
/*
* 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>
*/
#ifndef CRUFT_UTIL_ALLOC_FORWARDING_HPP
#define CRUFT_UTIL_ALLOC_FORWARDING_HPP
#include <utility>
namespace cruft::alloc {
template <typename BackingT>
class forwarding {
public:
explicit forwarding (BackingT &backing):
m_backing (backing)
{ ; }
template <typename T, typename ...Args>
auto allocate (Args &&...args)
{
return m_backing.template allocate<T> (std::forward<Args> (args)...);
}
template <typename T, typename ...Args>
auto deallocate (Args &&...args)
{
return m_backing.template deallocate<T> (std::forward<Args> (args)...);
}
auto data (void) { return m_backing.data (); }
auto begin (void) { return m_backing.begin (); }
auto end (void) { return m_backing.end (); }
auto data (void) const { return m_backing.data (); }
auto begin (void) const { return m_backing.begin (); }
auto end (void) const { return m_backing.end (); }
auto capacity (void) const { return m_backing.capacity (); }
auto used (void) const { return m_backing.used (); }
auto remain (void) const { return m_backing.remain (); }
private:
BackingT &m_backing;
};
};
#endif

View File

@ -1,209 +0,0 @@
/*
* 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 2016-2018 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP
#define CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP
#include "traits.hpp"
#include <cstddef>
#include <memory>
namespace cruft::alloc::raw {
// wraps an allocator given at construction time, forwarding all calls to
// the inner object. used to allow virtual dispatch of the non-virtual
// allocator interface.
class dynamic {
public:
struct alignment_unsupported : public std::exception {};
//---------------------------------------------------------------------
// disable copying, but allow moving (required for calls to 'make')
dynamic (const dynamic&) = delete;
dynamic& operator= (const dynamic&) = delete;
dynamic (dynamic &&rhs) = default;
dynamic& operator= (dynamic&&) = default;
//---------------------------------------------------------------------
// construct an inner wrapper for type T. used to get around lack of
// ambiguous template constructors.
template <typename T, typename ...Args>
static dynamic
make (Args &&...args)
{
return dynamic (
std::make_unique<child<T>> (
std::forward<Args> (args)...
)
);
}
//---------------------------------------------------------------------
// if aligned allocation is not exposed by the child then we will
// unconditionally throw if it is ever called. unfortunately we can't
// dynamically eliminate the function altogether given run-time
// dynamic dispatch needs the common calls exposed to the clients, and
// aligned allocate is stupid useful.
template <typename T>
auto allocate (size_t bytes) { return m_child->allocate<T> (bytes); }
template <typename T>
auto allocate (size_t bytes, size_t alignment) { return m_child->allocate<T> (bytes, alignment); }
auto deallocate (void *ptr, size_t bytes)
{ return m_child->deallocate (ptr, bytes); }
auto deallocate (void *ptr, size_t bytes, size_t alignment)
{ return m_child->deallocate (ptr, bytes, alignment); }
//---------------------------------------------------------------------
auto begin (void) { return m_child->begin (); }
auto begin (void) const { return m_child->begin (); }
auto offset (const void *ptr) const
{ return m_child->offset (ptr); }
//---------------------------------------------------------------------
auto reset (void) { return m_child->reset (); }
//---------------------------------------------------------------------
// capacity queries
auto capacity (void) const { return m_child->capacity (); }
auto used (void) const { return m_child->used (); }
auto remain (void) const { return m_child->remain (); }
private:
// Internal base for arbitrary allocator types. Necessary for
// type ellision in super-client classes.
class interface {
public:
interface () = default;
interface (const interface&) = delete;
interface (interface&&) = delete;
interface& operator= (const interface&) = delete;
interface& operator= (interface&&) = delete;
virtual ~interface () { ; }
// allocation management
virtual cruft::view<std::byte*> allocate (size_t bytes) = 0;
virtual cruft::view<std::byte*> allocate (size_t bytes, size_t alignment) = 0;
virtual void deallocate (void *ptr, size_t bytes) = 0;
virtual void deallocate (void *ptr, size_t bytes, size_t alignment) = 0;
virtual std::byte* begin (void) = 0;
virtual const std::byte* begin (void) const = 0;
virtual std::byte* end (void) = 0;
virtual const std::byte* end (void) const = 0;
virtual size_t offset (const void*) const = 0;
virtual void reset (void) = 0;
// capacity queries
virtual size_t capacity (void) const = 0;
virtual size_t used (void) const = 0;
virtual size_t remain (void) const = 0;
};
template <typename ChildT>
class child final : public interface {
public:
struct _alignment_unsupported : public alignment_unsupported { };
template <typename ...Args>
child (Args &&...args):
interface (),
m_target (std::forward<Args> (args)...)
{ ; }
// allocation management
cruft::view<std::byte*>
allocate (size_t bytes) override
{ return m_target.allocate (bytes); }
// we can't totally eliminate this call given the point is to
// expose the common API area, but we will throw if the operation
// is unsupported in the child.
cruft::view<std::byte*>
allocate (size_t bytes, size_t alignment) override
{
if constexpr (has_aligned_allocate_v<ChildT>) {
return m_target.allocate (bytes, alignment);
} else {
(void)bytes;
(void)alignment;
throw _alignment_unsupported ();
}
}
void
deallocate (void *ptr, size_t bytes) override
{ m_target.deallocate (ptr, bytes); }
void
deallocate (void *ptr, size_t bytes, size_t alignment) override
{
if constexpr (has_aligned_allocate_v<ChildT>) {
m_target.deallocate (ptr, bytes, alignment);
} else {
(void)ptr;
(void)bytes;
(void)alignment;
throw _alignment_unsupported ();
}
}
const std::byte*
begin (void) const override
{ return m_target.begin (); }
std::byte*
begin (void) override
{ return m_target.begin (); }
std::byte*
end (void) override
{ return m_target.end (); }
const std::byte*
end (void) const override
{ return m_target.end (); }
size_t
offset (const void *ptr) const override
{ return m_target.offset (ptr); }
void reset (void) override
{ return m_target.reset (); }
// capacity queries
size_t capacity (void) const override { return m_target.capacity (); }
size_t used (void) const override { return m_target.used (); }
size_t remain (void) const override { return m_target.remain (); }
private:
ChildT m_target;
};
dynamic (std::unique_ptr<interface> _child):
m_child (std::move (_child))
{ ; }
std::unique_ptr<interface> m_child;
};
}
#endif

View File

@ -1,63 +0,0 @@
#include "tap.hpp"
#include "alloc/arena.hpp"
#include "alloc/raw/linear.hpp"
#include "debug.hpp"
///////////////////////////////////////////////////////////////////////////////
static std::byte g_backing[1024*1024];
//-----------------------------------------------------------------------------
struct setter {
setter (const setter&) = delete;
setter (bool &_target):
target (_target)
{ target = false; }
~setter ()
{ target = true; }
bool &target;
};
//-----------------------------------------------------------------------------
int
main (void)
{
cruft::alloc::raw::linear alloc (cruft::make_view (g_backing));
cruft::alloc::arena<cruft::alloc::raw::linear> arena (alloc);
cruft::TAP::logger tap;
bool flag = true;
// double check our testing object is working, because I'm tired and stupid
{
setter val (flag);
CHECK (!flag);
}
CHECK (flag);
// ensure manual acquire and release calls constructors and destructors
{
auto obj = arena.acquire<setter> (flag);
tap.expect_eq (flag, false, "arena manual acquire calls constructor");
arena.release (obj);
tap.expect_eq (flag, true, "arena manual release calls destructor");
}
// ensure unique_ptr like objects call constructors and destructors
{
auto obj = arena.unique<setter> (flag);
tap.expect_eq (flag, false, "arena unique acquire calls constructor");
}
tap.expect_eq (flag, true, "arena unique release calls destructor");
return tap.status ();
}

View File

@ -1,22 +0,0 @@
#include "tap.hpp"
#include "alloc/raw/dynamic.hpp"
#include "alloc/raw/null.hpp"
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
cruft::TAP::logger tap;
auto obj = cruft::alloc::raw::dynamic::make<cruft::alloc::raw::null> ();
tap.expect_throw<std::bad_alloc> (
[&] (void) {
obj.allocate (sizeof (char));
},
"trivial dispatch to null allocator"
);
return tap.status ();
}

View File

@ -1,20 +0,0 @@
#include "alloc/forwarding.hpp"
#include "alloc/raw/linear.hpp"
#include "tap.hpp"
int
main ()
{
std::byte buffer[64];
cruft::alloc::raw::linear linear (buffer);
cruft::alloc::forwarding forwarding (linear);
cruft::TAP::logger tap;
tap.expect_eq (linear.used (), 0u, "construction does not allocate");
forwarding.allocate<uint8_t> (16u);
tap.expect_eq (linear.used (), 16u, "allocation size is exactly committed");
return tap.status ();
}