bit::memory
policy_allocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definition of a policy-based 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_ALLOCATORS_POLICY_ALLOCATOR_HPP
30 #define BIT_MEMORY_ALLOCATORS_POLICY_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 "../utilities/ebo_storage.hpp" // detail::ebo_storage
37 #include "../utilities/allocator_info.hpp" // allocator_info
38 #include "../utilities/debugging.hpp" // debug_tag_...
39 #include "../utilities/errors.hpp" // get_leak_handler, etc
40 #include "../utilities/macros.hpp" // BIT_MEMORY_UNLIKELY
41 
42 #include "../concepts/Allocator.hpp" // Allocator
43 #include "../concepts/ExtendedAllocator.hpp" // ExtendedAllocator
44 
45 #include "../traits/allocator_traits.hpp" // allocator_traits
46 
47 #include <cstddef> // std::size_t, std::ptrdiff_t
48 #include <mutex> // std::lock_guard
49 
50 namespace bit {
51  namespace memory {
52 
76  template<typename ExtendedAllocator,
77  typename MemoryTagger,
78  typename MemoryTracker,
79  typename BoundsChecker,
80  typename BasicLockable>
82  : private ebo_storage<ExtendedAllocator,
83  MemoryTagger,
84  MemoryTracker,
85  BoundsChecker,
86  BasicLockable>
87  {
88 
89  using base_type = ebo_storage<ExtendedAllocator,
90  MemoryTagger,
91  MemoryTracker,
92  BoundsChecker,
93  BasicLockable>;
94 
95  //-----------------------------------------------------------------------
96  // Public Member Types
97  //-----------------------------------------------------------------------
98  public:
99 
100  // Inherit the various traits specified by the allocation policy;
101  // otherwise default back to the same defaults that the allocator_traits
102  // will choose.
103 
106  using lock_type = BasicLockable;
107  using tracker_type = MemoryTracker;
108 
109  //-----------------------------------------------------------------------
110  // Constructor / Destructor / Assignment
111  //-----------------------------------------------------------------------
112  public:
113 
118  template<typename...Args, typename = std::enable_if_t<std::is_constructible<ExtendedAllocator,Args...>::value>>
119  policy_allocator( Args&&...args );
120 
127  policy_allocator( policy_allocator&& other ) = default;
128 
135  policy_allocator( const policy_allocator& other ) = default;
136 
137  //-----------------------------------------------------------------------
138 
141 
142  //-----------------------------------------------------------------------
143 
150  policy_allocator& operator=( policy_allocator&& other ) = default;
151 
158  policy_allocator& operator=( const policy_allocator& other ) = default;
159 
160  //-----------------------------------------------------------------------
161  // Observers
162  //-----------------------------------------------------------------------
163  public:
164 
168  const tracker_type& tracker() const noexcept;
169 
170  //-----------------------------------------------------------------------
171  // Allocation / Deallocation
172  //-----------------------------------------------------------------------
173 
178  void* try_allocate( std::size_t size, std::size_t align ) noexcept;
179 
184  void deallocate( void* p, std::size_t size );
185 
186  //-----------------------------------------------------------------------
187 
192  template<typename U = ExtendedAllocator, typename = std::enable_if_t<allocator_can_truncate_deallocations<U>::value>>
193  void deallocate_all();
194 
195  //-----------------------------------------------------------------------
196  // Observers
197  //-----------------------------------------------------------------------
198  public:
199 
207  template<typename U = ExtendedAllocator, typename = std::enable_if_t<allocator_knows_ownership<U>::value>>
208  bool owns( const void* p ) const noexcept;
209 
217  template<typename U = ExtendedAllocator, typename = std::enable_if_t<allocator_has_info<U>::value>>
218  allocator_info info() const noexcept;
219 
220  //----------------------------------------------------------------------
221  // Capacity
222  //----------------------------------------------------------------------
223  public:
224 
235  template<typename U = ExtendedAllocator, typename = std::enable_if_t<allocator_has_max_size<U>::value>>
236  std::size_t max_size() const noexcept;
237 
244  template<typename U = ExtendedAllocator, typename = std::enable_if_t<allocator_has_min_size<U>::value>>
245  std::size_t min_size() const noexcept;
246  };
247 
248  } // namespace memory
249 } // namespace bit
250 
251 #include "detail/policy_allocator.inl"
252 
253 #endif /* BIT_MEMORY_ALLOCATORS_POLICY_ALLOCATOR_HPP */
std::size_t min_size() const noexcept
Gets the minimum size allocateable from this allocator.
allocator_info info() const noexcept
Retrieves info about this allocator.
const tracker_type & tracker() const noexcept
Accesses the tracker from the policy_allocator.
Type-trait to determine the default alignment of the given T.
Definition: Allocator.hpp:774
void deallocate(void *p, std::size_t size)
Deallocates the pointer p with the size size.
void deallocate_all()
Deallocates all memory in this allocator.
Type-trait to determine the maximum alignment of the given T.
Definition: Allocator.hpp:792
bool owns(const void *p) const noexcept
Checks if p is owned by the underlying allocator.
~policy_allocator()
Destructs the policy allocator, checking for any leaks.
This allocator manages policy-based memory allocation strategies using policy-based-design.
std::size_t max_size() const noexcept
Gets the maximum size allocateable from this allocator.
void * try_allocate(std::size_t size, std::size_t align) noexcept
Attempts to allocate size bytes aligned to a boundary of align using the underlying allocator...
This type is used to hold the generic information for a given allocator.
policy_allocator & operator=(policy_allocator &&other)=default
Move-assigns this policy_allocator from another allocator.
policy_allocator(Args &&...args)
Constructs an policy_allocator by forwarding all arguments to the underlying ExtendedAllocator.
A utility class used for leveraging empty-base optimization for a generic sequence of types...
Definition: ebo_storage.hpp:62