bit::memory
policy_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains a block allocator that uses policy-based design
4  * to componentize entries
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_POLICY_BLOCK_ALLOCATOR_HPP
31 #define BIT_MEMORY_BLOCK_ALLOCATORS_POLICY_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 "../utilities/ebo_storage.hpp" // detail::ebo_storage
38 #include "../utilities/allocator_info.hpp" // allocator_info
39 #include "../utilities/memory_block.hpp" // memory_block
40 #include "../utilities/owner.hpp" // owner
41 
42 #include "../concepts/BlockAllocator.hpp" // block_allocator_has
43 
44 #include "../traits/block_allocator_traits.hpp" // block_allocator_traits
45 
46 #include <cstddef> // std::size_t
47 #include <tuple> // std::tuple, std::forward_as_tuple
48 #include <mutex> // std::lock_guard
49 
50 namespace bit {
51  namespace memory {
52  namespace detail {
53 
59  template<typename BlockAllocator,
60  bool HasAlignment = block_allocator_has_default_block_alignment<BlockAllocator>::value>
61  struct policy_block_allocator_base
62  {
63 
64  };
65 
66  template<typename BlockAllocator>
67  struct policy_block_allocator_base<BlockAllocator,true>
68  {
69  using default_block_alignment = typename BlockAllocator::default_block_alignment;
70  };
71  } // namespae detail
72 
73 
97  template<typename BlockAllocator,
98  typename MemoryTagger,
99  typename MemoryTracker,
100  typename BasicLockable>
102  : private ebo_storage<BlockAllocator,MemoryTagger,MemoryTracker,BasicLockable>,
103  public detail::policy_block_allocator_base<BlockAllocator>
104  {
106  using base_type = ebo_storage<BlockAllocator,
107  MemoryTagger,
108  MemoryTracker,
109  BasicLockable>;
110 
111  //-----------------------------------------------------------------------
112  // Public Member Types
113  //-----------------------------------------------------------------------
114  public:
115 
116  using lock_type = BasicLockable;
117  using tracker_type = MemoryTracker;
118 
119  //-----------------------------------------------------------------------
120  // Constructors
121  //-----------------------------------------------------------------------
122  public:
123 
125  policy_block_allocator() = default;
126 
131  template<typename Arg0, typename...Args,
132  typename = std::enable_if_t<
133  std::is_constructible<BlockAllocator,Arg0,Args...>::value &&
134  !std::is_same<Arg0,std::decay_t<policy_block_allocator>>::value>>
135  policy_block_allocator( Arg0&& arg0, Args&&...args );
136 
143  policy_block_allocator( policy_block_allocator&& other ) = default;
144 
151  policy_block_allocator( const policy_block_allocator& other ) = default;
152 
153  //-----------------------------------------------------------------------
154 
157  ~policy_block_allocator();
158 
159  //-----------------------------------------------------------------------
160 
168  policy_block_allocator& operator=( policy_block_allocator&& other ) = default;
169 
177  policy_block_allocator& operator=( const policy_block_allocator& other ) = default;
178 
179  //-----------------------------------------------------------------------
180  // Block Allocation
181  //-----------------------------------------------------------------------
182  public:
183 
187  owner<memory_block> allocate_block();
188 
192  void deallocate_block( owner<memory_block> block );
193 
194  //-----------------------------------------------------------------------
195  // Observers
196  //-----------------------------------------------------------------------
197  public:
198 
202  const tracker_type& tracker() const noexcept;
203 
211  allocator_info info() const noexcept;
212 
216  std::size_t next_block_size() const noexcept;
217  };
218 
219  } // namespace memory
220 } // namespace memory
221 
222 #include "detail/policy_block_allocator.inl"
223 
224 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_POLICY_BLOCK_ALLOCATOR_HPP */
The block_allocator_traits class template provides a standardized way to access block allocator funct...
This block allocator uses policies to modify the behaviour of the underlying type.
This type is used to hold the generic information for a given allocator.
A utility class used for leveraging empty-base optimization for a generic sequence of types...
Definition: ebo_storage.hpp:62