[libc++] Replace uses of __libcpp_allocate by std::allocator<>

Both are equivalent, however std::allocator can appear in constant
expressions and is higher level.
This commit is contained in:
Louis Dionne 2020-09-28 15:47:49 -04:00
parent 93ba33066c
commit 59f8ac3eb4
2 changed files with 24 additions and 58 deletions

View File

@ -11,8 +11,9 @@
#define _LIBCPP___SSO_ALLOCATOR #define _LIBCPP___SSO_ALLOCATOR
#include <__config> #include <__config>
#include <type_traits> #include <memory>
#include <new> #include <new>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header #pragma GCC system_header
@ -54,14 +55,14 @@ public:
__allocated_ = true; __allocated_ = true;
return (pointer)&buf_; return (pointer)&buf_;
} }
return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp))); return allocator<_Tp>().allocate(__n);
} }
_LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n)
{ {
if (__p == (pointer)&buf_) if (__p == (pointer)&buf_)
__allocated_ = false; __allocated_ = false;
else 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);} _LIBCPP_INLINE_VISIBILITY size_type max_size() const throw() {return size_type(~0) / sizeof(_Tp);}

View File

@ -2738,9 +2738,7 @@ __val_expr<_ValExpr>::operator valarray<__val_expr::result_type>() const
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ =
__r.__end_ = __r.__end_ = allocator<result_type>().allocate(__n);
static_cast<result_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(result_type), _LIBCPP_ALIGNOF(result_type)));
for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i) for (size_t __i = 0; __i != __n; ++__r.__end_, ++__i)
::new (__r.__end_) result_type(__expr_[__i]); ::new (__r.__end_) result_type(__expr_[__i]);
} }
@ -2757,8 +2755,7 @@ valarray<_Tp>::valarray(size_t __n)
{ {
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2792,8 +2789,7 @@ valarray<_Tp>::valarray(const value_type* __p, size_t __n)
{ {
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2818,8 +2814,7 @@ valarray<_Tp>::valarray(const valarray& __v)
{ {
if (__v.size()) if (__v.size())
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__v.size());
_VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2856,8 +2851,7 @@ valarray<_Tp>::valarray(initializer_list<value_type> __il)
const size_t __n = __il.size(); const size_t __n = __il.size();
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2886,8 +2880,7 @@ valarray<_Tp>::valarray(const slice_array<value_type>& __sa)
const size_t __n = __sa.__size_; const size_t __n = __sa.__size_;
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2914,8 +2907,7 @@ valarray<_Tp>::valarray(const gslice_array<value_type>& __ga)
const size_t __n = __ga.__1d_.size(); const size_t __n = __ga.__1d_.size();
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2944,8 +2936,7 @@ valarray<_Tp>::valarray(const mask_array<value_type>& __ma)
const size_t __n = __ma.__1d_.size(); const size_t __n = __ma.__1d_.size();
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -2974,8 +2965,7 @@ valarray<_Tp>::valarray(const indirect_array<value_type>& __ia)
const size_t __n = __ia.__1d_.size(); const size_t __n = __ia.__1d_.size();
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {
@ -3011,8 +3001,7 @@ valarray<_Tp>::__assign_range(const value_type* __f, const value_type* __l)
if (size() != __n) if (size() != __n)
{ {
__clear(size()); __clear(size());
__begin_ = static_cast<value_type*>( __begin_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
__end_ = __begin_ + __n; __end_ = __begin_ + __n;
_VSTD::uninitialized_copy(__f, __l, __begin_); _VSTD::uninitialized_copy(__f, __l, __begin_);
} else { } else {
@ -3265,10 +3254,7 @@ valarray<_Tp>::operator+() const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(+*__p); ::new (__r.__end_) value_type(+*__p);
} }
@ -3283,10 +3269,7 @@ valarray<_Tp>::operator-() const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(-*__p); ::new (__r.__end_) value_type(-*__p);
} }
@ -3301,10 +3284,7 @@ valarray<_Tp>::operator~() const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(~*__p); ::new (__r.__end_) value_type(~*__p);
} }
@ -3319,9 +3299,7 @@ valarray<_Tp>::operator!() const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<bool>().allocate(__n);
__r.__end_ =
static_cast<bool*>(_VSTD::__libcpp_allocate(__n * sizeof(bool), _LIBCPP_ALIGNOF(bool)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) bool(!*__p); ::new (__r.__end_) bool(!*__p);
} }
@ -3639,10 +3617,7 @@ valarray<_Tp>::shift(int __i) const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
const value_type* __sb; const value_type* __sb;
value_type* __tb; value_type* __tb;
value_type* __te; value_type* __te;
@ -3678,10 +3653,7 @@ valarray<_Tp>::cshift(int __i) const
size_t __n = size(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
__i %= static_cast<int>(__n); __i %= static_cast<int>(__n);
const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i; const value_type* __m = __i >= 0 ? __begin_ + __i : __end_ + __i;
for (const value_type* __s = __m; __s != __end_; ++__r.__end_, ++__s) 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(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(__f(*__p)); ::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(); size_t __n = size();
if (__n) if (__n)
{ {
__r.__begin_ = __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
__r.__end_ =
static_cast<value_type*>(
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n) for (const value_type* __p = __begin_; __n; ++__r.__end_, ++__p, --__n)
::new (__r.__end_) value_type(__f(*__p)); ::new (__r.__end_) value_type(__f(*__p));
} }
@ -3736,7 +3702,7 @@ void valarray<_Tp>::__clear(size_t __capacity)
{ {
while (__end_ != __begin_) while (__end_ != __begin_)
(--__end_)->~value_type(); (--__end_)->~value_type();
_VSTD::__libcpp_deallocate(__begin_, __capacity * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)); allocator<value_type>().deallocate(__begin_, __capacity);
__begin_ = __end_ = nullptr; __begin_ = __end_ = nullptr;
} }
} }
@ -3748,8 +3714,7 @@ valarray<_Tp>::resize(size_t __n, value_type __x)
__clear(size()); __clear(size());
if (__n) if (__n)
{ {
__begin_ = __end_ = static_cast<value_type*>( __begin_ = __end_ = allocator<value_type>().allocate(__n);
_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
#ifndef _LIBCPP_NO_EXCEPTIONS #ifndef _LIBCPP_NO_EXCEPTIONS
try try
{ {