Path: blob/main/contrib/llvm-project/libcxx/include/__new/allocate.h
213766 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___NEW_ALLOCATE_H9#define _LIBCPP___NEW_ALLOCATE_H1011#include <__config>12#include <__cstddef/max_align_t.h>13#include <__cstddef/size_t.h>14#include <__new/align_val_t.h>15#include <__new/global_new_delete.h> // for _LIBCPP_HAS_SIZED_DEALLOCATION16#include <__type_traits/type_identity.h>17#include <__utility/element_count.h>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_BEGIN_NAMESPACE_STD2425_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT {26#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__27return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__;28#else29return __align > _LIBCPP_ALIGNOF(max_align_t);30#endif31}3233template <class... _Args>34_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) {35#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)36return __builtin_operator_new(__args...);37#else38return ::operator new(__args...);39#endif40}4142template <class... _Args>43_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {44#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete)45__builtin_operator_delete(__args...);46#else47::operator delete(__args...);48#endif49}5051template <class _Tp>52inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _Tp*53__libcpp_allocate(__element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) {54size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);55#if _LIBCPP_HAS_ALIGNED_ALLOCATION56if (__is_overaligned_for_new(__align)) {57const align_val_t __align_val = static_cast<align_val_t>(__align);58return static_cast<_Tp*>(std::__libcpp_operator_new(__size, __align_val));59}60#endif6162(void)__align;63return static_cast<_Tp*>(std::__libcpp_operator_new(__size));64}6566#if _LIBCPP_HAS_SIZED_DEALLOCATION67# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) __VA_ARGS__68#else69# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) /* nothing */70#endif7172template <class _Tp>73inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(74__type_identity_t<_Tp>* __ptr, __element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {75size_t __size = static_cast<size_t>(__n) * sizeof(_Tp);76(void)__size;77#if !_LIBCPP_HAS_ALIGNED_ALLOCATION78(void)__align;79return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));80#else81if (__is_overaligned_for_new(__align)) {82const align_val_t __align_val = static_cast<align_val_t>(__align);83return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size), __align_val);84} else {85return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size));86}87#endif88}8990#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION9192template <class _Tp>93inline _LIBCPP_HIDE_FROM_ABI void94__libcpp_deallocate_unsized(__type_identity_t<_Tp>* __ptr, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT {95#if !_LIBCPP_HAS_ALIGNED_ALLOCATION96(void)__align;97return std::__libcpp_operator_delete(__ptr);98#else99if (__is_overaligned_for_new(__align)) {100const align_val_t __align_val = static_cast<align_val_t>(__align);101return std::__libcpp_operator_delete(__ptr, __align_val);102} else {103return std::__libcpp_operator_delete(__ptr);104}105#endif106}107_LIBCPP_END_NAMESPACE_STD108109#endif // _LIBCPP___NEW_ALLOCATE_H110111112