bit::memory
stack_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of a block allocator that uses
4  * the stack for temporary, automatic cleanup
5  *****************************************************************************/
6 
7 /*
8  The MIT License (MIT)
9 
10  Copyright (c) 2018 Matthew Rodusek
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  SOFTWARE.
29 */
30 #ifndef BIT_MEMORY_BLOCK_ALLOCATORS_STACK_BLOCK_ALLOCATOR_HPP
31 #define BIT_MEMORY_BLOCK_ALLOCATORS_STACK_BLOCK_ALLOCATOR_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "detail/named_block_allocator.hpp" // detail::named_block_allocator
38 
39 #include "../utilities/allocator_info.hpp" // allocator_info
40 #include "../utilities/macros.hpp" // BIT_MEMORY_UNUSED
41 #include "../utilities/memory_block.hpp" // memory_block
42 #include "../utilities/memory_block_cache.hpp" // memory_block_cache
43 #include "../utilities/owner.hpp" // owner
44 #include "../utilities/pointer_utilities.hpp" // is_power_of_two
45 
46 #include <cstddef> // std::max_align_t
47 #include <cassert> // assert
48 
49 namespace bit {
50  namespace memory {
51 
66  template<std::size_t BlockSize,
67  std::size_t Blocks=1u,
68  std::size_t Align=alignof(std::max_align_t)>
70  {
71  static_assert( Blocks > 0,
72  "Must have at least one block" );
73  static_assert( is_power_of_two(Align),
74  "Alignment must be a power of two" );
75  static_assert( Blocks == 1 || BlockSize % Align == 0,
76  "Block size must must be an increment of the alignment" );
77 
78  //-----------------------------------------------------------------------
79  // Public Member Types
80  //-----------------------------------------------------------------------
81  public:
82 
83  using block_size = std::integral_constant<std::size_t,BlockSize>;
84  using block_alignment = std::integral_constant<std::size_t,Align>;
85 
86  //-----------------------------------------------------------------------
87  // Constructors
88  //-----------------------------------------------------------------------
89  public:
90 
95  explicit stack_block_allocator() noexcept;
96 
97  // Deleted move constructor
98  stack_block_allocator( stack_block_allocator&& other ) = delete;
99 
100  // Deleted copy constructor
101  stack_block_allocator( const stack_block_allocator& other ) = delete;
102 
103  //-----------------------------------------------------------------------
104 
105  // Deleted move assignment
106  stack_block_allocator& operator=( stack_block_allocator&& other ) = delete;
107 
108  // Deleted copy assignment
109  stack_block_allocator& operator=( const stack_block_allocator& other ) = delete;
110 
111  //-----------------------------------------------------------------------
112  // Block Allocations
113  //-----------------------------------------------------------------------
114  public:
115 
119  owner<memory_block> allocate_block() noexcept;
120 
127  void deallocate_block( owner<memory_block> block ) noexcept;
128 
129  //-----------------------------------------------------------------------
130  // Observers
131  //-----------------------------------------------------------------------
132  public:
133 
137  std::size_t next_block_size() const noexcept;
138 
145  allocator_info info() const noexcept;
146 
147  //-----------------------------------------------------------------------
148  // Private Members
149  //-----------------------------------------------------------------------
150  private:
151 
152  static constexpr auto s_storage_size = BlockSize * Blocks;
153 
154  alignas(Align) char m_storage[s_storage_size];
155  memory_block_cache m_cache;
156  };
157 
158  //-------------------------------------------------------------------------
159  // Utilities
160  //-------------------------------------------------------------------------
161 
162  template<std::size_t BlockSize,
163  std::size_t Blocks=1,
164  std::size_t Align=alignof(std::max_align_t)>
165  using named_stack_block_allocator = detail::named_block_allocator<stack_block_allocator<BlockSize,Blocks,Align>>;
166 
167  } // namespace memory
168 } // namespace bit
169 
170 #include "detail/stack_block_allocator.inl"
171 
172 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_STACK_BLOCK_ALLOCATOR_HPP */
An allocator with automatic storage reclamation.
A cache containing an intrinsically linked list of memory_blocks.
This type is used to hold the generic information for a given allocator.