From: Roland Schulz Date: Sun, 19 Aug 2018 23:20:34 +0000 (-0700) Subject: Simplify Allocator X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=7e4f7399dadefb8e4f4073887284292ffa4fa944;p=alexxy%2Fgromacs.git Simplify Allocator 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 --- diff --git a/src/gromacs/fileio/checkpoint.cpp b/src/gromacs/fileio/checkpoint.cpp index a0482fbd28..fa69f490e8 100644 --- a/src/gromacs/fileio/checkpoint.cpp +++ b/src/gromacs/fileio/checkpoint.cpp @@ -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::other; + using realAllocator = typename std::allocator_traits::template rebind_alloc; return doVectorLow(xd, part, ecpt, sflags, numReals, nullptr, nullptr, nullptr, list, CptElementType::real); } } diff --git a/src/gromacs/gpu_utils/hostallocator.cpp b/src/gromacs/gpu_utils/hostallocator.cpp index 32e342a54f..378dbab163 100644 --- a/src/gromacs/gpu_utils/hostallocator.cpp +++ b/src/gromacs/gpu_utils/hostallocator.cpp @@ -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() : diff --git a/src/gromacs/gpu_utils/hostallocator.h b/src/gromacs/gpu_utils/hostallocator.h index 95ddce8d79..ebe6f2e6e8 100644 --- a/src/gromacs/gpu_utils/hostallocator.h +++ b/src/gromacs/gpu_utils/hostallocator.h @@ -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. * diff --git a/src/gromacs/utility/allocator.h b/src/gromacs/utility/allocator.h index 65f1339afa..f888583d99 100644 --- a/src/gromacs/utility/allocator.h +++ b/src/gromacs/utility/allocator.h @@ -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 - struct rebind - { - typedef Allocator 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::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(p); + return static_cast(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 - void - construct(pointer p, Args && ... args) { ::new(p)T(std::forward(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::value>::type> + template::value>::type> bool operator==(const Allocator & /*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 + bool operator!=(const Allocator &rhs) const { return !(*this == rhs); } }; } // namespace gmx diff --git a/src/gromacs/utility/tests/alignedallocator-impl.h b/src/gromacs/utility/tests/alignedallocator-impl.h index 8a49e0172d..045da902c4 100644 --- a/src/gromacs/utility/tests/alignedallocator-impl.h +++ b/src/gromacs/utility/tests/alignedallocator-impl.h @@ -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);