alloc: eliminate 'raw' allocator distinction

This commit is contained in:
Danny Robson 2018-12-19 17:55:24 +11:00
parent 6308166179
commit 4e25f6e3e2
22 changed files with 92 additions and 124 deletions

View File

@ -193,26 +193,26 @@ list (
algo/sort.cpp algo/sort.cpp
algo/sort.hpp algo/sort.hpp
alloc/fwd.hpp alloc/fwd.hpp
alloc/affix.cpp
alloc/affix.hpp
alloc/aligned/direct.hpp
alloc/aligned/foreign.hpp
alloc/allocator.cpp alloc/allocator.cpp
alloc/allocator.hpp alloc/allocator.hpp
alloc/easy.hpp alloc/easy.hpp
alloc/raw/traits.hpp alloc/fallback.cpp
alloc/raw/affix.cpp alloc/fallback.hpp
alloc/raw/affix.hpp alloc/linear.cpp
alloc/raw/aligned/direct.hpp alloc/linear.hpp
alloc/raw/aligned/foreign.hpp alloc/malloc.cpp
alloc/raw/fallback.cpp alloc/malloc.hpp
alloc/raw/fallback.hpp alloc/null.cpp
alloc/raw/linear.cpp alloc/null.hpp
alloc/raw/linear.hpp alloc/stack.cpp
alloc/raw/malloc.cpp alloc/stack.hpp
alloc/raw/malloc.hpp
alloc/raw/null.cpp
alloc/raw/null.hpp
alloc/raw/stack.cpp
alloc/raw/stack.hpp
alloc/std.hpp alloc/std.hpp
alloc/traits.hpp alloc/traits.hpp
alloc/traits.hpp
annotation.hpp annotation.hpp
array/darray.hpp array/darray.hpp
array/sarray.cpp array/sarray.cpp

View File

@ -8,7 +8,7 @@
#include "affix.hpp" #include "affix.hpp"
using cruft::alloc::raw::affix; using cruft::alloc::affix;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -6,14 +6,13 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP #pragma once
#define CRUFT_UTIL_ALLOC_RAW_AFFIX_HPP
#include "../../view.hpp" #include "../view.hpp"
#include <cstddef> #include <cstddef>
namespace cruft::alloc::raw { namespace cruft::alloc {
/// a raw memory allocator which initialises an instance of PrefixT /// a raw memory allocator which initialises an instance of PrefixT
/// immediately before each allocation and, if specified, an instance /// immediately before each allocation and, if specified, an instance
/// of SuffixT after the allocation. /// of SuffixT after the allocation.
@ -42,5 +41,3 @@ namespace cruft::alloc::raw {
size_t offset (const void*) const; size_t offset (const void*) const;
}; };
} }
#endif

View File

