mn_allocator_typetraits.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINILIB_ALLOCATOR_TYPETRAITS_H__
19 #define __MINILIB_ALLOCATOR_TYPETRAITS_H__
20 
21 #include "../mn_config.hpp"
22 #include "../mn_typetraits.hpp"
23 #include "../utils/mn_alignment.hpp"
24 
25 namespace mn {
26  namespace memory {
27  struct std_allocator_tag { };
29 
30  namespace internal {
31 
32  template<typename TAlloC>
34  using allocator_type = TAlloC ;
35  using value_type = typename allocator_type::value_type;
36  using pointer = typename allocator_type::pointer;
37  using const_pointer = typename allocator_type::const_pointer;
38  using difference_type = typename allocator_type::difference_type;
39  using size_type = typename allocator_type::size_type;
40 
41 
42  using allocator_category = typename TAlloC::allocator_category ;
43  using is_thread_safe = typename TAlloC::is_thread_safe ;
44  };
45  }
46  template <class TAlloC>
48  : public mn::integral_constant<bool, internal::allocator_traits<TAlloC>::is_thread_safe::value> { };
49 
50 
51  template <class TAlloC>
52  inline void* allocate(const TAlloC& alloc, size_t size, size_t alignment,
54  return alloc.allocate(size, alignment);
55  }
56 
57  template <class TAlloC>
58  inline void* allocate(const TAlloC& alloc, size_t size, size_t alignment,
60  return alloc.allocate(size, alignment);
61  }
62  template <class TAlloC>
63  inline void* deallocate(const TAlloC& alloc, void* address, size_t size, size_t alignment,
65  alloc.deallocate(address, size, alignment); return address;
66  }
67 
68  template <class TAlloC>
69  inline void* deallocate(const TAlloC& alloc, void* address, size_t size, size_t alignment,
71  return address;
72  }
73 
74 
75  /*
76  * @brief The default specialization of the allocator_traits for a allocator.
77  * Any specialization must provide the same interface.
78  */
79  template <class TAllocator>
81  public:
85 
86  static pointer allocate_node(allocator_type& state, size_type size, size_type alignment) {
87  return state.allocate(size, alignment);
88  }
89 
91  size_type alignment) {
92  return state.allocate(count, size, alignment);
93  }
94 
96  size_type alignment) noexcept {
97  state.deallocate(node, size, alignment);
98  }
100  size_type size, size_type alignment) noexcept {
101  state.deallocate(node, count, size, alignment);
102  }
103 
104  static size_type max_node_size(const allocator_type& state) {
105  return size_t(-1);
106  }
107  static size_type max_array_size(const allocator_type& state) {
108  return size_t(-1);
109  }
110  static size_type max_alignment(const allocator_type& state) {
111  return mn::max_alignment;
112  }
113  };
114 
115  }
116 }
117 
118 
119 #endif // __MINILIB_ALLOCATOR_TYPETRAITS_H__
Definition: mn_allocator_typetraits.hpp:80
typename internal::allocator_traits< TAllocator >::size_type size_type
Definition: mn_allocator_typetraits.hpp:84
static pointer allocate_array(allocator_type &state, size_type count, size_type size, size_type alignment)
Definition: mn_allocator_typetraits.hpp:90
static void deallocate_array(allocator_type &state, pointer node, size_type count, size_type size, size_type alignment) noexcept
Definition: mn_allocator_typetraits.hpp:99
typename internal::allocator_traits< TAllocator >::allocator_type allocator_type
Definition: mn_allocator_typetraits.hpp:82
static size_type max_node_size(const allocator_type &state)
Definition: mn_allocator_typetraits.hpp:104
static void deallocate_node(allocator_type &state, pointer node, size_type size, size_type alignment) noexcept
Definition: mn_allocator_typetraits.hpp:95
static size_type max_alignment(const allocator_type &state)
Definition: mn_allocator_typetraits.hpp:110
static size_type max_array_size(const allocator_type &state)
Definition: mn_allocator_typetraits.hpp:107
typename internal::allocator_traits< TAllocator >::pointer pointer
Definition: mn_allocator_typetraits.hpp:83
static pointer allocate_node(allocator_type &state, size_type size, size_type alignment)
Definition: mn_allocator_typetraits.hpp:86
basic_node< T > node
Node type witch allocated in global heap.
Definition: mn_node.hpp:266
typename allocator_type::const_pointer const_pointer
Definition: mn_allocator_typetraits.hpp:37
typename allocator_type::pointer pointer
Definition: mn_allocator_typetraits.hpp:36
typename allocator_type::value_type value_type
Definition: mn_allocator_typetraits.hpp:35
typename allocator_type::size_type size_type
Definition: mn_allocator_typetraits.hpp:39
typename allocator_type::difference_type difference_type
Definition: mn_allocator_typetraits.hpp:38
typename TAlloC::is_thread_safe is_thread_safe
Definition: mn_allocator_typetraits.hpp:43
typename TAlloC::allocator_category allocator_category
Definition: mn_allocator_typetraits.hpp:42
TAlloC allocator_type
Definition: mn_allocator_typetraits.hpp:34
Definition: mn_allocator_typetraits.hpp:33
void * allocate(const TAlloC &alloc, size_t size, size_t alignment, mn::memory::std_allocator_tag)
Definition: mn_allocator_typetraits.hpp:52
void * deallocate(const TAlloC &alloc, void *address, size_t size, size_t alignment, mn::memory::std_allocator_tag)
Definition: mn_allocator_typetraits.hpp:63
Definition: mn_allocator_typetraits.hpp:28
Definition: mn_allocator_typetraits.hpp:27
Definition: mn_allocator_typetraits.hpp:25
constexpr size_t max_alignment
Definition: mn_def.hpp:55
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_typetraits.hpp:125
Definition: mn_allocator_typetraits.hpp:48