Simplify Allocator
authorRoland Schulz <roland.schulz@intel.com>
Sun, 19 Aug 2018 23:20:34 +0000 (16:20 -0700)
committerMagnus Lundborg <magnus.lundborg@scilifelab.se>
Fri, 7 Sep 2018 06:50:45 +0000 (08:50 +0200)
Remove all unneeded functions. Most of them weren't needed
with C++11 which requires all usage of allocators to go
through the allocator_traits which provide the default
implementation.

Change-Id: I37fb298c575fa5b09e5a6a1fdf4e7ff390eaec7a

src/gromacs/fileio/checkpoint.cpp
src/gromacs/gpu_utils/hostallocator.cpp
src/gromacs/gpu_utils/hostallocator.h
src/gromacs/utility/allocator.h
src/gromacs/utility/tests/alignedallocator-impl.h

index a0482fbd28b33bf61bbde19d964c00f38546e0ac..fa69f490e8696333ed34b45848daf611689e27fe 100644 (file)
@@ -696,7 +696,7 @@ static int doRvecVector(XDR *xd, StatePart part, int ecpt, int sflags,
     {
         // Use the rebind facility to change the value_type of the
         // allocator from RVec to real.
-        using realAllocator = typename AllocatorType::template rebind<real>::other;
+        using realAllocator = typename std::allocator_traits<AllocatorType>::template rebind_alloc<real>;
         return doVectorLow<real, realAllocator>(xd, part, ecpt, sflags, numReals, nullptr, nullptr, nullptr, list, CptElementType::real);
     }
 }
index 32e342a54f2395b8a821b06750e42d89a79be2da..378dbab1633b914379b2c89e832f4c555139097c 100644 (file)
@@ -60,7 +60,7 @@ HostAllocationPolicy::HostAllocationPolicy(PinningPolicy pinningPolicy)
 {
 }
 