@ -8,14 +8,14 @@
#pragma once #pragma once
#include "../../../std.hpp" #include "../../std.hpp"
#include "../../../debug.hpp" #include "../../debug.hpp"
#include "../../../view.hpp" #include "../../view.hpp"
#include <utility> #include <utility>
namespace cruft::alloc::raw::aligned { namespace cruft::alloc::aligned {
/// wraps a child allocator and enforces a fixed alignment /// wraps a child allocator and enforces a fixed alignment
template <typename ChildT> template <typename ChildT>
class direct { class direct {

View File

@ -10,14 +10,14 @@
#include "direct.hpp" #include "direct.hpp"
#include "../../../cast.hpp" #include "../../cast.hpp"
#include "../../../pointer.hpp" #include "../../pointer.hpp"
#include "../../../debug.hpp" #include "../../debug.hpp"
#include <utility> #include <utility>
namespace cruft::alloc::raw::aligned { namespace cruft::alloc::aligned {
/// wraps a child allocator and enforces a fixed alignment that is /// wraps a child allocator and enforces a fixed alignment that is
/// independant of the alignment of the provided source buffer. /// independant of the alignment of the provided source buffer.
/// ///

View File

@ -6,15 +6,14 @@
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net> * Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP #pragma once
#define CRUFT_UTIL_ALLOC_RAW_FALLBACK_HPP
#include "../../view.hpp" #include "../view.hpp"
#include <cstddef> #include <cstddef>
#include <tuple> #include <tuple>
namespace cruft::alloc::raw { namespace cruft::alloc {
/// A raw memory allocator that allocates memory series of child /// A raw memory allocator that allocates memory series of child
/// allocators, preferring earlier allocators. /// allocators, preferring earlier allocators.
template <typename ...ChildT> template <typename ...ChildT>
@ -39,5 +38,3 @@ namespace cruft::alloc::raw {
std::tuple<ChildT&...> m_children; std::tuple<ChildT&...> m_children;
}; };
} }
#endif

View File

@ -8,12 +8,12 @@
#include "linear.hpp" #include "linear.hpp"
#include "../traits.hpp" #include "traits.hpp"
#include "../../pointer.hpp" #include "../pointer.hpp"
#include "../../debug.hpp" #include "../debug.hpp"
using cruft::alloc::raw::linear; using cruft::alloc::linear;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -34,7 +34,7 @@ linear::linear (linear &&rhs)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
linear& linear&
linear::operator= (cruft::alloc::raw::linear &&rhs) linear::operator= (linear &&rhs)
{ {
m_begin = std::exchange (rhs.m_begin, nullptr); m_begin = std::exchange (rhs.m_begin, nullptr);
m_end = std::exchange (rhs.m_end, nullptr); m_end = std::exchange (rhs.m_end, nullptr);

View File

@ -8,16 +8,16 @@
#pragma once #pragma once
#include "../../std.hpp" #include "../std.hpp"
#include "../../view.hpp" #include "../view.hpp"
#include "../../pointer.hpp" #include "../pointer.hpp"
#include "../../memory/buffer/traits.hpp" #include "../memory/buffer/traits.hpp"
#include <cstddef> #include <cstddef>
#include <iterator> #include <iterator>
namespace cruft::alloc::raw { namespace cruft::alloc {
// allocate progressively across a buffer without concern for deallocation. // allocate progressively across a buffer without concern for deallocation.
// deallocation is a noop; the only way to free allocations is via reset. // deallocation is a noop; the only way to free allocations is via reset.
class linear { class linear {

View File

@ -7,8 +7,3 @@
*/ */
#include "malloc.hpp" #include "malloc.hpp"
#include "../../debug.hpp"
#include <cstdlib>

View File

@ -6,10 +6,9 @@
* Copyright 2018 Danny Robson <danny@nerdcruft.net> * Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP #pragma once
#define CRUFT_UTIL_ALLOC_RAW_MALLOC_HPP
#include "../../view.hpp" #include "../view.hpp"
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
@ -23,7 +22,7 @@ posix_memalign (void **ptr, std::size_t align, std::size_t size)
} }
#endif #endif
namespace cruft::alloc::raw { namespace cruft::alloc {
class malloc { class malloc {
public: public:
template <typename T> template <typename T>
@ -55,6 +54,3 @@ namespace cruft::alloc::raw {
} }
}; };
} }
#endif

View File

@ -9,11 +9,11 @@
#include "null.hpp" #include "null.hpp"
#include "../../debug.hpp" #include "../debug.hpp"
#include <new> #include <new>
using cruft::alloc::raw::null; using cruft::alloc::null;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -6,15 +6,14 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_ALLOC_RAW_NULL_HPP #pragma once
#define CRUFT_UTIL_ALLOC_RAW_NULL_HPP
#include "../../view.hpp" #include "../view.hpp"
#include <cstddef> #include <cstddef>
namespace cruft::alloc::raw { namespace cruft::alloc {
// allocator that always fails, throwing bad_alloc. deallocate will // allocator that always fails, throwing bad_alloc. deallocate will
// succeed with nullptr as with delete, but is undefined with other values // succeed with nullptr as with delete, but is undefined with other values
// (it is likely to at least assert). // (it is likely to at least assert).
@ -66,5 +65,3 @@ namespace cruft::alloc::raw {
size_t remain (void) const; size_t remain (void) const;
}; };
} }
#endif

View File

@ -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 <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_ALLOC_RAW_TRAITS_HPP
#define CRUFT_UTIL_ALLOC_RAW_TRAITS_HPP
#include <type_traits>
namespace cruft::alloc::raw {
template <typename AllocT, typename = std::void_t<>>
struct has_aligned_allocate : std::false_type {};
template <typename AllocT>
struct has_aligned_allocate<
AllocT,
std::void_t<
decltype (
std::declval<AllocT> ().allocate(16, 16)
)
>
>: public std::true_type {};
template <typename AllocT>
constexpr auto has_aligned_allocate_v = has_aligned_allocate<AllocT>::value;
};
#endif

View File

@ -8,11 +8,11 @@
#include "stack.hpp" #include "stack.hpp"
#include "../../debug.hpp" #include "../debug.hpp"
#include "../../pointer.hpp" #include "../pointer.hpp"
#include "../../cast.hpp" #include "../cast.hpp"
using cruft::alloc::raw::stack; using cruft::alloc::stack;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -6,16 +6,15 @@
* Copyright 2015 Danny Robson <danny@nerdcruft.net> * Copyright 2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_ALLOC_RAW_STACK_HPP #pragma once
#define CRUFT_UTIL_ALLOC_RAW_STACK_HPP
#include "../../view.hpp" #include "../view.hpp"
#include "../../pointer.hpp" #include "../pointer.hpp"
#include <cstddef> #include <cstddef>
namespace cruft::alloc::raw { namespace cruft::alloc {
// allocate memory from a buffer in a stacklike manner. deallocation that // allocate memory from a buffer in a stacklike manner. deallocation that
// is not correctly ordered has undefined (read 'bad') results. // is not correctly ordered has undefined (read 'bad') results.
class stack { class stack {
@ -112,5 +111,3 @@ namespace cruft::alloc::raw {
std::byte *m_cursor; std::byte *m_cursor;
}; };
} }
#endif

View File

@ -52,4 +52,27 @@ namespace cruft::alloc {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <typename AllocatorT> template <typename AllocatorT>
constexpr auto is_allocator_v = is_allocator<AllocatorT>::value; constexpr auto is_allocator_v = is_allocator<AllocatorT>::value;
///////////////////////////////////////////////////////////////////////////
template <typename AllocT, typename = std::void_t<>>
struct has_aligned_allocate : std::false_type {};
//-------------------------------------------------------------------------
template <typename AllocT>
struct has_aligned_allocate<
AllocT,
std::void_t<
decltype (
std::declval<AllocT> ().allocate(16, 16)
)
>
>: public std::true_type {};
//-------------------------------------------------------------------------
template <typename AllocT>
constexpr auto has_aligned_allocate_v = has_aligned_allocate<AllocT>::value;
} }

View File

@ -1,7 +1,7 @@
#include "tap.hpp" #include "tap.hpp"
#include "alloc/raw/aligned/direct.hpp" #include "alloc/aligned/direct.hpp"
#include "alloc/raw/linear.hpp" #include "alloc/linear.hpp"
int int
@ -20,7 +20,7 @@ main (int, char**)
// we're probably operating correctly. // we're probably operating correctly.
static constexpr std::size_t alignment = 3; static constexpr std::size_t alignment = 3;
cruft::alloc::raw::aligned::direct<cruft::alloc::raw::linear> alloc ( cruft::alloc::aligned::direct<cruft::alloc::linear> alloc (
cruft::make_view (buffer), alignment cruft::make_view (buffer), alignment
); );

View File

@ -1,5 +1,5 @@
#include "alloc/raw/aligned/foreign.hpp" #include "alloc/aligned/foreign.hpp"
#include "alloc/raw/linear.hpp" #include "alloc/linear.hpp"
#include "pointer.hpp" #include "pointer.hpp"
#include "tap.hpp" #include "tap.hpp"
@ -17,7 +17,7 @@ main ()
alignof (std::max_align_t) alignof (std::max_align_t)
) + increment; ) + increment;
cruft::alloc::raw::aligned::foreign<cruft::alloc::raw::linear> alloc ( cruft::alloc::aligned::foreign<cruft::alloc::linear> alloc (
cruft::view(base,std::end(buffer)), cruft::view(base,std::end(buffer)),
alignment alignment
); );

View File

@ -1,5 +1,5 @@
#include "alloc/easy.hpp" #include "alloc/easy.hpp"
#include "alloc/raw/linear.hpp" #include "alloc/linear.hpp"
#include "memory/buffer/simple.hpp" #include "memory/buffer/simple.hpp"
#include "tap.hpp" #include "tap.hpp"
@ -28,7 +28,7 @@ int main ()
cruft::memory::buffer::simple buf (elements); cruft::memory::buffer::simple buf (elements);
cruft::alloc::easy::owned < cruft::alloc::easy::owned <
cruft::alloc::raw::linear, cruft::alloc::linear,
cruft::memory::buffer::simple cruft::memory::buffer::simple
> alloc ( > alloc (
std::move (buf) std::move (buf)

View File

@ -1,5 +1,5 @@
#include "tap.hpp" #include "tap.hpp"
#include "alloc/raw/linear.hpp" #include "alloc/linear.hpp"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -11,7 +11,7 @@ main (void)
constexpr size_t BUFFER_SIZE = 1024; constexpr size_t BUFFER_SIZE = 1024;
alignas (std::max_align_t) u08 memory[BUFFER_SIZE]; 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.begin (), std::begin (memory), "base pointers match");
tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0"); tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");

View File

@ -1,10 +1,10 @@
#include "tap.hpp" #include "tap.hpp"
#include "alloc/raw/stack.hpp" #include "alloc/stack.hpp"
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void
n_allocations (cruft::alloc::raw::stack &store, n_allocations (cruft::alloc::stack &store,
unsigned count, unsigned count,
size_t bytes, size_t bytes,
size_t alignment = alignof (std::max_align_t)) size_t alignment = alignof (std::max_align_t))
@ -32,7 +32,7 @@ main (void)
alignas (std::max_align_t) std::byte memory[BUFFER_SIZE]; alignas (std::max_align_t) std::byte memory[BUFFER_SIZE];
std::fill (std::begin (memory), std::end (memory), std::byte{0}); 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.begin (), std::begin (memory), "base pointers match");
tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0"); tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");