bit::memory
block_allocator_traits.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header defines traits for block_allocators
4  *
5  * Currently, this library does not contain any optional features
6  * that a block_allocator may define; but is defined and used for
7  * ensuring forward-compatibility as things change
8  *****************************************************************************/
9 
10 /*
11  The MIT License (MIT)
12 
13  Copyright (c) 2018 Matthew Rodusek
14 
15  Permission is hereby granted, free of charge, to any person obtaining a copy
16  of this software and associated documentation files (the "Software"), to deal
17  in the Software without restriction, including without limitation the rights
18  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19  copies of the Software, and to permit persons to whom the Software is
20  furnished to do so, subject to the following conditions:
21 
22  The above copyright notice and this permission notice shall be included in
23  all copies or substantial portions of the Software.
24 
25  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  SOFTWARE.
32 */
33 #ifndef BIT_MEMORY_TRAITS_BLOCK_ALLOCATOR_TRAITS_HPP
34 #define BIT_MEMORY_TRAITS_BLOCK_ALLOCATOR_TRAITS_HPP
35 
36 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
37 # pragma once
38 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
39 
40 #include "../utilities/allocator_info.hpp" // allocator_info
41 #include "../utilities/macros.hpp" // BIT_MEMORY_UNUSED
42 #include "../utilities/memory_block.hpp" // memory_block
43 #include "../utilities/owner.hpp" // owner
44 
45 #include "../concepts/BlockAllocator.hpp" // is_block_allocator
46 
47 #include <type_traits> // std::true_type, std::false_type, etc
48 #include <cstddef> // std::size_t, std::ptrdiff_t
49 #include <memory> // std::addressof
50 #include <typeinfo> // std::type_info
51 
52 namespace bit {
53  namespace memory {
54 
69  template<typename BlockAllocator>
71  {
72  static_assert( is_block_allocator<BlockAllocator>::value, "BlockAllocator must be a BlockAllocator" );
73 
74  //----------------------------------------------------------------------
75  // Public Member Types
76  //----------------------------------------------------------------------
77  public:
78 
80 
81  //----------------------------------------------------------------------
82  // Block Allocations
83  //----------------------------------------------------------------------
84  public:
85 
90  static owner<memory_block> allocate_block( BlockAllocator& alloc );
91 
96  static void deallocate_block( BlockAllocator& alloc,
97  owner<memory_block> block );
98 
99  //----------------------------------------------------------------------
100  // Observers
101  //----------------------------------------------------------------------
102  public:
103 
115  static allocator_info info( const BlockAllocator& alloc ) noexcept;
116 
121  static std::size_t next_block_size( const BlockAllocator& alloc ) noexcept;
122 
132  static std::size_t next_block_alignment( const BlockAllocator& alloc ) noexcept;
133 
134  //----------------------------------------------------------------------
135  // Private Implementation
136  //----------------------------------------------------------------------
137  private:
138 
145  static allocator_info do_info( std::true_type,
146  const BlockAllocator& alloc );
147  static allocator_info do_info( std::false_type,
148  const BlockAllocator& alloc );
150 
151  static std::size_t do_next_block_size( std::true_type,
152  const BlockAllocator& alloc );
153  static std::size_t do_next_block_size( std::false_type,
154  const BlockAllocator& alloc );
155 
156  static std::size_t do_next_block_align_from_type( std::true_type,
157  const BlockAllocator& alloc );
158  static std::size_t do_next_block_align_from_type( std::false_type,
159  const BlockAllocator& alloc);
160  static std::size_t do_next_block_align_from_fn( std::true_type,
161  const BlockAllocator& alloc );
162  static std::size_t do_next_block_align_from_fn( std::false_type,
163  const BlockAllocator& alloc );
164  };
165 
166  } // namespace memory
167 } // namespace bit
168 
169 #include "detail/block_allocator_traits.inl"
170 
171 #endif /* BIT_MEMORY_TRAITS_BLOCK_ALLOCATOR_TRAITS_HPP */
static std::size_t next_block_alignment(const BlockAllocator &alloc) noexcept
Gets the alignment of the next block from the block allocator.
static allocator_info info(const BlockAllocator &alloc) noexcept
Gets the name of the specified block allocator.
static std::size_t next_block_size(const BlockAllocator &alloc) noexcept
Gets the size of the next block from the block allocator.
Type-trait to determine whether T defines &#39;default_block_alignment&#39;.
The block_allocator_traits class template provides a standardized way to access block allocator funct...
This type is used to hold the generic information for a given allocator.
static void deallocate_block(BlockAllocator &alloc, owner< memory_block > block)
Deallocates a block from the underlying container.
static owner< memory_block > allocate_block(BlockAllocator &alloc)
Allocates a block from the underlying allocator.
Type-trait to determine whether T satisfies the minimum requirements to be a BlockAllocator.