bit::memory
thread_local_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header defines a thread-local variant of the static block
4  * allocator
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_THREAD_LOCAL_BLOCK_ALLOCATOR_HPP
31 #define BIT_MEMORY_BLOCK_ALLOCATORS_THREAD_LOCAL_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/memory_block.hpp" // memory_block
41 #include "../utilities/memory_block_cache.hpp" // memory_block_cache
42 #include "../utilities/owner.hpp" // owner
43 
44 #include <cstddef> // std::size_t, std::max_align_t
45 #include <cassert> // assert
46 
47 namespace bit {
48  namespace memory {
49 
70  template<std::size_t BlockSize,
71  std::size_t Blocks = 1,
72  std::size_t Align = alignof(std::max_align_t),
73  typename Tag = void>
75  {
76  static_assert( Blocks > 0,
77  "Must have at least one block" );
78  static_assert( is_power_of_two(Align),
79  "Alignment must be a power of two" );
80  static_assert( Blocks == 1 || BlockSize % Align == 0,
81  "Block size must must be an increment of the block size" );
82 
83  //-----------------------------------------------------------------------
84  // Public Members
85  //-----------------------------------------------------------------------
86  public:
87 
88  using block_size = std::integral_constant<std::size_t,BlockSize>;
89  using block_alignment = std::integral_constant<std::size_t,Align>;
90 
91  //-----------------------------------------------------------------------
92  // Constructor
93  //-----------------------------------------------------------------------
94  public:
95 
97  thread_local_block_allocator() noexcept = default;
98 
102  thread_local_block_allocator( thread_local_block_allocator&& other ) noexcept = default;
103 
107  thread_local_block_allocator( const thread_local_block_allocator& other ) noexcept = default;
108 
109 
110  //-----------------------------------------------------------------------
111 
116  thread_local_block_allocator& operator=( thread_local_block_allocator&& other ) noexcept = default;
117 
122  thread_local_block_allocator& operator=( const thread_local_block_allocator& other ) noexcept = default;
123 
124  //-----------------------------------------------------------------------
125  // Block Allocations
126  //-----------------------------------------------------------------------
127  public:
128 
132  owner<memory_block> allocate_block() noexcept;
133 
137  void deallocate_block( owner<memory_block> block ) noexcept;
138 
139  //-----------------------------------------------------------------------
140  // Observers
141  //-----------------------------------------------------------------------
142  public:
143 
147  std::size_t next_block_size() const noexcept;
148 
155  allocator_info info() const noexcept;
156 
157  //-----------------------------------------------------------------------
158  // Private Members
159  //-----------------------------------------------------------------------
160  private:
161 
162  static constexpr auto s_storage_size = BlockSize * Blocks;
163  alignas(Align) static thread_local char s_storage[s_storage_size];
164 
168  static memory_block_cache& block_cache() noexcept;
169  };
170 
171  //-------------------------------------------------------------------------
172  // Utilities
173  //-------------------------------------------------------------------------
174 
175  template<std::size_t BlockSize,
176  std::size_t Blocks = 1,
177  std::size_t Align = alignof(std::max_align_t),
178  typename Tag = void>
179  using named_thread_local_block_allocator = detail::named_block_allocator<thread_local_block_allocator<BlockSize,Blocks,Align,Tag>>;
180 
181  } // namespace memory
182 } // namespace bit
183 
184 #include "detail/thread_local_block_allocator.inl"
185 
186 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_THREAD_LOCAL_BLOCK_ALLOCATOR_HPP */
A cache containing an intrinsically linked list of memory_blocks.
This is a block allocator that distributes blocks of static memory that is local to the specific thre...
This type is used to hold the generic information for a given allocator.