File indexing completed on 2026-05-03 08:13:57
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___NEW_ALLOCATE_H
0010 #define _LIBCPP___NEW_ALLOCATE_H
0011
0012 #include <__config>
0013 #include <__cstddef/max_align_t.h>
0014 #include <__cstddef/size_t.h>
0015 #include <__new/align_val_t.h>
0016 #include <__new/global_new_delete.h> // for _LIBCPP_HAS_SIZED_DEALLOCATION
0017 #include <__type_traits/type_identity.h>
0018 #include <__utility/element_count.h>
0019
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 # pragma GCC system_header
0022 #endif
0023
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025
0026 _LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {
0027 #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__
0028 return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
0029 #else
0030 return __align > _LIBCPP_ALIGNOF(max_align_t);
0031 #endif
0032 }
0033
0034 template <class... _Args>
0035 _LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {
0036 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
0037 return __builtin_operator_new(__args...);
0038 #else
0039 return ::operator new(__args...);
0040 #endif
0041 }
0042
0043 template <class... _Args>
0044 _LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {
0045 #if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)
0046 __builtin_operator_delete(__args...);
0047 #else
0048 ::operator delete(__args...);
0049 #endif
0050 }
0051
0052 template <class _Tp>
0053 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp*
0054 __libcpp_allocate(__element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) {
0055 size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);
0056 #if _LIBCPP_HAS_ALIGNED_ALLOCATION
0057 if (__is_overaligned_for_new(__align)) {
0058 const align_val_t __align_val = static_cast<align_val_t>(__align);
0059 return static_cast<_Tp*>(std::__libcpp_operator_new(__size, __align_val));
0060 }
0061 #endif
0062
0063 (void)__align;
0064 return static_cast<_Tp*>(std::__libcpp_operator_new(__size));
0065 }
0066
0067 #if _LIBCPP_HAS_SIZED_DEALLOCATION
0068 # define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) __VA_ARGS__
0069 #else
0070 # define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...)
0071 #endif
0072
0073 template <class _Tp>
0074 inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(
0075 __type_identity_t<_Tp>* __ptr, __element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
0076 size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);
0077 (void)__size;
0078 #if !_LIBCPP_HAS_ALIGNED_ALLOCATION
0079 (void)__align;
0080 return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));
0081 #else
0082 if (__is_overaligned_for_new(__align)) {
0083 const align_val_t __align_val = static_cast<align_val_t>(__align);
0084 return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size), __align_val);
0085 } else {
0086 return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));
0087 }
0088 #endif
0089 }
0090
0091 #undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION
0092
0093 template <class _Tp>
0094 inline _LIBCPP_HIDE_FROM_ABI void
0095 __libcpp_deallocate_unsized(__type_identity_t<_Tp>* __ptr, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {
0096 #if !_LIBCPP_HAS_ALIGNED_ALLOCATION
0097 (void)__align;
0098 return std::__libcpp_operator_delete(__ptr);
0099 #else
0100 if (__is_overaligned_for_new(__align)) {
0101 const align_val_t __align_val = static_cast<align_val_t>(__align);
0102 return std::__libcpp_operator_delete(__ptr, __align_val);
0103 } else {
0104 return std::__libcpp_operator_delete(__ptr);
0105 }
0106 #endif
0107 }
0108 _LIBCPP_END_NAMESPACE_STD
0109
0110 #endif