From 39ed7b27e8397f9ab6b7df8ed376c5629e8b28d8 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 19 Dec 2018 17:34:35 +1100 Subject: [PATCH] alloc: remove unused allocators --- CMakeLists.txt | 8 -- alloc/arena.cpp | 1 - alloc/arena.hpp | 140 ------------------------- alloc/forwarding.cpp | 9 -- alloc/forwarding.hpp | 51 ---------- alloc/raw/dynamic.hpp | 209 -------------------------------------- test/alloc/arena.cpp | 63 ------------ test/alloc/dynamic.cpp | 22 ---- test/alloc/forwarding.cpp | 20 ---- 9 files changed, 523 deletions(-) delete mode 100644 alloc/arena.cpp delete mode 100644 alloc/arena.hpp delete mode 100644 alloc/forwarding.cpp delete mode 100644 alloc/forwarding.hpp delete mode 100644 alloc/raw/dynamic.hpp delete mode 100644 test/alloc/arena.cpp delete mode 100644 test/alloc/dynamic.cpp delete mode 100644 test/alloc/forwarding.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ecd6982..1f22e9f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/alloc/arena.cpp b/alloc/arena.cpp deleted file mode 100644 index a29f7c79..00000000 --- a/alloc/arena.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "arena.hpp" diff --git a/alloc/arena.hpp b/alloc/arena.hpp deleted file mode 100644 index 16c43044..00000000 --- a/alloc/arena.hpp +++ /dev/null @@ -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 - */ - -#ifndef CRUFT_UTIL_ALLOC_ARENA_HPP -#define CRUFT_UTIL_ALLOC_ARENA_HPP - -#include "../memory/deleter.hpp" -#include "../cast.hpp" -#include "../view.hpp" - -#include -#include - -namespace cruft::alloc { - /// wraps a block allocator with an interface suitable for allocating - /// individual objects. - template - class arena { - public: - explicit arena (T &store): - m_store (store) - { ; } - - //--------------------------------------------------------------------- - template - U* - acquire (Args&&... args) - { - U *data = m_store.template allocate (1).data (); - - try { - new (data) U (std::forward (args)...); - } catch (...) { - m_store.template deallocate ({data,1}); - throw; - } - - return data; - } - - //--------------------------------------------------------------------- - template - void - release (U *u) - { - u->~U (); - m_store.template deallocate (cruft::view {u,1u}); - } - - - //--------------------------------------------------------------------- - template - using deleter_t = cruft::memory::owner_deleter< - U,arena,&arena::release - >; - - template - using unique_t = std::unique_ptr>; - - // 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 - auto - unique (Args&& ...args) - { - return unique_t { - acquire (std::forward (args)...), - deleter_t (*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, but that makes it difficult to initialise - /// the raw allocator before we have to supply the reference to the arena. - template - class owned { - public: - template - explicit owned (ArgsT &&...args) - : m_store (std::forward (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 - decltype(auto) acquire (ArgsT &&...args) - { return m_arena.template acquire (std::forward (args)...); } - - template - decltype(auto) release (ArgsT &&...args) - { return m_arena.release (std::forward (args)...); } - - template - decltype(auto) unique (ArgsT &&...args) - { return m_arena.template unique (std::forward (args)...); } - - template - decltype(auto) reset (Args&&...args) - { return m_store.reset (std::forward (args)...); } - - auto const& store (void) const& { return m_store; } - auto & store (void) & { return m_store; } - - private: - AllocT m_store; - arena m_arena; - }; -} - -#endif diff --git a/alloc/forwarding.cpp b/alloc/forwarding.cpp deleted file mode 100644 index a91eb702..00000000 --- a/alloc/forwarding.cpp +++ /dev/null @@ -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 - */ - -#include "forwarding.hpp" diff --git a/alloc/forwarding.hpp b/alloc/forwarding.hpp deleted file mode 100644 index 52d89266..00000000 --- a/alloc/forwarding.hpp +++ /dev/null @@ -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 - */ - -#ifndef CRUFT_UTIL_ALLOC_FORWARDING_HPP -#define CRUFT_UTIL_ALLOC_FORWARDING_HPP - -#include - -namespace cruft::alloc { - template - class forwarding { - public: - explicit forwarding (BackingT &backing): - m_backing (backing) - { ; } - - template - auto allocate (Args &&...args) - { - return m_backing.template allocate (std::forward (args)...); - } - - template - auto deallocate (Args &&...args) - { - return m_backing.template deallocate (std::forward (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 diff --git a/alloc/raw/dynamic.hpp b/alloc/raw/dynamic.hpp deleted file mode 100644 index b960f0f7..00000000 --- a/alloc/raw/dynamic.hpp +++ /dev/null @@ -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 - */ - -#ifndef CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP -#define CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP - -#include "traits.hpp" - -#include -#include - -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 - static dynamic - make (Args &&...args) - { - return dynamic ( - std::make_unique> ( - std::forward (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 - auto allocate (size_t bytes) { return m_child->allocate (bytes); } - - template - auto allocate (size_t bytes, size_t alignment) { return m_child->allocate (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 allocate (size_t bytes) = 0; - virtual cruft::view 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 - class child final : public interface { - public: - struct _alignment_unsupported : public alignment_unsupported { }; - - template - child (Args &&...args): - interface (), - m_target (std::forward (args)...) - { ; } - - // allocation management - cruft::view - 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 - allocate (size_t bytes, size_t alignment) override - { - if constexpr (has_aligned_allocate_v) { - 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) { - 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 _child): - m_child (std::move (_child)) - { ; } - - std::unique_ptr m_child; - }; -} - -#endif diff --git a/test/alloc/arena.cpp b/test/alloc/arena.cpp deleted file mode 100644 index ba900367..00000000 --- a/test/alloc/arena.cpp +++ /dev/null @@ -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 ⌖ -}; - - -//----------------------------------------------------------------------------- -int -main (void) -{ - cruft::alloc::raw::linear alloc (cruft::make_view (g_backing)); - cruft::alloc::arena 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 (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 (flag); - tap.expect_eq (flag, false, "arena unique acquire calls constructor"); - } - - tap.expect_eq (flag, true, "arena unique release calls destructor"); - - return tap.status (); -} diff --git a/test/alloc/dynamic.cpp b/test/alloc/dynamic.cpp deleted file mode 100644 index d22a5c3e..00000000 --- a/test/alloc/dynamic.cpp +++ /dev/null @@ -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 (); - - tap.expect_throw ( - [&] (void) { - obj.allocate (sizeof (char)); - }, - "trivial dispatch to null allocator" - ); - - return tap.status (); -} diff --git a/test/alloc/forwarding.cpp b/test/alloc/forwarding.cpp deleted file mode 100644 index 5e9a1e71..00000000 --- a/test/alloc/forwarding.cpp +++ /dev/null @@ -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 (16u); - tap.expect_eq (linear.used (), 16u, "allocation size is exactly committed"); - - return tap.status (); -} \ No newline at end of file