bit::memory
new_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains an API for allocating memory blocks that use
4  * ::operator new as the underlying allocation scheme.
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_NEW_BLOCK_ALLOCATOR_HPP
31 #define BIT_MEMORY_BLOCK_ALLOCATORS_NEW_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/enum_types.hpp" // block_align_t, block_size_t
38 #include "detail/cached_block_allocator.hpp" // detail::cached_block_allocator
39 #include "detail/named_block_allocator.hpp" // detail::named_block_allocator
40 
41 #include "../utilities/ebo_storage.hpp" // ebo_storage
42 #include "../utilities/dynamic_size_type.hpp" // dynamic_size, etc
43 #include "../utilities/allocator_info.hpp" // allocator_info
44 #include "../utilities/macros.hpp" // BIT_MEMORY_UNLIKELY
45 #include "../utilities/memory_block.hpp" // memory_block
46 #include "../utilities/owner.hpp" // owner
47 
48 #include "../policies/growth_multipliers/no_growth.hpp" // no_growth
49 
50 #include <cstddef> // std::size_t, std::ptrdiff_t
51 #include <type_traits> // std::true_type, std::false_type, etc
52 
53 namespace bit {
54  namespace memory {
55 
65  template<std::size_t DefaultBlockSize,
66  typename GrowthMultiplier=no_growth_multiplier>
68  : ebo_storage<GrowthMultiplier>,
69  dynamic_size_type<0,DefaultBlockSize>
70  {
73 
74  //----------------------------------------------------------------------
75  // Public Member Types
76  //----------------------------------------------------------------------
77  public:
78 
79  using default_block_alignment = std::integral_constant<std::size_t,alignof(std::max_align_t)>;
80 
81  //----------------------------------------------------------------------
82  // Constructors
83  //----------------------------------------------------------------------
84  public:
85 
89  new_block_allocator() noexcept = default;
90 
98  template<std::size_t USize=DefaultBlockSize,
99  typename=std::enable_if_t<USize!=dynamic_size>>
100  explicit new_block_allocator( GrowthMultiplier growth );
101 
109  template<std::size_t USize=DefaultBlockSize,
110  typename=std::enable_if_t<USize==dynamic_size>>
111  explicit new_block_allocator( block_size_t block_size,
112  GrowthMultiplier growth = GrowthMultiplier{} );
113 
117  new_block_allocator( new_block_allocator&& other ) = default;
118 
122  new_block_allocator( const new_block_allocator& other ) = default;
123 
124  //-----------------------------------------------------------------------
125 
130  new_block_allocator& operator=( new_block_allocator&& other ) = default;
131 
136  new_block_allocator& operator=( const new_block_allocator& other ) = default;
137 
138  //----------------------------------------------------------------------
139  // Block Allocations
140  //----------------------------------------------------------------------
141  public:
142 
146  owner<memory_block> allocate_block() noexcept;
147 
151  void deallocate_block( owner<memory_block> block ) noexcept;
152 
153  //----------------------------------------------------------------------
154  // Observers
155  //----------------------------------------------------------------------
156  public:
157 
161  std::size_t next_block_size() const noexcept;
162 
169  allocator_info info() const noexcept;
170  };
171 
172  //-------------------------------------------------------------------------
173  // Utiltiies
174  //-------------------------------------------------------------------------
175 
176  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
177  using cached_new_block_allocator
178  = detail::cached_block_allocator<new_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
179 
180  //-------------------------------------------------------------------------
181 
182  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
183  using named_new_block_allocator
184  = detail::named_block_allocator<new_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
185 
186  //-------------------------------------------------------------------------
187 
188  template<std::size_t DefaultBlockSize, typename GrowthMultiplier=no_growth_multiplier>
189  using named_cached_new_block_allocator
190  = detail::named_block_allocator<cached_new_block_allocator<DefaultBlockSize,GrowthMultiplier>>;
191 
192  } // namespace memory
193 } // namespace bit
194 
195 #include "detail/new_block_allocator.inl"
196 
197 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_NEW_BLOCK_ALLOCATOR_HPP */
This type is used as a compile-time constant for storing size values.
void deallocate_block(owner< memory_block > block) noexcept
Deallocates a new&#39;ed memory_block.
allocator_info info() const noexcept
Gets the info about this allocator.
new_block_allocator() noexcept=default
Default-constructs this new_block_allocator.
new_block_allocator & operator=(new_block_allocator &&other)=default
Move-assigns a new_block_allocator from another allocator.
A block allocator that wraps around calls to new and delete.
owner< memory_block > allocate_block() noexcept
Allocates a new&#39;ed memory_block.
std::size_t next_block_size() const noexcept
Queries the next block size expected from this allocator.
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