-std::size_t HostAllocationPolicy::alignment()
+std::size_t HostAllocationPolicy::alignment() const noexcept
 {
     return (pinningPolicy_ == PinningPolicy::PinnedIfSupported ?
             PageAlignedAllocationPolicy::alignment() :
index 95ddce8d796af7f2f9572a69e7eae12072a7650b..ebe6f2e6e8ea1ef0f8ebd21761c6a85cb0135fd3 100644 (file)
@@ -137,7 +137,7 @@ class HostAllocationPolicy
         //! Constructor
         HostAllocationPolicy(PinningPolicy policy = PinningPolicy::CannotBePinned);
         /*! \brief Return the alignment size currently used by the active pinning policy. */
-        std::size_t alignment();
+        std::size_t alignment() const noexcept;
         /*! \brief Allocate and perhaps pin page-aligned memory suitable for
          * e.g. GPU transfers.
          *
index 65f1339afaaa9d1b033aa86da79a94b2d5b98611..f888583d99305917e098c29f21470f3759352949 100644 (file)
@@ -99,32 +99,8 @@ class Allocator : public AllocationPolicy
 {
     public:
         // The standard library specification for a custom allocator
-        // requires these typedefs, with this capitalization/underscoring.
+        // requires this typedef, with this capitalization/underscoring.
         typedef T              value_type;      //!< Type of allocated elements
-        typedef T             &reference;       //!< Reference to allocated elements
-        typedef const T       &const_reference; //!< Constant reference to allocated elements
-        typedef T *            pointer;         //!< Pointer to allocated elements
-        typedef const T *      const_pointer;   //!< Constant pointer to allocated elements
-        typedef std::size_t    size_type;       //!< Integer type to use for size of objects
-        typedef std::ptrdiff_t difference_type; //!< Type to hold differences between pointers
-
-        // This typedef is required by GROMACS for testing and assertions
-        typedef AllocationPolicy allocation_policy; //!< Type of the AllocationPolicy
-
-        /*! \libinternal \brief Standard-required typedef to use allocator with different class.
-         *
-         *  \tparam U new class
-         *
-         *  This is used for things like std::list where the size of each link
-         *  is larger than the class stored in the link.
-         *
-         *  Required by the specification for an allocator.
-         */
-        template <class U>
-        struct rebind
-        {
-            typedef Allocator<U, AllocationPolicy> other; //!< Align class U with our alignment
-        };
 
         /*! \brief Constructor
          *
@@ -139,22 +115,6 @@ class Allocator : public AllocationPolicy
          */
         Allocator(const AllocationPolicy &p) : AllocationPolicy(p) {}
 
-        /*! \brief Return address of an object
-         *
-         *  \param r Reference to object of type T
-         *  \return Pointer to T memory
-         */
-        pointer
-        address(reference r) const { return &r; }
-
-        /*! \brief Return address of a const object
-         *
-         *  \param r Const reference to object of type T
-         *  \return Pointer to T memory
-         */
-        const_pointer
-        address(const_reference r) const { return &r; }
-
         /*! \brief Do the actual memory allocation
          *
          *  \param n    Number of elements of type T to allocate. n can be
@@ -166,7 +126,7 @@ class Allocator : public AllocationPolicy
          *
          *  \throws std::bad_alloc if the allocation fails.
          */
-        pointer
+        value_type*
         allocate(std::size_t n, typename std::allocator<void>::const_pointer gmx_unused hint = nullptr)
         {
             void *p = AllocationPolicy::malloc(n*sizeof(T));
@@ -177,7 +137,7 @@ class Allocator : public AllocationPolicy
             }
             else
             {
-                return static_cast<pointer>(p);
+                return static_cast<value_type*>(p);
             }
         }
 
@@ -187,49 +147,19 @@ class Allocator : public AllocationPolicy
          * \param n  number of objects previously passed to allocate()
          */
         void
-        deallocate(pointer p, std::size_t gmx_unused n)
+        deallocate(value_type* p, std::size_t gmx_unused n)
         {
             AllocationPolicy::free(p);
         }
 
-        //! Return the policy object for this allocator.
-        AllocationPolicy getPolicy() const
-        {
-            return *this;
-        }
-
-        /*! \brief Construct an object without allocating memory
-         *
-         * \tparam Args  Variable-length list of types for constructor args
-         * \param p      Adress of memory where to construct object
-         * \param args   Variable-length list of arguments to constructor
-         */
-        template<class ... Args>
-        void
-        construct(pointer p, Args && ... args) { ::new(p)T(std::forward<Args>(args) ...); }
-
-        /*! \brief Call the destructor of object without releasing memory
-         *
-         * \param p  Address of memory where to destroy object
-         */
-        void
-        destroy(pointer p) { p->~value_type(); }
-
-        /*! \brief Return largest number of objects that can be allocated
-         *
-         * This will be set such that the number of objects T multiplied by
-         * the size of each object is the largest value that can be represented
-         * by size_type.
-         */
-        std::size_t
-        max_size() const { return SIZE_MAX / sizeof(T); }
-
         /*! \brief Return true if two allocators are identical
          *
          * This is a member function of the left-hand-side allocator.
          * Always true for stateless polcies. Has to be defined in the policy for stateful policies.
+         * FUTURE: Can be removed with C++17 (is_always_equal)
          */
-        template<class T2, class A = AllocationPolicy, typename = typename std::enable_if<std::is_empty<A>::value>::type>
+        template<class T2, class A = AllocationPolicy,
+                 typename          = typename std::enable_if<std::is_empty<A>::value>::type>
         bool operator==(const Allocator<T2, AllocationPolicy> & /*unused*/) const { return true; }
 
         /*! \brief Return true if two allocators are different
@@ -238,8 +168,8 @@ class Allocator : public AllocationPolicy
          *
          * This is a member function of the left-hand-side allocator.
          */
-        bool
-        operator!=(const Allocator &rhs) const { return !(*this == rhs); }
+        template<class T2>
+        bool operator!=(const Allocator<T2, AllocationPolicy> &rhs) const { return !(*this == rhs); }
 };
 
 }      // namespace gmx
index 8a49e0172de2c8f4c53db9e3b6d91a5396585fcd..045da902c4b767674fc8412aeed0b31702fe3ddc 100644 (file)
@@ -74,7 +74,7 @@ class AllocatorTest : public ::testing::Test
          * intended alignment. */
         std::size_t mask(const T &allocator)
         {
-            return allocator.getPolicy().alignment() - 1;
+            return allocator.alignment() - 1;
         }
 };
 
@@ -82,7 +82,7 @@ class AllocatorTest : public ::testing::Test
 
 TYPED_TEST(AllocatorTest, AllocatorAlignAllocatesWithAlignment) //NOLINT(misc-definitions-in-headers)
 {
-    using pointer = typename TypeParam::pointer;
+    using pointer = typename TypeParam::value_type*;
     TypeParam a;
     pointer   p = a.allocate(1000);