[libcxx-commits] [libcxx] 59f8ac3 - [libc++] Replace uses of __libcpp_allocate by std::allocator<>

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 28 13:10:35 PDT 2020


Author: Louis Dionne
Date: 2020-09-28T16:09:42-04:00
New Revision: 59f8ac3eb441b9bf1fb589facc024a03c218bece

URL: https://github.com/llvm/llvm-project/commit/59f8ac3eb441b9bf1fb589facc024a03c218bece
DIFF: https://github.com/llvm/llvm-project/commit/59f8ac3eb441b9bf1fb589facc024a03c218bece.diff

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

Both are equivalent, however std::allocator can appear in constant
expressions and is higher level.

Added: 
    

Modified: 
    libcxx/include/__sso_allocator
    libcxx/include/valarray

Removed: 
    


################################################################################
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 <type_traits>
+#include <memory>
 #include <new>
+#include <type_traits>
 
 #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<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)
     {
         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<result_type*>(
-                    _VSTD::__libcpp_allocate(__n * sizeof(result_type), _LIBCPP_ALIGNOF(result_type)));
+            __r.__end_ = allocator<result_type>().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<value_type*>(
-            _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().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<value_type*>(
-            _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2818,8 +2814,7 @@ valarray<_Tp>::valarray(const valarray& __v)
 {
     if (__v.size())
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-            _VSTD::__libcpp_allocate(__v.size() * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__v.size());
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2856,8 +2851,7 @@ valarray<_Tp>::valarray(initializer_list<value_type> __il)
     const size_t __n = __il.size();
     if (__n)
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-_VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2886,8 +2880,7 @@ valarray<_Tp>::valarray(const slice_array<value_type>& __sa)
     const size_t __n = __sa.__size_;
     if (__n)
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-          _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2914,8 +2907,7 @@ valarray<_Tp>::valarray(const gslice_array<value_type>& __ga)
     const size_t __n = __ga.__1d_.size();
     if (__n)
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2944,8 +2936,7 @@ valarray<_Tp>::valarray(const mask_array<value_type>& __ma)
     const size_t __n = __ma.__1d_.size();
     if (__n)
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {
@@ -2974,8 +2965,7 @@ valarray<_Tp>::valarray(const indirect_array<value_type>& __ia)
     const size_t __n = __ia.__1d_.size();
     if (__n)
     {
-        __begin_ = __end_ = static_cast<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<bool*>(_VSTD::__libcpp_allocate(__n * sizeof(bool), _LIBCPP_ALIGNOF(bool)));
+        __r.__begin_ = __r.__end_ = allocator<bool>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().allocate(__n);
         __i %= static_cast<int>(__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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __r.__begin_ = __r.__end_ = allocator<value_type>().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<value_type>().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<value_type*>(
-           _VSTD::__libcpp_allocate(__n * sizeof(value_type), _LIBCPP_ALIGNOF(value_type)));
+        __begin_ = __end_ = allocator<value_type>().allocate(__n);
 #ifndef _LIBCPP_NO_EXCEPTIONS
         try
         {


        


More information about the libcxx-commits mailing list