File indexing completed on 2026-05-03 08:13:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___MEMORY_ALLOCATOR_H
0011 #define _LIBCPP___MEMORY_ALLOCATOR_H
0012
0013 #include <__config>
0014 #include <__cstddef/ptrdiff_t.h>
0015 #include <__cstddef/size_t.h>
0016 #include <__memory/addressof.h>
0017 #include <__memory/allocate_at_least.h>
0018 #include <__memory/allocator_traits.h>
0019 #include <__new/allocate.h>
0020 #include <__new/exceptions.h>
0021 #include <__type_traits/is_const.h>
0022 #include <__type_traits/is_constant_evaluated.h>
0023 #include <__type_traits/is_same.h>
0024 #include <__type_traits/is_void.h>
0025 #include <__type_traits/is_volatile.h>
0026 #include <__utility/forward.h>
0027
0028 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0029 # pragma GCC system_header
0030 #endif
0031
0032 _LIBCPP_BEGIN_NAMESPACE_STD
0033
0034 template <class _Tp>
0035 class allocator;
0036
0037 #if _LIBCPP_STD_VER <= 17
0038
0039
0040 template <>
0041 class _LIBCPP_TEMPLATE_VIS allocator<void> {
0042 public:
0043 _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
0044 _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
0045 _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
0046
0047 template <class _Up>
0048 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0049 typedef allocator<_Up> other;
0050 };
0051 };
0052 #endif
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 template <bool _Cond, class _Unique>
0067 struct __non_trivial_if {};
0068
0069 template <class _Unique>
0070 struct __non_trivial_if<true, _Unique> {
0071 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}
0072 };
0073
0074
0075
0076
0077
0078
0079 template <class _Tp>
0080 class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
0081 static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
0082 static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
0083
0084 public:
0085 typedef size_t size_type;
0086 typedef ptrdiff_t difference_type;
0087 typedef _Tp value_type;
0088 typedef true_type propagate_on_container_move_assignment;
0089 #if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
0090 _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
0091 #endif
0092
0093 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
0094
0095 template <class _Up>
0096 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
0097
0098 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
0099 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
0100 if (__n > allocator_traits<allocator>::max_size(*this))
0101 __throw_bad_array_new_length();
0102 if (__libcpp_is_constant_evaluated()) {
0103 return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
0104 } else {
0105 return std::__libcpp_allocate<_Tp>(__element_count(__n));
0106 }
0107 }
0108
0109 #if _LIBCPP_STD_VER >= 23
0110 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
0111 static_assert(sizeof(_Tp) >= 0, "cannot allocate memory for an incomplete type");
0112 return {allocate(__n), __n};
0113 }
0114 #endif
0115
0116 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
0117 if (__libcpp_is_constant_evaluated()) {
0118 ::operator delete(__p);
0119 } else {
0120 std::__libcpp_deallocate<_Tp>(__p, __element_count(__n));
0121 }
0122 }
0123
0124
0125 #if _LIBCPP_STD_VER <= 17
0126 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
0127 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
0128 _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
0129 _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
0130
0131 template <class _Up>
0132 struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0133 typedef allocator<_Up> other;
0134 };
0135
0136 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
0137 return std::addressof(__x);
0138 }
0139 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
0140 return std::addressof(__x);
0141 }
0142
0143 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
0144 return allocate(__n);
0145 }
0146
0147 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
0148 return size_type(~0) / sizeof(_Tp);
0149 }
0150
0151 template <class _Up, class... _Args>
0152 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
0153 ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
0154 }
0155
0156 _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
0157 #endif
0158 };
0159
0160 template <class _Tp, class _Up>
0161 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0162 operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
0163 return true;
0164 }
0165
0166 #if _LIBCPP_STD_VER <= 17
0167
0168 template <class _Tp, class _Up>
0169 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
0170 return false;
0171 }
0172
0173 #endif
0174
0175 _LIBCPP_END_NAMESPACE_STD
0176
0177 #endif