bit::memory
virtual_block_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains an implementation of a virtual-block allocator.
4  *****************************************************************************/
5 
6 /*
7  The MIT License (MIT)
8 
9  Copyright (c) 2018 Matthew Rodusek
10 
11  Permission is hereby granted, free of charge, to any person obtaining a copy
12  of this software and associated documentation files (the "Software"), to deal
13  in the Software without restriction, including without limitation the rights
14  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the Software is
16  furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  SOFTWARE.
28 */
29 #ifndef BIT_MEMORY_BLOCK_ALLOCATORS_VIRTUAL_BLOCK_ALLOCATOR_HPP
30 #define BIT_MEMORY_BLOCK_ALLOCATORS_VIRTUAL_BLOCK_ALLOCATOR_HPP
31 
32 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
33 # pragma once
34 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
35 
36 #include "detail/named_block_allocator.hpp" // detail::named_block_allocator
37 
38 #include "../regions/virtual_memory.hpp"
39 
40 #include "../utilities/ebo_storage.hpp" // ebo_storage
41 #include "../utilities/dynamic_size_type.hpp" // dynamic_size, etc
42 #include "../utilities/allocator_info.hpp" // allocator_info
43 #include "../utilities/memory_block_cache.hpp" // memory_block_cache
44 #include "../utilities/owner.hpp" // owner
45 
46 #include "../policies/growth_multipliers/no_growth.hpp" // no_growth
47 
48 #include <cstddef> // std::size_t
49 #include <type_traits> // std::enable_if
50 
51 namespace bit {
52  namespace memory {
53 
63  template<std::size_t Pages, typename GrowthMultiplier=no_growth_multiplier>
65  : ebo_storage<GrowthMultiplier>,
66  dynamic_size_type<0,Pages>
67  {
70 
71  //-----------------------------------------------------------------------
72  // Constructors
73  //-----------------------------------------------------------------------
74  public:
75 
80  template<std::size_t UPages=Pages,
81  typename=std::enable_if_t<UPages!=dynamic_size>>
82  explicit virtual_block_allocator( GrowthMultiplier growth = GrowthMultiplier{} );
83 
89  template<std::size_t UPages=Pages,
90  typename=std::enable_if_t<UPages==dynamic_size>>
91  explicit virtual_block_allocator( std::size_t pages,
92  GrowthMultiplier growth = GrowthMultiplier{} );
93 
98 
99  // Deleted copy constructor
100  virtual_block_allocator( const virtual_block_allocator& other ) = delete;
101 
102  //-----------------------------------------------------------------------
103 
107 
108  //-----------------------------------------------------------------------
109 
110  // Deleted move-assignment
111  virtual_block_allocator& operator=( virtual_block_allocator&& other ) = delete;
112 
113  // Deleted copy-assignment
114  virtual_block_allocator& operator=( const virtual_block_allocator& other ) = delete;
115 
116  //-----------------------------------------------------------------------
117  // Block Allocations
118  //-----------------------------------------------------------------------
119  public:
120 
124  owner<memory_block> allocate_block() noexcept;
125 
129  void deallocate_block( owner<memory_block> block ) noexcept;
130 
131  //-----------------------------------------------------------------------
132  // Observers
133  //-----------------------------------------------------------------------
134  public:
135 
139  std::size_t next_block_size() const noexcept;
140 
147  allocator_info info() const noexcept;
148 
149  //-----------------------------------------------------------------------
150  // Private Members
151  //-----------------------------------------------------------------------
152  private:
153 
154  void* m_memory;
155  std::ptrdiff_t m_active_page;
156  memory_block_cache m_cache;
157  };
158 
159  //-------------------------------------------------------------------------
160  // Utilities
161  //-------------------------------------------------------------------------
162 
163  template<std::size_t Pages,
164  typename GrowthMultiplier=no_growth_multiplier>
165  using named_virtual_block_allocator = detail::named_block_allocator<virtual_block_allocator<Pages,GrowthMultiplier>>;
166 
167  } // namespace memory
168 } // namespace bit
169 
170 #include "detail/virtual_block_allocator.inl"
171 
172 #endif /* BIT_MEMORY_BLOCK_ALLOCATORS_VIRTUAL_BLOCK_ALLOCATOR_HPP */
allocator_info info() const noexcept
Gets the info about this allocator.
This block allocator is used for creating virtual memory blocks.
~virtual_block_allocator()
Destructs this virtual_block_allocator, decommitting any commit pages.
This type is used as a compile-time constant for storing size values.
virtual_block_allocator(GrowthMultiplier growth=GrowthMultiplier{})
Constructs a virtual_block_allocator that reserves the specified number of pages up front...
A multiplier that doesn&#39;t actually grow.
Definition: no_growth.hpp:47
std::size_t next_block_size() const noexcept
Queries the next block size expected from this allocator.
owner< memory_block > allocate_block() noexcept
Allocates a memory_block of size block_size.
A cache containing an intrinsically linked list of memory_blocks.
void deallocate_block(owner< memory_block > block) noexcept
Deallocates a given memory_block.
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