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/allocate.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
61template <class _Tp>
62class allocator
63// TODO(LLVM 24): Remove the opt-out
64#ifdef _LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR
65 : __non_trivially_default_constructible_if<!is_void<_Tp>::value, allocator<_Tp> >
66#endif
67{
68 static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
69 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
70
71public:
72 typedef size_t size_type;
73 typedef ptrdiff_t difference_type;
74 typedef _Tp value_type;
75 typedef true_type propagate_on_container_move_assignment;
76#if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
77 _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
78#endif
79
80 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
81
82 template <class _Up>
83 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
84
85 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
86 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
87 if (__n > allocator_traits<allocator>::max_size(*this))
88 std::__throw_bad_array_new_length();
89 if (__libcpp_is_constant_evaluated()) {
90 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
91 } else {
92 return std::__libcpp_allocate<_Tp>(__element_count(__n));
93 }
94 }
95
96#if _LIBCPP_STD_VER >= 23
97 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
98 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
99 return {allocate(__n), __n};
100 }
101#endif
102
103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
104 if (__libcpp_is_constant_evaluated()) {
105 ::operator delete(__p);
106 } else {
107 std::__libcpp_deallocate<_Tp>(__p, __element_count(__n));
108 }
109 }
110
111 // C++20 Removed members
112#if _LIBCPP_STD_VER <= 17
113 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
114 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
115 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
116 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
117
118 template <class _Up>
119 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
120 typedef allocator<_Up> other;
121 };
122
123 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
124 return std::addressof(__x);
125 }
126 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer
127 address(const_reference __x) const _NOEXCEPT {
128 return std::addressof(__x);
129 }
130
131 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
132 return allocate(__n);
133 }
134
135 [[__nodiscard__]] _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
136 return size_type(~0) / sizeof(_Tp);
137 }
138
139 template <class _Up, class... _Args>
140 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
141 ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
142 }
143
144 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
145#endif
146};
147
148template <class _Tp, class _Up>
149inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
150operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
151 return true;
152}
153
154#if _LIBCPP_STD_VER <= 17
155
156template <class _Tp, class _Up>
157inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
158 return false;
159}
160
161#endif
162
163_LIBCPP_END_NAMESPACE_STD
164
165#endif // _LIBCPP___MEMORY_ALLOCATOR_H
166