From 59f8ac3eb441b9bf1fb589facc024a03c218bece Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Mon, 28 Sep 2020 15:47:49 -0400 Subject: [PATCH] [libc++] Replace uses of __libcpp_allocate by std::allocator<> Both are equivalent, however std::allocator can appear in constant expressions and is higher level. --- libcxx/include/__sso_allocator | 7 ++-- libcxx/include/valarray | 75 +++++++++------------------------- 2 files changed, 24 insertions(+), 58 deletions(-) diff --git a/libcxx/include/__sso_allocator b/libcxx/include/__sso_allocator index 393012873848..117b728f1499 100644 --- a/libcxx/include/__sso_allocator +++ b/libcxx/include/__sso_allocator @@ -11,8 +11,9 @@ #define _LIBCPP___SSO_ALLOCATOR #include <__config> -#include +#include #include +#include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header @@ -54,14 +55,14 @@ public: __allocated_ = true; return (pointer)&buf_; } - return static_cast(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); + return allocator<_Tp>().allocate(__n); } _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) { if (__p == (pointer)&buf_) __allocated_ = false; else - _VSTD::__libcpp_deallocate(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)); + allocator<_Tp>().deallocate(__p, __n); } _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);} diff --git a/libcxx/include/valarray b/libcxx/include/valarray index c048a6d7e498..4bbd5ed323af 100644 --- a/libcxx/include/valarray +++ b/libcxx/include/valarray @@ -2738,9 +2738,7 @@ __val_expr<_ValExpr>::operator valarray<__val_expr::result_type>() const if (__n) { __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(result_type), _LIBCPP_ALIGNOF(result_type))); + __r.__end_ = allocator().allocate(__n); for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i) ::new (__r.__end_) result_type(__expr_[__i]); } @@ -2757,8 +2755,7 @@ valarray<_Tp>::valarray(size_t __n) { if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2792,8 +2789,7 @@ valarray<_Tp>::valarray(const value_type* __p, size_t __n) { if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2818,8 +2814,7 @@ valarray<_Tp>::valarray(const valarray& __v) { if (__v.size()) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__v.size()); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2856,8 +2851,7 @@ valarray<_Tp>::valarray(initializer_list __il) const size_t __n = __il.size(); if (__n) { - __begin_ = __end_ = static_cast( -_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2886,8 +2880,7 @@ valarray<_Tp>::valarray(const slice_array& __sa) const size_t __n = __sa.__size_; if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2914,8 +2907,7 @@ valarray<_Tp>::valarray(const gslice_array& __ga) const size_t __n = __ga.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2944,8 +2936,7 @@ valarray<_Tp>::valarray(const mask_array& __ma) const size_t __n = __ma.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -2974,8 +2965,7 @@ valarray<_Tp>::valarray(const indirect_array& __ia) const size_t __n = __ia.__1d_.size(); if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try { @@ -3011,8 +3001,7 @@ valarray<_Tp>::__assign_range(const value_type* __f, const value_type* __l) if (size() != __n) { __clear(size()); - __begin_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = allocator().allocate(__n); __end_ = __begin_ + __n; _VSTD::uninitialized_copy(__f, __l, __begin_); } else { @@ -3265,10 +3254,7 @@ valarray<_Tp>::operator+() const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(+*__p); } @@ -3283,10 +3269,7 @@ valarray<_Tp>::operator-() const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(-*__p); } @@ -3301,10 +3284,7 @@ valarray<_Tp>::operator~() const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(~*__p); } @@ -3319,9 +3299,7 @@ valarray<_Tp>::operator!() const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast(_VSTD::__libcpp_allocate(__n * sizeof(bool), _LIBCPP_ALIGNOF(bool))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) bool(!*__p); } @@ -3639,10 +3617,7 @@ valarray<_Tp>::shift(int __i) const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); const value_type* __sb; value_type* __tb; value_type* __te; @@ -3678,10 +3653,7 @@ valarray<_Tp>::cshift(int __i) const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); __i %= static_cast(__n); const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i; for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s) @@ -3700,10 +3672,7 @@ valarray<_Tp>::apply(value_type __f(value_type)) const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(__f(*__p)); } @@ -3718,10 +3687,7 @@ valarray<_Tp>::apply(value_type __f(const value_type&)) const size_t __n = size(); if (__n) { - __r.__begin_ = - __r.__end_ = - static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __r.__begin_ = __r.__end_ = allocator().allocate(__n); for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) ::new (__r.__end_) value_type(__f(*__p)); } @@ -3736,7 +3702,7 @@ void valarray<_Tp>::__clear(size_t __capacity) { while (__end_ != __begin_) (--__end_)->~value_type(); - _VSTD::__libcpp_deallocate(__begin_, __capacity * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)); + allocator().deallocate(__begin_, __capacity); __begin_ = __end_ = nullptr; } } @@ -3748,8 +3714,7 @@ valarray<_Tp>::resize(size_t __n, value_type __x) __clear(size()); if (__n) { - __begin_ = __end_ = static_cast( - _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type))); + __begin_ = __end_ = allocator().allocate(__n); #ifndef _LIBCPP_NO_EXCEPTIONS try {