xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/allocator.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric 
10fe6060f1SDimitry Andric #ifndef _LIBCPP___MEMORY_ALLOCATOR_H
11fe6060f1SDimitry Andric #define _LIBCPP___MEMORY_ALLOCATOR_H
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include <__config>
1406c3fb27SDimitry Andric #include <__memory/addressof.h>
1581ad6265SDimitry Andric #include <__memory/allocate_at_least.h>
16fe6060f1SDimitry Andric #include <__memory/allocator_traits.h>
17*0fca6ea1SDimitry Andric #include <__type_traits/is_const.h>
18bdd1243dSDimitry Andric #include <__type_traits/is_constant_evaluated.h>
19bdd1243dSDimitry Andric #include <__type_traits/is_same.h>
20bdd1243dSDimitry Andric #include <__type_traits/is_void.h>
21bdd1243dSDimitry Andric #include <__type_traits/is_volatile.h>
22fe6060f1SDimitry Andric #include <__utility/forward.h>
23fe6060f1SDimitry Andric #include <cstddef>
24fe6060f1SDimitry Andric #include <new>
25fe6060f1SDimitry Andric 
26fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27fe6060f1SDimitry Andric #  pragma GCC system_header
28fe6060f1SDimitry Andric #endif
29fe6060f1SDimitry Andric 
30fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
31fe6060f1SDimitry Andric 
32cb14a3feSDimitry Andric template <class _Tp>
33cb14a3feSDimitry Andric class allocator;
34fe6060f1SDimitry Andric 
35*0fca6ea1SDimitry Andric #if _LIBCPP_STD_VER <= 17
3681ad6265SDimitry Andric // These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
3781ad6265SDimitry Andric // Specializing allocator<void> is deprecated, but not using it.
38fe6060f1SDimitry Andric template <>
39cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS allocator<void> {
40fe6060f1SDimitry Andric public:
41fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
42fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
43fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
44fe6060f1SDimitry Andric 
45cb14a3feSDimitry Andric   template <class _Up>
46cb14a3feSDimitry Andric   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
47cb14a3feSDimitry Andric     typedef allocator<_Up> other;
48cb14a3feSDimitry Andric   };
49fe6060f1SDimitry Andric };
50fe6060f1SDimitry Andric 
51*0fca6ea1SDimitry Andric // TODO(LLVM 20): Remove the escape hatch
52*0fca6ea1SDimitry Andric #  ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
53fe6060f1SDimitry Andric template <>
54cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS allocator<const void> {
55fe6060f1SDimitry Andric public:
56fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
57fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
58fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;
59fe6060f1SDimitry Andric 
60cb14a3feSDimitry Andric   template <class _Up>
61cb14a3feSDimitry Andric   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
62cb14a3feSDimitry Andric     typedef allocator<_Up> other;
63cb14a3feSDimitry Andric   };
64fe6060f1SDimitry Andric };
65*0fca6ea1SDimitry Andric #  endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
66*0fca6ea1SDimitry Andric #endif   // _LIBCPP_STD_VER <= 17
67fe6060f1SDimitry Andric 
68fe6060f1SDimitry Andric // This class provides a non-trivial default constructor to the class that derives from it
69fe6060f1SDimitry Andric // if the condition is satisfied.
70fe6060f1SDimitry Andric //
71fe6060f1SDimitry Andric // The second template parameter exists to allow giving a unique type to __non_trivial_if,
72fe6060f1SDimitry Andric // which makes it possible to avoid breaking the ABI when making this a base class of an
73fe6060f1SDimitry Andric // existing class. Without that, imagine we have classes D1 and D2, both of which used to
74fe6060f1SDimitry Andric // have no base classes, but which now derive from __non_trivial_if. The layout of a class
75fe6060f1SDimitry Andric // that inherits from both D1 and D2 will change because the two __non_trivial_if base
76fe6060f1SDimitry Andric // classes are not allowed to share the same address.
77fe6060f1SDimitry Andric //
78fe6060f1SDimitry Andric // By making those __non_trivial_if base classes unique, we work around this problem and
79fe6060f1SDimitry Andric // it is safe to start deriving from __non_trivial_if in existing classes.
80fe6060f1SDimitry Andric template <bool _Cond, class _Unique>
81fe6060f1SDimitry Andric struct __non_trivial_if {};
82fe6060f1SDimitry Andric 
83fe6060f1SDimitry Andric template <class _Unique>
84fe6060f1SDimitry Andric struct __non_trivial_if<true, _Unique> {
85cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}
86fe6060f1SDimitry Andric };
87fe6060f1SDimitry Andric 
88fe6060f1SDimitry Andric // allocator
89fe6060f1SDimitry Andric //
90fe6060f1SDimitry Andric // Note: For ABI compatibility between C++20 and previous standards, we make
91fe6060f1SDimitry Andric //       allocator<void> trivial in C++20.
92fe6060f1SDimitry Andric 
93fe6060f1SDimitry Andric template <class _Tp>
94cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
95*0fca6ea1SDimitry Andric   static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
96349cc55cSDimitry Andric   static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
97cb14a3feSDimitry Andric 
98fe6060f1SDimitry Andric public:
99fe6060f1SDimitry Andric   typedef size_t size_type;
100fe6060f1SDimitry Andric   typedef ptrdiff_t difference_type;
101fe6060f1SDimitry Andric   typedef _Tp value_type;
102fe6060f1SDimitry Andric   typedef true_type propagate_on_container_move_assignment;
1037a6dacacSDimitry Andric #if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
1047a6dacacSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
1057a6dacacSDimitry Andric #endif
106fe6060f1SDimitry Andric 
10706c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric   template <class _Up>
110cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
111fe6060f1SDimitry Andric 
112*0fca6ea1SDimitry Andric   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
113fe6060f1SDimitry Andric     if (__n > allocator_traits<allocator>::max_size(*this))
114349cc55cSDimitry Andric       __throw_bad_array_new_length();
115fe6060f1SDimitry Andric     if (__libcpp_is_constant_evaluated()) {
116fe6060f1SDimitry Andric       return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
117fe6060f1SDimitry Andric     } else {
1185f757f3fSDimitry Andric       return static_cast<_Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
119fe6060f1SDimitry Andric     }
120fe6060f1SDimitry Andric   }
121fe6060f1SDimitry Andric 
12206c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 23
123cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
12481ad6265SDimitry Andric     return {allocate(__n), __n};
12581ad6265SDimitry Andric   }
12681ad6265SDimitry Andric #endif
12781ad6265SDimitry Andric 
128cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
129fe6060f1SDimitry Andric     if (__libcpp_is_constant_evaluated()) {
130fe6060f1SDimitry Andric       ::operator delete(__p);
131fe6060f1SDimitry Andric     } else {
1325f757f3fSDimitry Andric       std::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
133fe6060f1SDimitry Andric     }
134fe6060f1SDimitry Andric   }
135fe6060f1SDimitry Andric 
136fe6060f1SDimitry Andric   // C++20 Removed members
137*0fca6ea1SDimitry Andric #if _LIBCPP_STD_VER <= 17
138fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
139fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
140fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
141fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
142fe6060f1SDimitry Andric 
143fe6060f1SDimitry Andric   template <class _Up>
144fe6060f1SDimitry Andric   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
145fe6060f1SDimitry Andric     typedef allocator<_Up> other;
146fe6060f1SDimitry Andric   };
147fe6060f1SDimitry Andric 
148cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
1495f757f3fSDimitry Andric     return std::addressof(__x);
150fe6060f1SDimitry Andric   }
151cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
1525f757f3fSDimitry Andric     return std::addressof(__x);
153fe6060f1SDimitry Andric   }
154fe6060f1SDimitry Andric 
155*0fca6ea1SDimitry Andric   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
156fe6060f1SDimitry Andric     return allocate(__n);
157fe6060f1SDimitry Andric   }
158fe6060f1SDimitry Andric 
1595f757f3fSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
160fe6060f1SDimitry Andric     return size_type(~0) / sizeof(_Tp);
161fe6060f1SDimitry Andric   }
162fe6060f1SDimitry Andric 
163fe6060f1SDimitry Andric   template <class _Up, class... _Args>
164cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
1655f757f3fSDimitry Andric     ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
166fe6060f1SDimitry Andric   }
167fe6060f1SDimitry Andric 
168cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
169fe6060f1SDimitry Andric #endif
170fe6060f1SDimitry Andric };
171fe6060f1SDimitry Andric 
172*0fca6ea1SDimitry Andric // TODO(LLVM 20): Remove the escape hatch
173*0fca6ea1SDimitry Andric #ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
174fe6060f1SDimitry Andric template <class _Tp>
175fe6060f1SDimitry Andric class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
176cb14a3feSDimitry Andric     : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> > {
177349cc55cSDimitry Andric   static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
178cb14a3feSDimitry Andric 
179fe6060f1SDimitry Andric public:
180fe6060f1SDimitry Andric   typedef size_t size_type;
181fe6060f1SDimitry Andric   typedef ptrdiff_t difference_type;
182fe6060f1SDimitry Andric   typedef const _Tp value_type;
183fe6060f1SDimitry Andric   typedef true_type propagate_on_container_move_assignment;
1847a6dacacSDimitry Andric #  if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
1857a6dacacSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
1867a6dacacSDimitry Andric #  endif
187fe6060f1SDimitry Andric 
18806c3fb27SDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric   template <class _Up>
191cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
192fe6060f1SDimitry Andric 
193*0fca6ea1SDimitry Andric   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const _Tp* allocate(size_t __n) {
194fe6060f1SDimitry Andric     if (__n > allocator_traits<allocator>::max_size(*this))
195349cc55cSDimitry Andric       __throw_bad_array_new_length();
196fe6060f1SDimitry Andric     if (__libcpp_is_constant_evaluated()) {
197fe6060f1SDimitry Andric       return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
198fe6060f1SDimitry Andric     } else {
1995f757f3fSDimitry Andric       return static_cast<const _Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
200fe6060f1SDimitry Andric     }
201fe6060f1SDimitry Andric   }
202fe6060f1SDimitry Andric 
20306c3fb27SDimitry Andric #  if _LIBCPP_STD_VER >= 23
204cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<const _Tp*> allocate_at_least(size_t __n) {
20581ad6265SDimitry Andric     return {allocate(__n), __n};
20681ad6265SDimitry Andric   }
20781ad6265SDimitry Andric #  endif
20881ad6265SDimitry Andric 
209cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(const _Tp* __p, size_t __n) {
210fe6060f1SDimitry Andric     if (__libcpp_is_constant_evaluated()) {
211fe6060f1SDimitry Andric       ::operator delete(const_cast<_Tp*>(__p));
212fe6060f1SDimitry Andric     } else {
2135f757f3fSDimitry Andric       std::__libcpp_deallocate((void*)const_cast<_Tp*>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
214fe6060f1SDimitry Andric     }
215fe6060f1SDimitry Andric   }
216fe6060f1SDimitry Andric 
217fe6060f1SDimitry Andric   // C++20 Removed members
218*0fca6ea1SDimitry Andric #  if _LIBCPP_STD_VER <= 17
219fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
220fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
221fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
222fe6060f1SDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
223fe6060f1SDimitry Andric 
224fe6060f1SDimitry Andric   template <class _Up>
225fe6060f1SDimitry Andric   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
226fe6060f1SDimitry Andric     typedef allocator<_Up> other;
227fe6060f1SDimitry Andric   };
228fe6060f1SDimitry Andric 
229cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
2305f757f3fSDimitry Andric     return std::addressof(__x);
231fe6060f1SDimitry Andric   }
232fe6060f1SDimitry Andric 
233*0fca6ea1SDimitry Andric   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 const _Tp* allocate(size_t __n, const void*) {
234fe6060f1SDimitry Andric     return allocate(__n);
235fe6060f1SDimitry Andric   }
236fe6060f1SDimitry Andric 
2375f757f3fSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
238fe6060f1SDimitry Andric     return size_type(~0) / sizeof(_Tp);
239fe6060f1SDimitry Andric   }
240fe6060f1SDimitry Andric 
241fe6060f1SDimitry Andric   template <class _Up, class... _Args>
242cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
2435f757f3fSDimitry Andric     ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
244fe6060f1SDimitry Andric   }
245fe6060f1SDimitry Andric 
246cb14a3feSDimitry Andric   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
247fe6060f1SDimitry Andric #  endif
248fe6060f1SDimitry Andric };
249*0fca6ea1SDimitry Andric #endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
250fe6060f1SDimitry Andric 
251fe6060f1SDimitry Andric template <class _Tp, class _Up>
252cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
253cb14a3feSDimitry Andric operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
254cb14a3feSDimitry Andric   return true;
255cb14a3feSDimitry Andric }
256fe6060f1SDimitry Andric 
25706c3fb27SDimitry Andric #if _LIBCPP_STD_VER <= 17
25806c3fb27SDimitry Andric 
259fe6060f1SDimitry Andric template <class _Tp, class _Up>
260cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
261cb14a3feSDimitry Andric   return false;
262cb14a3feSDimitry Andric }
263fe6060f1SDimitry Andric 
26406c3fb27SDimitry Andric #endif
26506c3fb27SDimitry Andric 
266fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
267fe6060f1SDimitry Andric 
268fe6060f1SDimitry Andric #endif // _LIBCPP___MEMORY_ALLOCATOR_H
269