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 <__cstddef/ptrdiff_t.h>
15#include <__cstddef/size_t.h>
16#include <__memory/addressof.h>
17#include <__memory/allocator_traits.h>
18#include <__new/align_val_t.h>
19#include <__new/exceptions.h>
20#include <__type_traits/is_const.h>
21#include <__type_traits/is_constant_evaluated.h>
22#include <__type_traits/is_same.h>
23#include <__type_traits/is_void.h>
24#include <__type_traits/is_volatile.h>
25#include <__utility/forward.h>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_BEGIN_NAMESPACE_STD
32
33template <class _Tp>
34class allocator;
35
36#if _LIBCPP_STD_VER <= 17
37// These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
38// Specializing allocator<void> is deprecated, but not using it.
39template <>
40class allocator<void> {
41public:
42 _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
43 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
44 _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
45
46 template <class _Up>
47 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
48 typedef allocator<_Up> other;
49 };
50};
51#endif // _LIBCPP_STD_VER <= 17
52
53template <bool, class _Unique>
54struct __non_trivially_default_constructible_if {};
55
56template <class _Unique>
57struct __non_trivially_default_constructible_if<true, _Unique> {
58 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivially_default_constructible_if() {}
59};
60
61// TODO(LLVM 26): Remove these wrappers and call them unconditionally in `allocator::allocate` and
62// `allocator::deallocate`. These are here to the `__builtin_operator`s are never in a constant
63// context due to a Clang bug.
64template <class _Tp, class... _Args>
65_Tp* __operator_new_wrapper(size_t __count, _Args&&... __args) {
66 return static_cast<_Tp*>(__builtin_operator_new(__count * sizeof(_Tp), std::forward<_Args>(__args)...));
67}
68
69template <class... _Args>
70void __operator_delete_wrapper(_Args&&... __args) {
71 return __builtin_operator_delete(std::forward<_Args>(__args)...);
72}
73
74template <class _Tp>
75class allocator
76// TODO(LLVM 25): Remove the opt-out
77#ifdef _LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR
78 : __non_trivially_default_constructible_if<!is_void<_Tp>::value, allocator<_Tp> >
79#endif
80{
81 static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
82 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
83
84public:
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#if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
90 _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
91#endif
92
93 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
94
95 template <class _Up>
96 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
97
98 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
99 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
100 if (__n > allocator_traits<allocator>::max_size(*this))
101 std::__throw_bad_array_new_length();
102 if (__libcpp_is_constant_evaluated())
103 return static_cast<_Tp*>(::operator new(sizeof(_Tp) * __n));
104#if _LIBCPP_HAS_ALIGNED_ALLOCATION
105 if _LIBCPP_CONSTEXPR (_LIBCPP_ALIGNOF(value_type) > _LIBCPP_DEFAULT_NEW_ALIGNMENT)
106 return std::__operator_new_wrapper<value_type>(__n, static_cast<align_val_t>(_LIBCPP_ALIGNOF(value_type)));
107#endif
108 return std::__operator_new_wrapper<value_type>(__n);
109 }
110
111#if _LIBCPP_STD_VER >= 23
112 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
113 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
114 return {allocate(__n), __n};
115 }
116#endif
117
118#if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309L
119# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) __VA_ARGS__
120#else
121# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) /* nothing */
122#endif
123
124 _LIBCPP_HIDE_FROM_ABI
125 _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, [[__maybe_unused__]] size_t __n) _NOEXCEPT {
126 if (__libcpp_is_constant_evaluated())
127 return ::operator delete(__p);
128#if _LIBCPP_HAS_ALIGNED_ALLOCATION
129 if (_LIBCPP_ALIGNOF(value_type) > _LIBCPP_DEFAULT_NEW_ALIGNMENT)
130 return std::__operator_delete_wrapper(__p _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __n * sizeof(value_type)),
131 static_cast<align_val_t>(_LIBCPP_ALIGNOF(value_type)));
132#endif
133 return std::__operator_delete_wrapper(__p _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __n * sizeof(value_type)));
134 }
135
136#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION
137
138 // C++20 Removed members
139#if _LIBCPP_STD_VER <= 17
140 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
141 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
142 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
143 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
144
145 template <class _Up>
146 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
147 typedef allocator<_Up> other;
148 };
149
150 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
151 return std::addressof(__x);
152 }
153 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
154 address(const_reference __x) const _NOEXCEPT {
155 return std::addressof(__x);
156 }
157
158 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
159 return allocate(__n);
160 }
161
162 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
163 return size_type(~0) / sizeof(_Tp);
164 }
165
166 template <class _Up, class... _Args>
167 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
168 ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
169 }
170
171 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
172#endif
173};
174
175template <class _Tp, class _Up>
176inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
177operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
178 return true;
179}
180
181#if _LIBCPP_STD_VER <= 17
182
183template <class _Tp, class _Up>
184inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
185 return false;
186}
187
188#endif
189
190_LIBCPP_END_NAMESPACE_STD
191
192#endif // _LIBCPP___MEMORY_ALLOCATOR_H
193