Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:34

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___MEMORY_ALLOCATOR_H
0011 #define _LIBCPP___CXX03___MEMORY_ALLOCATOR_H
0012 
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__memory/addressof.h>
0015 #include <__cxx03/__memory/allocate_at_least.h>
0016 #include <__cxx03/__memory/allocator_traits.h>
0017 #include <__cxx03/__type_traits/is_const.h>
0018 #include <__cxx03/__type_traits/is_constant_evaluated.h>
0019 #include <__cxx03/__type_traits/is_same.h>
0020 #include <__cxx03/__type_traits/is_void.h>
0021 #include <__cxx03/__type_traits/is_volatile.h>
0022 #include <__cxx03/__utility/forward.h>
0023 #include <__cxx03/cstddef>
0024 #include <__cxx03/new>
0025 
0026 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0027 #  pragma GCC system_header
0028 #endif
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 template <class _Tp>
0033 class allocator;
0034 
0035 #if _LIBCPP_STD_VER <= 17
0036 // These specializations shouldn't be marked _LIBCPP_DEPRECATED_IN_CXX17.
0037 // Specializing allocator<void> is deprecated, but not using it.
0038 template <>
0039 class _LIBCPP_TEMPLATE_VIS allocator<void> {
0040 public:
0041   _LIBCPP_DEPRECATED_IN_CXX17 typedef void* pointer;
0042   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
0043   _LIBCPP_DEPRECATED_IN_CXX17 typedef void value_type;
0044 
0045   template <class _Up>
0046   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0047     typedef allocator<_Up> other;
0048   };
0049 };
0050 
0051 // TODO(LLVM 20): Remove the escape hatch
0052 #  ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
0053 template <>
0054 class _LIBCPP_TEMPLATE_VIS allocator<const void> {
0055 public:
0056   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
0057   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
0058   _LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;
0059 
0060   template <class _Up>
0061   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0062     typedef allocator<_Up> other;
0063   };
0064 };
0065 #  endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
0066 #endif   // _LIBCPP_STD_VER <= 17
0067 
0068 // This class provides a non-trivial default constructor to the class that derives from it
0069 // if the condition is satisfied.
0070 //
0071 // The second template parameter exists to allow giving a unique type to __non_trivial_if,
0072 // which makes it possible to avoid breaking the ABI when making this a base class of an
0073 // existing class. Without that, imagine we have classes D1 and D2, both of which used to
0074 // have no base classes, but which now derive from __non_trivial_if. The layout of a class
0075 // that inherits from both D1 and D2 will change because the two __non_trivial_if base
0076 // classes are not allowed to share the same address.
0077 //
0078 // By making those __non_trivial_if base classes unique, we work around this problem and
0079 // it is safe to start deriving from __non_trivial_if in existing classes.
0080 template <bool _Cond, class _Unique>
0081 struct __non_trivial_if {};
0082 
0083 template <class _Unique>
0084 struct __non_trivial_if<true, _Unique> {
0085   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __non_trivial_if() _NOEXCEPT {}
0086 };
0087 
0088 // allocator
0089 //
0090 // Note: For ABI compatibility between C++20 and previous standards, we make
0091 //       allocator<void> trivial in C++20.
0092 
0093 template <class _Tp>
0094 class _LIBCPP_TEMPLATE_VIS allocator : private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> > {
0095   static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
0096   static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
0097 
0098 public:
0099   typedef size_t size_type;
0100   typedef ptrdiff_t difference_type;
0101   typedef _Tp value_type;
0102   typedef true_type propagate_on_container_move_assignment;
0103 #if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
0104   _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
0105 #endif
0106 
0107   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
0108 
0109   template <class _Up>
0110   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
0111 
0112   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* allocate(size_t __n) {
0113     if (__n > allocator_traits<allocator>::max_size(*this))
0114       __throw_bad_array_new_length();
0115     if (__libcpp_is_constant_evaluated()) {
0116       return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
0117     } else {
0118       return static_cast<_Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
0119     }
0120   }
0121 
0122 #if _LIBCPP_STD_VER >= 23
0123   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<_Tp*> allocate_at_least(size_t __n) {
0124     return {allocate(__n), __n};
0125   }
0126 #endif
0127 
0128   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(_Tp* __p, size_t __n) _NOEXCEPT {
0129     if (__libcpp_is_constant_evaluated()) {
0130       ::operator delete(__p);
0131     } else {
0132       std::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
0133     }
0134   }
0135 
0136   // C++20 Removed members
0137 #if _LIBCPP_STD_VER <= 17
0138   _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
0139   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
0140   _LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
0141   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
0142 
0143   template <class _Up>
0144   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0145     typedef allocator<_Up> other;
0146   };
0147 
0148   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI pointer address(reference __x) const _NOEXCEPT {
0149     return std::addressof(__x);
0150   }
0151   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
0152     return std::addressof(__x);
0153   }
0154 
0155   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 _Tp* allocate(size_t __n, const void*) {
0156     return allocate(__n);
0157   }
0158 
0159   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
0160     return size_type(~0) / sizeof(_Tp);
0161   }
0162 
0163   template <class _Up, class... _Args>
0164   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
0165     ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
0166   }
0167 
0168   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
0169 #endif
0170 };
0171 
0172 // TODO(LLVM 20): Remove the escape hatch
0173 #ifdef _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
0174 template <class _Tp>
0175 class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
0176     : private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> > {
0177   static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
0178 
0179 public:
0180   typedef size_t size_type;
0181   typedef ptrdiff_t difference_type;
0182   typedef const _Tp value_type;
0183   typedef true_type propagate_on_container_move_assignment;
0184 #  if _LIBCPP_STD_VER <= 23 || defined(_LIBCPP_ENABLE_CXX26_REMOVED_ALLOCATOR_MEMBERS)
0185   _LIBCPP_DEPRECATED_IN_CXX23 typedef true_type is_always_equal;
0186 #  endif
0187 
0188   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator() _NOEXCEPT = default;
0189 
0190   template <class _Up>
0191   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 allocator(const allocator<_Up>&) _NOEXCEPT {}
0192 
0193   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const _Tp* allocate(size_t __n) {
0194     if (__n > allocator_traits<allocator>::max_size(*this))
0195       __throw_bad_array_new_length();
0196     if (__libcpp_is_constant_evaluated()) {
0197       return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
0198     } else {
0199       return static_cast<const _Tp*>(std::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
0200     }
0201   }
0202 
0203 #  if _LIBCPP_STD_VER >= 23
0204   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr allocation_result<const _Tp*> allocate_at_least(size_t __n) {
0205     return {allocate(__n), __n};
0206   }
0207 #  endif
0208 
0209   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void deallocate(const _Tp* __p, size_t __n) {
0210     if (__libcpp_is_constant_evaluated()) {
0211       ::operator delete(const_cast<_Tp*>(__p));
0212     } else {
0213       std::__libcpp_deallocate((void*)const_cast<_Tp*>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
0214     }
0215   }
0216 
0217   // C++20 Removed members
0218 #  if _LIBCPP_STD_VER <= 17
0219   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
0220   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
0221   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
0222   _LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
0223 
0224   template <class _Up>
0225   struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
0226     typedef allocator<_Up> other;
0227   };
0228 
0229   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI const_pointer address(const_reference __x) const _NOEXCEPT {
0230     return std::addressof(__x);
0231   }
0232 
0233   _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX17 const _Tp* allocate(size_t __n, const void*) {
0234     return allocate(__n);
0235   }
0236 
0237   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
0238     return size_type(~0) / sizeof(_Tp);
0239   }
0240 
0241   template <class _Up, class... _Args>
0242   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void construct(_Up* __p, _Args&&... __args) {
0243     ::new ((void*)__p) _Up(std::forward<_Args>(__args)...);
0244   }
0245 
0246   _LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_HIDE_FROM_ABI void destroy(pointer __p) { __p->~_Tp(); }
0247 #  endif
0248 };
0249 #endif // _LIBCPP_ENABLE_REMOVED_ALLOCATOR_CONST
0250 
0251 template <class _Tp, class _Up>
0252 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
0253 operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
0254   return true;
0255 }
0256 
0257 #if _LIBCPP_STD_VER <= 17
0258 
0259 template <class _Tp, class _Up>
0260 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {
0261   return false;
0262 }
0263 
0264 #endif
0265 
0266 _LIBCPP_END_NAMESPACE_STD
0267 
0268 #endif // _LIBCPP___CXX03___MEMORY_ALLOCATOR_H