From 9d5e5992464cfb2bf9ad0fa7f540b0381d58f8fa Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 29 Aug 2017 16:53:11 +1000 Subject: [PATCH] alloc: seperate out the raw allocators from the adapters --- CMakeLists.txt | 28 ++++++++++++++-------------- alloc/fallback.cpp | 1 - alloc/fwd.hpp | 21 ++++++++++++--------- alloc/{ => raw}/affix.cpp | 3 ++- alloc/{ => raw}/affix.hpp | 15 +++++++++++---- alloc/{ => raw}/aligned.hpp | 6 +++--- alloc/{ => raw}/dynamic.hpp | 6 +++--- alloc/raw/fallback.cpp | 17 +++++++++++++++++ alloc/{ => raw}/fallback.hpp | 20 ++++++++++++++------ alloc/{ => raw}/linear.cpp | 6 +++--- alloc/{ => raw}/linear.hpp | 6 +++--- alloc/{ => raw}/malloc.cpp | 4 ++-- alloc/{ => raw}/malloc.hpp | 8 ++++---- alloc/{ => raw}/null.cpp | 4 ++-- alloc/{ => raw}/null.hpp | 6 +++--- alloc/{ => raw}/stack.cpp | 8 ++++---- alloc/{ => raw}/stack.hpp | 6 +++--- alloc/{ => raw}/stack.ipp | 4 ++-- test/alloc/aligned.cpp | 6 +++--- test/alloc/arena.cpp | 9 ++++++--- test/alloc/dynamic.cpp | 9 +++++---- test/alloc/linear.cpp | 5 +++-- test/alloc/stack.cpp | 6 +++--- 23 files changed, 122 insertions(+), 82 deletions(-) delete mode 100644 alloc/fallback.cpp rename alloc/{ => raw}/affix.cpp (95%) rename alloc/{ => raw}/affix.hpp (66%) rename alloc/{ => raw}/aligned.hpp (96%) rename alloc/{ => raw}/dynamic.hpp (98%) create mode 100644 alloc/raw/fallback.cpp rename alloc/{ => raw}/fallback.hpp (63%) rename alloc/{ => raw}/linear.cpp (97%) rename alloc/{ => raw}/linear.hpp (93%) rename alloc/{ => raw}/malloc.cpp (96%) rename alloc/{ => raw}/malloc.hpp (88%) rename alloc/{ => raw}/null.cpp (97%) rename alloc/{ => raw}/null.hpp (93%) rename alloc/{ => raw}/stack.cpp (97%) rename alloc/{ => raw}/stack.hpp (93%) rename alloc/{ => raw}/stack.ipp (89%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ebaec42..afae28d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,26 +150,26 @@ list ( algo/sort.hpp algo/sort.ipp alloc/fwd.hpp - alloc/affix.cpp - alloc/affix.hpp - alloc/aligned.hpp alloc/allocator.cpp alloc/allocator.hpp alloc/allocator.ipp alloc/arena.cpp alloc/arena.hpp alloc/arena.ipp - alloc/dynamic.hpp - alloc/fallback.cpp - alloc/fallback.hpp - alloc/linear.cpp - alloc/linear.hpp - alloc/malloc.cpp - alloc/malloc.hpp - alloc/null.cpp - alloc/null.hpp - alloc/stack.cpp - alloc/stack.hpp + alloc/raw/affix.cpp + alloc/raw/affix.hpp + alloc/raw/aligned.hpp + alloc/raw/dynamic.hpp + alloc/raw/fallback.cpp + alloc/raw/fallback.hpp + alloc/raw/linear.cpp + alloc/raw/linear.hpp + alloc/raw/malloc.cpp + alloc/raw/malloc.hpp + alloc/raw/null.cpp + alloc/raw/null.hpp + alloc/raw/stack.cpp + alloc/raw/stack.hpp annotation.hpp ascii.hpp backtrace.hpp diff --git a/alloc/fallback.cpp b/alloc/fallback.cpp deleted file mode 100644 index 76435d99..00000000 --- a/alloc/fallback.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "./fallback.hpp" diff --git a/alloc/fwd.hpp b/alloc/fwd.hpp index d9e3ac57..cb3fd8a5 100644 --- a/alloc/fwd.hpp +++ b/alloc/fwd.hpp @@ -19,17 +19,20 @@ namespace util::alloc { - class affix; - class fallback; - class linear; - class malloc; - class null; - class stack; + namespace raw { + class affix; + class fallback; + class linear; + class malloc; + class null; + class stack; - class dynamic; + class dynamic; + + template + class aligned; + } - template - class aligned; template class arena; template class allocator; diff --git a/alloc/affix.cpp b/alloc/raw/affix.cpp similarity index 95% rename from alloc/affix.cpp rename to alloc/raw/affix.cpp index 87f37ba4..7f5f681a 100644 --- a/alloc/affix.cpp +++ b/alloc/raw/affix.cpp @@ -16,7 +16,8 @@ #include "./affix.hpp" -using util::alloc::affix; +using util::alloc::raw::affix; /////////////////////////////////////////////////////////////////////////////// + diff --git a/alloc/affix.hpp b/alloc/raw/affix.hpp similarity index 66% rename from alloc/affix.hpp rename to alloc/raw/affix.hpp index a337280a..1cb0111d 100644 --- a/alloc/affix.hpp +++ b/alloc/raw/affix.hpp @@ -14,13 +14,20 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_AFFIX_HPP -#define __UTIL_ALLOC_AFFIX_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP +#define CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP #include -namespace util::alloc { - template +namespace util::alloc::raw { + /// a raw memory allocator which initialises an instance of PrefixT + /// immediately before each allocation and, if specified, an instance + /// of SuffixT after the allocation. + /// + /// uses an instance of ParentT to actually perform the bulk allocations. + /// + /// useful for sentinels, reference counts, etc. + template class affix { void* allocate (size_t bytes); void* allocate (size_t bytes, size_t align); diff --git a/alloc/aligned.hpp b/alloc/raw/aligned.hpp similarity index 96% rename from alloc/aligned.hpp rename to alloc/raw/aligned.hpp index 2cd034d0..dcd7e881 100644 --- a/alloc/aligned.hpp +++ b/alloc/raw/aligned.hpp @@ -14,10 +14,10 @@ * Copyright 2016 Danny Robson */ -#ifndef __CRUFT_UTIL_ALLOC_ALIGNED_HPP -#define __CRUFT_UTIL_ALLOC_ALIGNED_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP +#define CRUFT_UTIL_ALLOC_RAW_ALIGNED_HPP -namespace util::alloc { +namespace util::alloc::raw { /// wraps a child allocator and enforces a fixed alignment template class aligned { diff --git a/alloc/dynamic.hpp b/alloc/raw/dynamic.hpp similarity index 98% rename from alloc/dynamic.hpp rename to alloc/raw/dynamic.hpp index 62b1ce5f..931faf29 100644 --- a/alloc/dynamic.hpp +++ b/alloc/raw/dynamic.hpp @@ -14,13 +14,13 @@ * Copyright 2016 Danny Robson */ -#ifndef __UTIL_ALLOC_DYNAMIC_HPP -#define __UTIL_ALLOC_DYNAMIC_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP +#define CRUFT_UTIL_ALLOC_RAW_DYNAMIC_HPP #include #include -namespace util::alloc { +namespace util::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. diff --git a/alloc/raw/fallback.cpp b/alloc/raw/fallback.cpp new file mode 100644 index 00000000..1c1762f0 --- /dev/null +++ b/alloc/raw/fallback.cpp @@ -0,0 +1,17 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright 2017 Danny Robson + */ + +#include "./fallback.hpp" diff --git a/alloc/fallback.hpp b/alloc/raw/fallback.hpp similarity index 63% rename from alloc/fallback.hpp rename to alloc/raw/fallback.hpp index e4a2e875..22ec0b32 100644 --- a/alloc/fallback.hpp +++ b/alloc/raw/fallback.hpp @@ -11,25 +11,33 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2015 Danny Robson + * Copyright 2015-2017 Danny Robson */ -#ifndef __UTIL_ALLOC_FALLBACK_HPP -#define __UTIL_ALLOC_FALLBACK_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP +#define CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP #include +#include -namespace util::alloc { - template +namespace util::alloc::raw { + /// A raw memory allocator that allocates memory series of child + /// allocators, preferring earlier allocators. + template class fallback { public: - fallback (A&, B&); + fallback (ChildT &..._children): + m_children (_children...) + { ; } void* allocate (size_t bytes); void* allocate (size_t bytes, size_t align); void deallocate (void *ptr, size_t bytes); void deallocate (void *ptr, size_t bytes, size_t align); + + private: + std::tuple m_children; }; } diff --git a/alloc/linear.cpp b/alloc/raw/linear.cpp similarity index 97% rename from alloc/linear.cpp rename to alloc/raw/linear.cpp index cd61b5a3..5c1d47e9 100644 --- a/alloc/linear.cpp +++ b/alloc/raw/linear.cpp @@ -16,10 +16,10 @@ #include "./linear.hpp" -#include "../pointer.hpp" -#include "../debug.hpp" +#include "../../pointer.hpp" +#include "../../debug.hpp" -using util::alloc::linear; +using util::alloc::raw::linear; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/linear.hpp b/alloc/raw/linear.hpp similarity index 93% rename from alloc/linear.hpp rename to alloc/raw/linear.hpp index 07c4d82e..aec0f475 100644 --- a/alloc/linear.hpp +++ b/alloc/raw/linear.hpp @@ -14,12 +14,12 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_LINEAR_HPP -#define __UTIL_ALLOC_LINEAR_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_LINEAR_HPP +#define CRUFT_UTIL_ALLOC_RAW_LINEAR_HPP #include -namespace util::alloc { +namespace util::alloc::raw { // allocate progressively across a buffer without concern for deallocation. // deallocation is a noop; the only way to free allocations is via reset. class linear { diff --git a/alloc/malloc.cpp b/alloc/raw/malloc.cpp similarity index 96% rename from alloc/malloc.cpp rename to alloc/raw/malloc.cpp index cc5afd1e..4901c92a 100644 --- a/alloc/malloc.cpp +++ b/alloc/raw/malloc.cpp @@ -16,11 +16,11 @@ #include "./malloc.hpp" -#include "../debug.hpp" +#include "../../debug.hpp" #include -using util::alloc::malloc; +using util::alloc::raw::malloc; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/malloc.hpp b/alloc/raw/malloc.hpp similarity index 88% rename from alloc/malloc.hpp rename to alloc/raw/malloc.hpp index 8137ce80..cdaaf93a 100644 --- a/alloc/malloc.hpp +++ b/alloc/raw/malloc.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_MALLOC_HPP -#define __UTIL_ALLOC_MALLOC_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP +#define CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP #include -namespace util::alloc { +namespace util::alloc::raw { class malloc { public: void* allocate (size_t bytes); @@ -32,4 +32,4 @@ namespace util::alloc { } -#endif +#endif \ No newline at end of file diff --git a/alloc/null.cpp b/alloc/raw/null.cpp similarity index 97% rename from alloc/null.cpp rename to alloc/raw/null.cpp index 7e041fa6..11070fb4 100644 --- a/alloc/null.cpp +++ b/alloc/raw/null.cpp @@ -17,11 +17,11 @@ #include "./null.hpp" -#include "../debug.hpp" +#include "../../debug.hpp" #include -using util::alloc::null; +using util::alloc::raw::null; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/null.hpp b/alloc/raw/null.hpp similarity index 93% rename from alloc/null.hpp rename to alloc/raw/null.hpp index 162b7800..971a5f48 100644 --- a/alloc/null.hpp +++ b/alloc/raw/null.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_NULL_HPP -#define __UTIL_ALLOC_NULL_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP +#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP #include -namespace util::alloc { +namespace util::alloc::raw { // allocator that always fails, throwing bad_alloc. deallocate will // succeed with nullptr as with delete, but is undefined with other values // (it is likely to at least assert). diff --git a/alloc/stack.cpp b/alloc/raw/stack.cpp similarity index 97% rename from alloc/stack.cpp rename to alloc/raw/stack.cpp index 3956fd65..7d05ea0b 100644 --- a/alloc/stack.cpp +++ b/alloc/raw/stack.cpp @@ -16,11 +16,11 @@ #include "./stack.hpp" -#include "../debug.hpp" -#include "../pointer.hpp" -#include "../cast.hpp" +#include "../../debug.hpp" +#include "../../pointer.hpp" +#include "../../cast.hpp" -using util::alloc::stack; +using util::alloc::raw::stack; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/stack.hpp b/alloc/raw/stack.hpp similarity index 93% rename from alloc/stack.hpp rename to alloc/raw/stack.hpp index 272ba54a..c2aac44c 100644 --- a/alloc/stack.hpp +++ b/alloc/raw/stack.hpp @@ -14,13 +14,13 @@ * Copyright 2015 Danny Robson */ -#ifndef __UTIL_ALLOC_STACK_HPP -#define __UTIL_ALLOC_STACK_HPP +#ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP +#define CRUFT_UTIL_ALLOC_RAW_STACK_HPP #include -namespace util::alloc { +namespace util::alloc::raw { // allocate memory from a buffer in a stacklike manner. deallocation that // is not correctly ordered has undefined (read 'bad') results. class stack { diff --git a/alloc/stack.ipp b/alloc/raw/stack.ipp similarity index 89% rename from alloc/stack.ipp rename to alloc/raw/stack.ipp index c779ae58..d3248e0f 100644 --- a/alloc/stack.ipp +++ b/alloc/raw/stack.ipp @@ -14,10 +14,10 @@ * Copyright 2015 Danny Robson */ -#ifdef __UTIL_ALLOC_STACK_IPP +#ifdef CRUFT_UTIL_ALLOC_RAW_STACK_IPP #error #endif -#define __UTIL_ALLOC_STACK_IPP +#define CRUFT_UTIL_ALLOC_RAW_STACK_IPP diff --git a/test/alloc/aligned.cpp b/test/alloc/aligned.cpp index 71fae127..22faa649 100644 --- a/test/alloc/aligned.cpp +++ b/test/alloc/aligned.cpp @@ -1,7 +1,7 @@ #include "tap.hpp" -#include "alloc/aligned.hpp" -#include "alloc/linear.hpp" +#include "alloc/raw/aligned.hpp" +#include "alloc/raw/linear.hpp" int @@ -20,7 +20,7 @@ main (int, char**) // we're probably operating correctly. static constexpr std::size_t alignment = 3; - util::alloc::aligned alloc ( + util::alloc::raw::aligned alloc ( alignment, std::begin (buffer), std::end (buffer) ); diff --git a/test/alloc/arena.cpp b/test/alloc/arena.cpp index 4813ad26..6ea41e32 100644 --- a/test/alloc/arena.cpp +++ b/test/alloc/arena.cpp @@ -1,11 +1,13 @@ #include "tap.hpp" #include "alloc/arena.hpp" -#include "alloc/linear.hpp" +#include "alloc/raw/linear.hpp" +/////////////////////////////////////////////////////////////////////////////// static char g_backing[1024*1024]; +//----------------------------------------------------------------------------- struct setter { setter (const setter&) = delete; @@ -20,11 +22,12 @@ struct setter { }; +//----------------------------------------------------------------------------- int main (void) { - util::alloc::linear alloc (std::begin (g_backing), std::end (g_backing)); - util::alloc::arena arena (alloc); + util::alloc::raw::linear alloc (std::begin (g_backing), std::end (g_backing)); + util::alloc::arena arena (alloc); util::TAP::logger tap; diff --git a/test/alloc/dynamic.cpp b/test/alloc/dynamic.cpp index f669a4d9..ca39bd71 100644 --- a/test/alloc/dynamic.cpp +++ b/test/alloc/dynamic.cpp @@ -1,14 +1,15 @@ - #include "tap.hpp" -#include "alloc/dynamic.hpp" -#include "alloc/null.hpp" +#include "alloc/raw/dynamic.hpp" +#include "alloc/raw/null.hpp" + +/////////////////////////////////////////////////////////////////////////////// int main (void) { util::TAP::logger tap; - auto obj = util::alloc::dynamic::make (); + auto obj = util::alloc::raw::dynamic::make (); tap.expect_throw ( [&] (void) { diff --git a/test/alloc/linear.cpp b/test/alloc/linear.cpp index e8800d70..0e94c68b 100644 --- a/test/alloc/linear.cpp +++ b/test/alloc/linear.cpp @@ -1,7 +1,8 @@ #include "tap.hpp" -#include "alloc/linear.hpp" +#include "alloc/raw/linear.hpp" +/////////////////////////////////////////////////////////////////////////////// int main (void) { @@ -10,7 +11,7 @@ main (void) constexpr size_t BUFFER_SIZE = 1024; alignas (std::max_align_t) char memory[BUFFER_SIZE]; - util::alloc::linear store (std::begin (memory), std::end (memory)); + util::alloc::raw::linear store (std::begin (memory), std::end (memory)); tap.expect_eq (store.base (), std::begin (memory), "base pointers match"); tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0"); diff --git a/test/alloc/stack.cpp b/test/alloc/stack.cpp index a52b6b5d..0c383c5c 100644 --- a/test/alloc/stack.cpp +++ b/test/alloc/stack.cpp @@ -1,10 +1,10 @@ #include "tap.hpp" -#include "alloc/stack.hpp" +#include "alloc/raw/stack.hpp" /////////////////////////////////////////////////////////////////////////////// void -n_allocations (util::alloc::stack &store, +n_allocations (util::alloc::raw::stack &store, unsigned count, size_t bytes, size_t alignment = alignof (std::max_align_t)) @@ -32,7 +32,7 @@ main (void) alignas (std::max_align_t) char memory[BUFFER_SIZE]; std::fill (std::begin (memory), std::end (memory), 0); - util::alloc::stack store (memory, memory + BUFFER_AVAILABLE); + util::alloc::raw::stack store (memory, memory + BUFFER_AVAILABLE); tap.expect_eq (store.base (), std::begin (memory), "base pointers match"); tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");