bit::memory
pool_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition for an allocator that creates
4  * fixed-sized allocations from a reused pool
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_ALLOCATORS_POOL_ALLOCATOR_HPP
31 #define BIT_MEMORY_ALLOCATORS_POOL_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_allocator.hpp" // detail::named_allocator
38 
39 #include "../utilities/freelist.hpp" // freelist
40 #include "../utilities/macros.hpp" // BIT_MEMORY_ASSUME
41 #include "../utilities/memory_block.hpp" // memory_block
42 #include "../utilities/owner.hpp" // owner
43 #include "../utilities/pointer_utilities.hpp" // is_power_of_two
44 
45 #include <cassert>
46 
47 namespace bit {
48  namespace memory {
49 
57  {
58  // TODO: Add template-argument for chunk-size
59 
60  //-----------------------------------------------------------------------
61  // Public Member Types
62  //-----------------------------------------------------------------------
63  public:
64 
67  using max_alignment = std::integral_constant<std::size_t,128>;
68 
69  //-----------------------------------------------------------------------
70  // Constructors / Assignment
71  //-----------------------------------------------------------------------
72  public:
73 
79  pool_allocator( std::size_t chunk_size, memory_block block );
80 
84  pool_allocator( pool_allocator&& other ) noexcept = default;
85 
86  // Deleted copy construction
87  pool_allocator( const pool_allocator& other ) = delete;
88 
89  //-----------------------------------------------------------------------
90 
91  // Deleted move assignment
92  pool_allocator& operator=( pool_allocator&& other ) = delete;
93 
94  // Deleted copy assignment
95  pool_allocator& operator=( const pool_allocator& other ) = delete;
96 
97  //-----------------------------------------------------------------------
98  // Allocations / Deallocations
99  //-----------------------------------------------------------------------
100  public:
101 
109  owner<void*> try_allocate( std::size_t size,
110  std::size_t align,
111  std::size_t offset = 0 ) noexcept;
112 
118  void deallocate( owner<void*> p, std::size_t size );
119 
121  void deallocate_all();
122 
123  //-----------------------------------------------------------------------
124  // Observers
125  //-----------------------------------------------------------------------
126  public:
127 
131  bool owns( const void* p ) const noexcept;
132 
136  std::size_t max_size() const noexcept;
137 
138  //----------------------------------------------------------------------
139 
146  allocator_info info() const noexcept;
147 
148  //-----------------------------------------------------------------------
149  // Private Members
150  //-----------------------------------------------------------------------
151  private:
152 
153  freelist m_freelist;
154  memory_block m_block;
155  std::size_t m_chunk_size;
156 
157  //-----------------------------------------------------------------------
158  // Private Member Functions
159  //-----------------------------------------------------------------------
160  private:
161 
163  void create_pool();
164  };
165 
166  //-------------------------------------------------------------------------
167  // Utilities
168  //-------------------------------------------------------------------------
169 
170  using named_pool_allocator = detail::named_allocator<pool_allocator>;
171 
172  } // namespace memory
173 } // namespace bit
174 
175 #include "detail/pool_allocator.inl"
176 
177 #endif /* BIT_MEMORY_ALLOCATORS_POOL_ALLOCATOR_HPP */
pool_allocator(std::size_t chunk_size, memory_block block)
Constructs a pool allocator with chunk sizes of chunk_size, in the arena indicated by block...
std::integral_constant< std::size_t, 128 > max_alignment
void deallocate_all()
Deallocates all memory in this pool_allocator.
bool owns(const void *p) const noexcept
Determines whether the pointer p is owned by this allocator.
STL namespace.
owner< void * > try_allocate(std::size_t size, std::size_t align, std::size_t offset=0) noexcept
Tries to allocate size bytes with the alignment of align, offset by offset.
void deallocate(owner< void *> p, std::size_t size)
Deallocates memory previously allocated from a call to try_allocate.
std::size_t max_size() const noexcept
Determines the max size that this allocator can allocate.
allocator_info info() const noexcept
Gets the info about this allocator.
A simple freelist that uses intrusive pointers inside raw memory.
Definition: freelist.hpp:53
This allocator creates a pool of fixed-sized chunk entries for allocations.
Wrapper around a block of memory, containing both the size and the address of the memory block...
This type is used to hold the generic information for a given allocator.