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 = 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_bad_array_new_length();
102         if (__libcpp_is_constant_evaluated()) {
103             return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
104         } else {
105             return static_cast<_Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
106         }
107     }
108 
109     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
110     void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
111         if (__libcpp_is_constant_evaluated()) {
112             ::operator delete(__p);
113         } else {
114             _VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
115         }
116     }
117 
118     // C++20 Removed members
119 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
120     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp*       pointer;
121     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
122     _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp&       reference;
123     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
124 
125     template <class _Up>
126     struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
127         typedef allocator<_Up> other;
128     };
129 
130     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
131     pointer address(reference __x) const _NOEXCEPT {
132         return _VSTD::addressof(__x);
133     }
134     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
135     const_pointer address(const_reference __x) const _NOEXCEPT {
136         return _VSTD::addressof(__x);
137     }
138 
139     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
140     _Tp* allocate(size_t __n, const void*) {
141         return allocate(__n);
142     }
143 
144     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
145         return size_type(~0) / sizeof(_Tp);
146     }
147 
148     template <class _Up, class... _Args>
149     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
150     void construct(_Up* __p, _Args&&... __args) {
151         ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
152     }
153 
154     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
155     void destroy(pointer __p) {
156         __p->~_Tp();
157     }
158 #endif
159 };
160 
161 template <class _Tp>
162 class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
163     : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
164 {
165     static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
166 public:
167     typedef size_t      size_type;
168     typedef ptrdiff_t   difference_type;
169     typedef const _Tp   value_type;
170     typedef true_type   propagate_on_container_move_assignment;
171     typedef true_type   is_always_equal;
172 
173     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
174     allocator() _NOEXCEPT = default;
175 
176     template <class _Up>
177     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
178     allocator(const allocator<_Up>&) _NOEXCEPT { }
179 
180     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
181     const _Tp* allocate(size_t __n) {
182         if (__n > allocator_traits<allocator>::max_size(*this))
183             __throw_bad_array_new_length();
184         if (__libcpp_is_constant_evaluated()) {
185             return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
186         } else {
187             return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
188         }
189     }
190 
191     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
192     void deallocate(const _Tp* __p, size_t __n) {
193         if (__libcpp_is_constant_evaluated()) {
194             ::operator delete(const_cast<_Tp*>(__p));
195         } else {
196             _VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
197         }
198     }
199 
200     // C++20 Removed members
201 #if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
202     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
203     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
204     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
205     _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
206 
207     template <class _Up>
208     struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
209         typedef allocator<_Up> other;
210     };
211 
212     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
213     const_pointer address(const_reference __x) const _NOEXCEPT {
214         return _VSTD::addressof(__x);
215     }
216 
217     _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
218     const _Tp* allocate(size_t __n, const void*) {
219         return allocate(__n);
220     }
221 
222     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
223         return size_type(~0) / sizeof(_Tp);
224     }
225 
226     template <class _Up, class... _Args>
227     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
228     void construct(_Up* __p, _Args&&... __args) {
229         ::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
230     }
231 
232     _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
233     void destroy(pointer __p) {
234         __p->~_Tp();
235     }
236 #endif
237 };
238 
239 template <class _Tp, class _Up>
240 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
241 bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
242 
243 template <class _Tp, class _Up>
244 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
245 bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
246 
247 _LIBCPP_END_NAMESPACE_STD
248 
249 #endif // _LIBCPP___MEMORY_ALLOCATOR_H
250