|
14 | 14 | #include <__cstddef/size_t.h>
|
15 | 15 | #include <__new/align_val_t.h>
|
16 | 16 | #include <__new/global_new_delete.h> // for _LIBCPP_HAS_SIZED_DEALLOCATION
|
| 17 | +#include <__type_traits/type_identity.h> |
| 18 | +#include <__utility/element_count.h> |
17 | 19 |
|
18 | 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
19 | 21 | # pragma GCC system_header
|
@@ -47,52 +49,58 @@ _LIBCPP_HIDE_FROM_ABI void __libcpp_operator_delete(_Args... __args) _NOEXCEPT {
|
47 | 49 | #endif
|
48 | 50 | }
|
49 | 51 |
|
50 |
| -inline _LIBCPP_HIDE_FROM_ABI void* __libcpp_allocate(size_t __size, size_t __align) { |
| 52 | +template <class _Tp> |
| 53 | +inline _LIBCPP_HIDE_FROM_ABI _Tp* __libcpp_allocate(__element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) { |
| 54 | + size_t __size = static_cast<size_t>(__n) * sizeof(_Tp); |
51 | 55 | #if _LIBCPP_HAS_ALIGNED_ALLOCATION
|
52 | 56 | if (__is_overaligned_for_new(__align)) {
|
53 | 57 | const align_val_t __align_val = static_cast<align_val_t>(__align);
|
54 |
| - return __libcpp_operator_new(__size, __align_val); |
| 58 | + return static_cast<_Tp*>(std::__libcpp_operator_new(__size, __align_val)); |
55 | 59 | }
|
56 | 60 | #endif
|
57 | 61 |
|
58 | 62 | (void)__align;
|
59 |
| - return __libcpp_operator_new(__size); |
| 63 | + return static_cast<_Tp*>(std::__libcpp_operator_new(__size)); |
60 | 64 | }
|
61 | 65 |
|
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...); |
| 66 | +#if _LIBCPP_HAS_SIZED_DEALLOCATION |
| 67 | +# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) __VA_ARGS__ |
67 | 68 | #else
|
68 |
| - return std::__libcpp_operator_delete(__ptr, __size, __args...); |
| 69 | +# define _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(...) /* nothing */ |
69 | 70 | #endif
|
70 |
| -} |
71 | 71 |
|
72 |
| -inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate(void* __ptr, size_t __size, size_t __align) _NOEXCEPT { |
| 72 | +template <class _Tp> |
| 73 | +inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate( |
| 74 | + __type_identity_t<_Tp>* __ptr, __element_count __n, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT { |
| 75 | + size_t __size = static_cast<size_t>(__n) * sizeof(_Tp); |
| 76 | + (void)__size; |
73 | 77 | #if !_LIBCPP_HAS_ALIGNED_ALLOCATION
|
74 | 78 | (void)__align;
|
75 |
| - return __do_deallocate_handle_size(__ptr, __size); |
| 79 | + return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size)); |
76 | 80 | #else
|
77 | 81 | if (__is_overaligned_for_new(__align)) {
|
78 | 82 | const align_val_t __align_val = static_cast<align_val_t>(__align);
|
79 |
| - return __do_deallocate_handle_size(__ptr, __size, __align_val); |
| 83 | + return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size), __align_val); |
80 | 84 | } else {
|
81 |
| - return __do_deallocate_handle_size(__ptr, __size); |
| 85 | + return std::__libcpp_operator_delete(__ptr _LIBCPP_ONLY_IF_SIZED_DEALLOCATION(, __size)); |
82 | 86 | }
|
83 | 87 | #endif
|
84 | 88 | }
|
85 | 89 |
|
86 |
| -inline _LIBCPP_HIDE_FROM_ABI void __libcpp_deallocate_unsized(void* __ptr, size_t __align) _NOEXCEPT { |
| 90 | +#undef _LIBCPP_ONLY_IF_SIZED_DEALLOCATION |
| 91 | + |
| 92 | +template <class _Tp> |
| 93 | +inline _LIBCPP_HIDE_FROM_ABI void |
| 94 | +__libcpp_deallocate_unsized(__type_identity_t<_Tp>* __ptr, size_t __align = _LIBCPP_ALIGNOF(_Tp)) _NOEXCEPT { |
87 | 95 | #if !_LIBCPP_HAS_ALIGNED_ALLOCATION
|
88 | 96 | (void)__align;
|
89 |
| - return __libcpp_operator_delete(__ptr); |
| 97 | + return std::__libcpp_operator_delete(__ptr); |
90 | 98 | #else
|
91 | 99 | if (__is_overaligned_for_new(__align)) {
|
92 | 100 | const align_val_t __align_val = static_cast<align_val_t>(__align);
|
93 |
| - return __libcpp_operator_delete(__ptr, __align_val); |
| 101 | + return std::__libcpp_operator_delete(__ptr, __align_val); |
94 | 102 | } else {
|
95 |
| - return __libcpp_operator_delete(__ptr); |
| 103 | + return std::__libcpp_operator_delete(__ptr); |
96 | 104 | }
|
97 | 105 | #endif
|
98 | 106 | }
|
|
0 commit comments