bit::memory
BlockAllocator.hpp
1 /*****************************************************************************
2  * \file
3  * \brief This header contains the definitions for the 'BlockAllocator'
4  * concept and related type-traits
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_CONCEPTS_BLOCKALLOCATOR_HPP
31 #define BIT_MEMORY_CONCEPTS_BLOCKALLOCATOR_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/identity.hpp" // detail::identity
38 #include "detail/void_t.hpp" // detail::void_t
39 
40 #include "../utilities/allocator_info.hpp" // allocator_info
41 #include "../utilities/memory_block.hpp" // memory_block
42 
43 #include <type_traits> // std::integral_constants
44 
45 namespace bit {
46  namespace memory {
47 
96 #if __cplusplus >= 202000L
97  // TODO(bitwize) replace 202000L with the correct __cplusplus when certified
98 
99  template<typename A>
100  concept BlockAllocator = requires( A a, memory_block block )
101  {
102  { block = a.allocate_block() } -> memory_block;
103  { a.deallocate_block( block ) } -> void;
104  { a.next_block_size() } -> std::size_t;
105  };
106 #endif
107 
108  namespace detail {
109 
110  template<typename T, typename = void>
111  struct block_allocator_has_allocate_block_impl : std::false_type{};
112 
113  template<typename T>
114  struct block_allocator_has_allocate_block_impl<T,void_t<
115  decltype( std::declval<memory_block&>() = std::declval<T&>().allocate_block() )
116  >> : std::true_type{};
117 
118  //-----------------------------------------------------------------------
119 
120  template<typename T, typename = void>
121  struct block_allocator_has_deallocate_block_impl : std::false_type{};
122 
123  template<typename T>
124  struct block_allocator_has_deallocate_block_impl<T,void_t<
125  decltype( std::declval<T&>().deallocate_block( std::declval<memory_block&>() ) )
126  >> : std::true_type{};
127 
128  //-----------------------------------------------------------------------
129 
130  template<typename T, typename = void>
131  struct block_allocator_has_default_block_alignment_impl : std::false_type{};
132 
133  template<typename T>
134  struct block_allocator_has_default_block_alignment_impl<T,void_t<
135  decltype(T::default_block_alignment)>
136  > : std::true_type{};
137 
138  //-----------------------------------------------------------------------
139 
140  template<typename T, typename = void>
141  struct block_allocator_has_next_block_size_impl : std::false_type{};
142 
143  template<typename T>
144  struct block_allocator_has_next_block_size_impl<T,void_t<
145  decltype(std::declval<std::size_t&>() = std::declval<const T&>().next_block_size())>
146  > : std::true_type{};
147 
148  //-----------------------------------------------------------------------
149 
150  template<typename T, typename = void>
151  struct block_allocator_has_next_block_alignment_impl : std::false_type{};
152 
153  template<typename T>
154  struct block_allocator_has_next_block_alignment_impl<T,void_t<
155  decltype(std::declval<std::size_t&>() = std::declval<const T&>().next_block_alignment())>
156  > : std::true_type{};
157 
158  //-----------------------------------------------------------------------
159 
160  template<typename T, typename = void>
161  struct block_allocator_has_info_impl : std::false_type{};
162 
163  template<typename T>
164  struct block_allocator_has_info_impl<T,
165  void_t<decltype( std::declval<allocator_info>() = std::declval<const T&>().info() )>
166  > : std::true_type{};
167 
168  } // namespace detail
169 
175  template<typename T>
177  : detail::block_allocator_has_default_block_alignment_impl<T>{};
178 
183  template<typename T>
184  constexpr std::size_t block_allocator_default_block_alignment_v
186 
187  //-------------------------------------------------------------------------
188 
194  template<typename T>
196  : detail::block_allocator_has_next_block_size_impl<T>{};
197 
202  template<typename T>
203  constexpr std::size_t block_allocator_has_next_block_size_v
205 
206  //-------------------------------------------------------------------------
207 
213  template<typename T>
215  : detail::block_allocator_has_next_block_alignment_impl<T>{};
216 
221  template<typename T>
222  constexpr std::size_t block_allocator_has_next_block_alignment_v
224 
225  //-------------------------------------------------------------------------
226 
232  template<typename T>
234  : detail::block_allocator_has_info_impl<T>{};
235 
240  template<typename T>
241  constexpr bool block_allocator_has_info_v
243 
244  //-------------------------------------------------------------------------
245 
252  template<typename T>
253  struct is_block_allocator : std::integral_constant<bool,
254  detail::block_allocator_has_allocate_block_impl<T>::value &&
255  detail::block_allocator_has_deallocate_block_impl<T>::value &&
256  detail::block_allocator_has_next_block_size_impl<T>::value
257  >{};
258 
263  template<typename T>
264  constexpr bool is_block_allocator_v = is_block_allocator<T>::value;
265 
266  } // namespace memory
267 } // namespace bit
268 
269 #endif /* BIT_MEMORY_CONCEPTS_BLOCKALLOCATOR_HPP */
Type-trait to determine whether T defines an &#39;info&#39; function.
Type-trait to determine whether T defines &#39;block_size&#39;.
STL namespace.
Type-trait to determine whether T defines &#39;default_block_alignment&#39;.
Type-trait to determine whether T defines &#39;block_size&#39;.
Type-trait to determine whether T satisfies the minimum requirements to be a BlockAllocator.