1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___MEMORY_ALLOCATOR_H 11 #define _LIBCPP___MEMORY_ALLOCATOR_H 12 13 #include <__config> 14 #include <__memory/allocator_traits.h> 15 #include <__utility/forward.h> 16 #include <cstddef> 17 #include <new> 18 #include <stdexcept> 19 #include <type_traits> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 #pragma GCC system_header 23 #endif 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 template <class _Tp> class allocator; 28 29 #if _LIBCPP_STD_VER <= 17 30 template <> 31 class _LIBCPP_TEMPLATE_VIS allocator<void> 32 { 33 public: 34 _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer; 35 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer; 36 _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type; 37 38 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;}; 39 }; 40 41 template <> 42 class _LIBCPP_TEMPLATE_VIS allocator<const void> 43 { 44 public: 45 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer; 46 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer; 47 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type; 48 49 template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;}; 50 }; 51 #endif 52 53 // This class provides a non-trivial default constructor to the class that derives from it 54 // if the condition is satisfied. 55 // 56 // The second template parameter exists to allow giving a unique type to __non_trivial_if, 57 // which makes it possible to avoid breaking the ABI when making this a base class of an 58 // existing class. Without that, imagine we have classes D1 and D2, both of which used to 59 // have no base classes, but which now derive from __non_trivial_if. The layout of a class 60 // that inherits from both D1 and D2 will change because the two __non_trivial_if base 61 // classes are not allowed to share the same address. 62 // 63 // By making those __non_trivial_if base classes unique, we work around this problem and 64 // it is safe to start deriving from __non_trivial_if in existing classes. 65 template <bool _Cond, class _Unique> 66 struct __non_trivial_if { }; 67 68 template <class _Unique> 69 struct __non_trivial_if<true, _Unique> { 70 _LIBCPP_INLINE_VISIBILITY 71 _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT { } 72 }; 73 74 // allocator 75 // 76 // Note: For ABI compatibility between C++20 and previous standards, we make 77 // allocator<void> trivial in C++20. 78 79 template <class _Tp> 80 class _LIBCPP_TEMPLATE_VIS allocator 81 : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > 82 { 83 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types"); 84 public: 85 typedef size_t size_type; 86 typedef ptrdiff_t difference_type; 87 typedef _Tp value_type; 88 typedef true_type propagate_on_container_move_assignment; 89 typedef true_type is_always_equal; 90 91 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 92 allocator() _NOEXCEPT _LIBCPP_DEFAULT 93 94 template <class _Up> 95 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 96 allocator(const allocator<_Up>&) _NOEXCEPT { } 97 98 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 99 _Tp* allocate(size_t __n) { 100 if (__n > allocator_traits<allocator>::max_size(*this)) 101 __throw_length_error("allocator<T>::allocate(size_t n)" 102 " 'n' exceeds maximum supported size"); 103 if (__libcpp_is_constant_evaluated()) { 104 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); 105 } else { 106 return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); 107 } 108 } 109 110 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 111 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT { 112 if (__libcpp_is_constant_evaluated()) { 113 ::operator delete(__p); 114 } else { 115 _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 116 } 117 } 118 119 // C++20 Removed members 120 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) 121 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer; 122 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer; 123 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference; 124 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference; 125 126 template <class _Up> 127 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind { 128 typedef allocator<_Up> other; 129 }; 130 131 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 132 pointer address(reference __x) const _NOEXCEPT { 133 return _VSTD::addressof(__x); 134 } 135 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 136 const_pointer address(const_reference __x) const _NOEXCEPT { 137 return _VSTD::addressof(__x); 138 } 139 140 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 141 _Tp* allocate(size_t __n, const void*) { 142 return allocate(__n); 143 } 144 145 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT { 146 return size_type(~0) / sizeof(_Tp); 147 } 148 149 template <class _Up, class... _Args> 150 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 151 void construct(_Up* __p, _Args&&... __args) { 152 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); 153 } 154 155 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 156 void destroy(pointer __p) { 157 __p->~_Tp(); 158 } 159 #endif 160 }; 161 162 template <class _Tp> 163 class _LIBCPP_TEMPLATE_VIS allocator<const _Tp> 164 : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> > 165 { 166 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types"); 167 public: 168 typedef size_t size_type; 169 typedef ptrdiff_t difference_type; 170 typedef const _Tp value_type; 171 typedef true_type propagate_on_container_move_assignment; 172 typedef true_type is_always_equal; 173 174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 175 allocator() _NOEXCEPT _LIBCPP_DEFAULT 176 177 template <class _Up> 178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 179 allocator(const allocator<_Up>&) _NOEXCEPT { } 180 181 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 182 const _Tp* allocate(size_t __n) { 183 if (__n > allocator_traits<allocator>::max_size(*this)) 184 __throw_length_error("allocator<const T>::allocate(size_t n)" 185 " 'n' exceeds maximum supported size"); 186 if (__libcpp_is_constant_evaluated()) { 187 return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp))); 188 } else { 189 return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); 190 } 191 } 192 193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 194 void deallocate(const _Tp* __p, size_t __n) { 195 if (__libcpp_is_constant_evaluated()) { 196 ::operator delete(const_cast<_Tp*>(__p)); 197 } else { 198 _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); 199 } 200 } 201 202 // C++20 Removed members 203 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS) 204 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer; 205 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer; 206 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference; 207 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference; 208 209 template <class _Up> 210 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind { 211 typedef allocator<_Up> other; 212 }; 213 214 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 215 const_pointer address(const_reference __x) const _NOEXCEPT { 216 return _VSTD::addressof(__x); 217 } 218 219 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17 220 const _Tp* allocate(size_t __n, const void*) { 221 return allocate(__n); 222 } 223 224 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT { 225 return size_type(~0) / sizeof(_Tp); 226 } 227 228 template <class _Up, class... _Args> 229 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 230 void construct(_Up* __p, _Args&&... __args) { 231 ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...); 232 } 233 234 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY 235 void destroy(pointer __p) { 236 __p->~_Tp(); 237 } 238 #endif 239 }; 240 241 template <class _Tp, class _Up> 242 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 243 bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;} 244 245 template <class _Tp, class _Up> 246 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 247 bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;} 248 249 _LIBCPP_END_NAMESPACE_STD 250 251 #endif // _LIBCPP___MEMORY_ALLOCATOR_H 252