From 4e25f6e3e27fe21bd46daa4448351a61a0080aa8 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 19 Dec 2018 17:55:24 +1100 Subject: [PATCH] alloc: eliminate 'raw' allocator distinction --- CMakeLists.txt | 30 ++++++++++++------------- alloc/{raw => }/affix.cpp | 2 +- alloc/{raw => }/affix.hpp | 9 +++----- alloc/{raw => }/aligned/direct.hpp | 8 +++---- alloc/{raw => }/aligned/foreign.hpp | 8 +++---- alloc/{raw => }/fallback.cpp | 0 alloc/{raw => }/fallback.hpp | 9 +++----- alloc/{raw => }/linear.cpp | 10 ++++----- alloc/{raw => }/linear.hpp | 10 ++++----- alloc/{raw => }/malloc.cpp | 5 ----- alloc/{raw => }/malloc.hpp | 10 +++------ alloc/{raw => }/null.cpp | 4 ++-- alloc/{raw => }/null.hpp | 9 +++----- alloc/raw/traits.hpp | 34 ----------------------------- alloc/{raw => }/stack.cpp | 8 +++---- alloc/{raw => }/stack.hpp | 11 ++++------ alloc/traits.hpp | 23 +++++++++++++++++++ test/alloc/aligned/direct.cpp | 6 ++--- test/alloc/aligned/foreign.cpp | 6 ++--- test/alloc/easy.cpp | 4 ++-- test/alloc/linear.cpp | 4 ++-- test/alloc/stack.cpp | 6 ++--- 22 files changed, 92 insertions(+), 124 deletions(-) rename alloc/{raw => }/affix.cpp (92%) rename alloc/{raw => }/affix.hpp (89%) rename alloc/{raw => }/aligned/direct.hpp (96%) rename alloc/{raw => }/aligned/foreign.hpp (96%) rename alloc/{raw => }/fallback.cpp (100%) rename alloc/{raw => }/fallback.hpp (86%) rename alloc/{raw => }/linear.cpp (94%) rename alloc/{raw => }/linear.hpp (95%) rename alloc/{raw => }/malloc.cpp (85%) rename alloc/{raw => }/malloc.hpp (89%) rename alloc/{raw => }/null.cpp (96%) rename alloc/{raw => }/null.hpp (92%) delete mode 100644 alloc/raw/traits.hpp rename alloc/{raw => }/stack.cpp (93%) rename alloc/{raw => }/stack.hpp (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f22e9f4..c9f30ad8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -193,26 +193,26 @@ list ( algo/sort.cpp algo/sort.hpp alloc/fwd.hpp + alloc/affix.cpp + alloc/affix.hpp + alloc/aligned/direct.hpp + alloc/aligned/foreign.hpp alloc/allocator.cpp alloc/allocator.hpp alloc/easy.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/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 + 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/std.hpp alloc/traits.hpp + alloc/traits.hpp annotation.hpp array/darray.hpp array/sarray.cpp diff --git a/alloc/raw/affix.cpp b/alloc/affix.cpp similarity index 92% rename from alloc/raw/affix.cpp rename to alloc/affix.cpp index eb42dcfe..33d4cb45 100644 --- a/alloc/raw/affix.cpp +++ b/alloc/affix.cpp @@ -8,7 +8,7 @@ #include "affix.hpp" -using cruft::alloc::raw::affix; +using cruft::alloc::affix; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/raw/affix.hpp b/alloc/affix.hpp similarity index 89% rename from alloc/raw/affix.hpp rename to alloc/affix.hpp index 9c1eda2e..6f4e38d3 100644 --- a/alloc/raw/affix.hpp +++ b/alloc/affix.hpp @@ -6,14 +6,13 @@ * Copyright 2015 Danny Robson */ -#ifndef CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP -#define CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP +#pragma once -#include "../../view.hpp" +#include "../view.hpp" #include -namespace cruft::alloc::raw { +namespace cruft::alloc { /// a raw memory allocator which initialises an instance of PrefixT /// immediately before each allocation and, if specified, an instance /// of SuffixT after the allocation. @@ -42,5 +41,3 @@ namespace cruft::alloc::raw { size_t offset (const void*) const; }; } - -#endif diff --git a/alloc/raw/aligned/direct.hpp b/alloc/aligned/direct.hpp similarity index 96% rename from alloc/raw/aligned/direct.hpp rename to alloc/aligned/direct.hpp index f5dea4b8..277c6522 100644 --- a/alloc/raw/aligned/direct.hpp +++ b/alloc/aligned/direct.hpp @@ -8,14 +8,14 @@ #pragma once -#include "../../../std.hpp" -#include "../../../debug.hpp" -#include "../../../view.hpp" +#include "../../std.hpp" +#include "../../debug.hpp" +#include "../../view.hpp" #include -namespace cruft::alloc::raw::aligned { +namespace cruft::alloc::aligned { /// wraps a child allocator and enforces a fixed alignment template class direct { diff --git a/alloc/raw/aligned/foreign.hpp b/alloc/aligned/foreign.hpp similarity index 96% rename from alloc/raw/aligned/foreign.hpp rename to alloc/aligned/foreign.hpp index 93ad2dbd..57602436 100644 --- a/alloc/raw/aligned/foreign.hpp +++ b/alloc/aligned/foreign.hpp @@ -10,14 +10,14 @@ #include "direct.hpp" -#include "../../../cast.hpp" -#include "../../../pointer.hpp" -#include "../../../debug.hpp" +#include "../../cast.hpp" +#include "../../pointer.hpp" +#include "../../debug.hpp" #include -namespace cruft::alloc::raw::aligned { +namespace cruft::alloc::aligned { /// wraps a child allocator and enforces a fixed alignment that is /// independant of the alignment of the provided source buffer. /// diff --git a/alloc/raw/fallback.cpp b/alloc/fallback.cpp similarity index 100% rename from alloc/raw/fallback.cpp rename to alloc/fallback.cpp diff --git a/alloc/raw/fallback.hpp b/alloc/fallback.hpp similarity index 86% rename from alloc/raw/fallback.hpp rename to alloc/fallback.hpp index eb143f25..f025eb9d 100644 --- a/alloc/raw/fallback.hpp +++ b/alloc/fallback.hpp @@ -6,15 +6,14 @@ * Copyright 2015-2017 Danny Robson */ -#ifndef CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP -#define CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP +#pragma once -#include "../../view.hpp" +#include "../view.hpp" #include #include -namespace cruft::alloc::raw { +namespace cruft::alloc { /// A raw memory allocator that allocates memory series of child /// allocators, preferring earlier allocators. template @@ -39,5 +38,3 @@ namespace cruft::alloc::raw { std::tuple m_children; }; } - -#endif diff --git a/alloc/raw/linear.cpp b/alloc/linear.cpp similarity index 94% rename from alloc/raw/linear.cpp rename to alloc/linear.cpp index 5c280ff2..62821c9f 100644 --- a/alloc/raw/linear.cpp +++ b/alloc/linear.cpp @@ -8,12 +8,12 @@ #include "linear.hpp" -#include "../traits.hpp" +#include "traits.hpp" -#include "../../pointer.hpp" -#include "../../debug.hpp" +#include "../pointer.hpp" +#include "../debug.hpp" -using cruft::alloc::raw::linear; +using cruft::alloc::linear; /////////////////////////////////////////////////////////////////////////////// @@ -34,7 +34,7 @@ linear::linear (linear &&rhs) //----------------------------------------------------------------------------- linear& -linear::operator= (cruft::alloc::raw::linear &&rhs) +linear::operator= (linear &&rhs) { m_begin = std::exchange (rhs.m_begin, nullptr); m_end = std::exchange (rhs.m_end, nullptr); diff --git a/alloc/raw/linear.hpp b/alloc/linear.hpp similarity index 95% rename from alloc/raw/linear.hpp rename to alloc/linear.hpp index dfd047c4..6caf176b 100644 --- a/alloc/raw/linear.hpp +++ b/alloc/linear.hpp @@ -8,16 +8,16 @@ #pragma once -#include "../../std.hpp" -#include "../../view.hpp" -#include "../../pointer.hpp" -#include "../../memory/buffer/traits.hpp" +#include "../std.hpp" +#include "../view.hpp" +#include "../pointer.hpp" +#include "../memory/buffer/traits.hpp" #include #include -namespace cruft::alloc::raw { +namespace cruft::alloc { // 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/raw/malloc.cpp b/alloc/malloc.cpp similarity index 85% rename from alloc/raw/malloc.cpp rename to alloc/malloc.cpp index 3bc1db43..48c2a6d0 100644 --- a/alloc/raw/malloc.cpp +++ b/alloc/malloc.cpp @@ -7,8 +7,3 @@ */ #include "malloc.hpp" - -#include "../../debug.hpp" - -#include - diff --git a/alloc/raw/malloc.hpp b/alloc/malloc.hpp similarity index 89% rename from alloc/raw/malloc.hpp rename to alloc/malloc.hpp index bf52ab20..191d5854 100644 --- a/alloc/raw/malloc.hpp +++ b/alloc/malloc.hpp @@ -6,10 +6,9 @@ * Copyright 2018 Danny Robson */ -#ifndef CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP -#define CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP +#pragma once -#include "../../view.hpp" +#include "../view.hpp" #include #include @@ -23,7 +22,7 @@ posix_memalign (void **ptr, std::size_t align, std::size_t size) } #endif -namespace cruft::alloc::raw { +namespace cruft::alloc { class malloc { public: template @@ -55,6 +54,3 @@ namespace cruft::alloc::raw { } }; } - - -#endif \ No newline at end of file diff --git a/alloc/raw/null.cpp b/alloc/null.cpp similarity index 96% rename from alloc/raw/null.cpp rename to alloc/null.cpp index 3dda3f99..1968a4b9 100644 --- a/alloc/raw/null.cpp +++ b/alloc/null.cpp @@ -9,11 +9,11 @@ #include "null.hpp" -#include "../../debug.hpp" +#include "../debug.hpp" #include -using cruft::alloc::raw::null; +using cruft::alloc::null; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/raw/null.hpp b/alloc/null.hpp similarity index 92% rename from alloc/raw/null.hpp rename to alloc/null.hpp index bb267764..99eea41c 100644 --- a/alloc/raw/null.hpp +++ b/alloc/null.hpp @@ -6,15 +6,14 @@ * Copyright 2015 Danny Robson */ -#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP -#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP +#pragma once -#include "../../view.hpp" +#include "../view.hpp" #include -namespace cruft::alloc::raw { +namespace cruft::alloc { // 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). @@ -66,5 +65,3 @@ namespace cruft::alloc::raw { size_t remain (void) const; }; } - -#endif diff --git a/alloc/raw/traits.hpp b/alloc/raw/traits.hpp deleted file mode 100644 index f3b68b44..00000000 --- a/alloc/raw/traits.hpp +++ /dev/null @@ -1,34 +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_RAW_TRAITS_HPP -#define CRUFT_UTIL_ALLOC_RAW_TRAITS_HPP - -#include - - -namespace cruft::alloc::raw { - template > - struct has_aligned_allocate : std::false_type {}; - - template - struct has_aligned_allocate< - AllocT, - std::void_t< - decltype ( - std::declval ().allocate(16, 16) - ) - > - >: public std::true_type {}; - - - template - constexpr auto has_aligned_allocate_v = has_aligned_allocate::value; -}; - -#endif diff --git a/alloc/raw/stack.cpp b/alloc/stack.cpp similarity index 93% rename from alloc/raw/stack.cpp rename to alloc/stack.cpp index 7ec6314b..28040901 100644 --- a/alloc/raw/stack.cpp +++ b/alloc/stack.cpp @@ -8,11 +8,11 @@ #include "stack.hpp" -#include "../../debug.hpp" -#include "../../pointer.hpp" -#include "../../cast.hpp" +#include "../debug.hpp" +#include "../pointer.hpp" +#include "../cast.hpp" -using cruft::alloc::raw::stack; +using cruft::alloc::stack; /////////////////////////////////////////////////////////////////////////////// diff --git a/alloc/raw/stack.hpp b/alloc/stack.hpp similarity index 95% rename from alloc/raw/stack.hpp rename to alloc/stack.hpp index 88cded2c..25cd19a6 100644 --- a/alloc/raw/stack.hpp +++ b/alloc/stack.hpp @@ -6,16 +6,15 @@ * Copyright 2015 Danny Robson */ -#ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP -#define CRUFT_UTIL_ALLOC_RAW_STACK_HPP +#pragma once -#include "../../view.hpp" -#include "../../pointer.hpp" +#include "../view.hpp" +#include "../pointer.hpp" #include -namespace cruft::alloc::raw { +namespace cruft::alloc { // allocate memory from a buffer in a stacklike manner. deallocation that // is not correctly ordered has undefined (read 'bad') results. class stack { @@ -112,5 +111,3 @@ namespace cruft::alloc::raw { std::byte *m_cursor; }; } - -#endif diff --git a/alloc/traits.hpp b/alloc/traits.hpp index f70abb0b..ef5495bc 100644 --- a/alloc/traits.hpp +++ b/alloc/traits.hpp @@ -52,4 +52,27 @@ namespace cruft::alloc { //------------------------------------------------------------------------- template constexpr auto is_allocator_v = is_allocator::value; + + + /////////////////////////////////////////////////////////////////////////// + template > + struct has_aligned_allocate : std::false_type {}; + + + //------------------------------------------------------------------------- + template + struct has_aligned_allocate< + AllocT, + std::void_t< + decltype ( + std::declval ().allocate(16, 16) + ) + > + >: public std::true_type {}; + + + + //------------------------------------------------------------------------- + template + constexpr auto has_aligned_allocate_v = has_aligned_allocate::value; } diff --git a/test/alloc/aligned/direct.cpp b/test/alloc/aligned/direct.cpp index a049123c..64cd3b84 100644 --- a/test/alloc/aligned/direct.cpp +++ b/test/alloc/aligned/direct.cpp @@ -1,7 +1,7 @@ #include "tap.hpp" -#include "alloc/raw/aligned/direct.hpp" -#include "alloc/raw/linear.hpp" +#include "alloc/aligned/direct.hpp" +#include "alloc/linear.hpp" int @@ -20,7 +20,7 @@ main (int, char**) // we're probably operating correctly. static constexpr std::size_t alignment = 3; - cruft::alloc::raw::aligned::direct alloc ( + cruft::alloc::aligned::direct alloc ( cruft::make_view (buffer), alignment ); diff --git a/test/alloc/aligned/foreign.cpp b/test/alloc/aligned/foreign.cpp index 42bb041c..fa108853 100644 --- a/test/alloc/aligned/foreign.cpp +++ b/test/alloc/aligned/foreign.cpp @@ -1,5 +1,5 @@ -#include "alloc/raw/aligned/foreign.hpp" -#include "alloc/raw/linear.hpp" +#include "alloc/aligned/foreign.hpp" +#include "alloc/linear.hpp" #include "pointer.hpp" #include "tap.hpp" @@ -17,7 +17,7 @@ main () alignof (std::max_align_t) ) + increment; - cruft::alloc::raw::aligned::foreign alloc ( + cruft::alloc::aligned::foreign alloc ( cruft::view(base,std::end(buffer)), alignment ); diff --git a/test/alloc/easy.cpp b/test/alloc/easy.cpp index c9162be5..41699ba2 100644 --- a/test/alloc/easy.cpp +++ b/test/alloc/easy.cpp @@ -1,5 +1,5 @@ #include "alloc/easy.hpp" -#include "alloc/raw/linear.hpp" +#include "alloc/linear.hpp" #include "memory/buffer/simple.hpp" #include "tap.hpp" @@ -28,7 +28,7 @@ int main () cruft::memory::buffer::simple buf (elements); cruft::alloc::easy::owned < - cruft::alloc::raw::linear, + cruft::alloc::linear, cruft::memory::buffer::simple > alloc ( std::move (buf) diff --git a/test/alloc/linear.cpp b/test/alloc/linear.cpp index 40e199db..e8059b72 100644 --- a/test/alloc/linear.cpp +++ b/test/alloc/linear.cpp @@ -1,5 +1,5 @@ #include "tap.hpp" -#include "alloc/raw/linear.hpp" +#include "alloc/linear.hpp" /////////////////////////////////////////////////////////////////////////////// @@ -11,7 +11,7 @@ main (void) constexpr size_t BUFFER_SIZE = 1024; alignas (std::max_align_t) u08 memory[BUFFER_SIZE]; - cruft::alloc::raw::linear store (cruft::make_view (memory)); + cruft::alloc::linear store (cruft::make_view (memory)); tap.expect_eq (store.begin (), 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 3c681b4a..022223d8 100644 --- a/test/alloc/stack.cpp +++ b/test/alloc/stack.cpp @@ -1,10 +1,10 @@ #include "tap.hpp" -#include "alloc/raw/stack.hpp" +#include "alloc/stack.hpp" /////////////////////////////////////////////////////////////////////////////// void -n_allocations (cruft::alloc::raw::stack &store, +n_allocations (cruft::alloc::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) std::byte memory[BUFFER_SIZE]; std::fill (std::begin (memory), std::end (memory), std::byte{0}); - cruft::alloc::raw::stack store (cruft::make_view(memory, memory + BUFFER_AVAILABLE)); + cruft::alloc::stack store (cruft::make_view(memory, memory + BUFFER_AVAILABLE)); tap.expect_eq (store.begin (), std::begin (memory), "base pointers match"); tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");