|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef _LIBCPP___NEW_ALLOCATE_H |
| 10 | +#define _LIBCPP___NEW_ALLOCATE_H |
| 11 | + |
| 12 | +#include <__config> |
| 13 | +#include <__cstddef/max_align_t.h> |
| 14 | +#include <__cstddef/size_t.h> |
| 15 | +#include <__new/align_val_t.h> |
| 16 | +#include <__new/global_new_delete.h> // for _LIBCPP_HAS_SIZED_DEALLOCATION |
| 17 | + |
| 18 | +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | +# pragma GCC system_header |
| 20 | +#endif |
| 21 | + |
| 22 | +_LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | + |
| 24 | +_LIBCPP_CONSTEXPR inline _LIBCPP_HIDE_FROM_ABI bool __is_overaligned_for_new(size_t __align) _NOEXCEPT { |
| 25 | +#ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ |
| 26 | + return __align > __STDCPP_DEFAULT_NEW_ALIGNMENT__; |
| 27 | +#else |
| 28 | + return __align > _LIBCPP_ALIGNOF(max_align_t); |
| 29 | +#endif |
| 30 | +} |
| 31 | + |
| 32 | +template <class... _Args> |
| 33 | +_LIBCPP_HIDE_FROM_ABI void* __libcpp_operator_new(_Args... __args) { |
| 34 | +#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) |
| 35 | + return __builtin_operator_new(__args...); |
| 36 | +#else |
| 37 | + return ::operator new(__args...); |
| 38 | +#endif |
| 39 | +} |
| 40 | + |
| 41 | +template <class... _Args> |
| 42 | +_LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT { |
| 43 | +#if __has_builtin(__builtin_operator_new) && __has_builtin(__builtin_operator_delete) |
| 44 | + __builtin_operator_delete(__args...); |
| 45 | +#else |
| 46 | + ::operator delete(__args...); |
| 47 | +#endif |
| 48 | +} |
| 49 | + |
| 50 | +inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) { |
| 51 | +#if _LIBCPP_HAS_ALIGNED_ALLOCATION |
| 52 | + if (__is_overaligned_for_new(__align)) { |
| 53 | + const align_val_t __align_val = static_cast<align_val_t>(__align); |
| 54 | + return __libcpp_operator_new(__size, __align_val); |
| 55 | + } |
| 56 | +#endif |
| 57 | + |
| 58 | + (void)__align; |
| 59 | + return __libcpp_operator_new(__size); |
| 60 | +} |
| 61 | + |
| 62 | +template <class... _Args> |
| 63 | +_LIBCPP_HIDE_FROM_ABI void __do_deallocate_handle_size(void* __ptr, size_t __size, _Args... __args) _NOEXCEPT { |
| 64 | +#if !_LIBCPP_HAS_SIZED_DEALLOCATION |
| 65 | + (void)__size; |
| 66 | + return std::__libcpp_operator_delete(__ptr, __args...); |
| 67 | +#else |
| 68 | + return std::__libcpp_operator_delete(__ptr, __size, __args...); |
| 69 | +#endif |
| 70 | +} |
| 71 | + |
| 72 | +inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) _NOEXCEPT { |
| 73 | +#if !_LIBCPP_HAS_ALIGNED_ALLOCATION |
| 74 | + (void)__align; |
| 75 | + return __do_deallocate_handle_size(__ptr, __size); |
| 76 | +#else |
| 77 | + if (__is_overaligned_for_new(__align)) { |
| 78 | + const align_val_t __align_val = static_cast<align_val_t>(__align); |
| 79 | + return __do_deallocate_handle_size(__ptr, __size, __align_val); |
| 80 | + } else { |
| 81 | + return __do_deallocate_handle_size(__ptr, __size); |
| 82 | + } |
| 83 | +#endif |
| 84 | +} |
| 85 | + |
| 86 | +inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) _NOEXCEPT { |
| 87 | +#if !_LIBCPP_HAS_ALIGNED_ALLOCATION |
| 88 | + (void)__align; |
| 89 | + return __libcpp_operator_delete(__ptr); |
| 90 | +#else |
| 91 | + if (__is_overaligned_for_new(__align)) { |
| 92 | + const align_val_t __align_val = static_cast<align_val_t>(__align); |
| 93 | + return __libcpp_operator_delete(__ptr, __align_val); |
| 94 | + } else { |
| 95 | + return __libcpp_operator_delete(__ptr); |
| 96 | + } |
| 97 | +#endif |
| 98 | +} |
| 99 | +_LIBCPP_END_NAMESPACE_STD |
| 100 | + |
| 101 | +#endif // _LIBCPP___NEW_ALLOCATE_H |
0 commit comments