thrust/mr/allocator.h
File members: thrust/mr/allocator.h
/*
* Copyright 2018 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <thrust/detail/config.h>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <thrust/detail/config.h>
#include <thrust/detail/config/memory_resource.h>
#include <thrust/detail/type_traits/pointer_traits.h>
#include <thrust/mr/polymorphic_adaptor.h>
#include <thrust/mr/validator.h>
#include <limits>
THRUST_NAMESPACE_BEGIN
namespace mr
{
template <typename T, class MR>
class allocator : private validator<MR>
{
public:
using void_pointer = typename MR::pointer;
using value_type = T;
using pointer = typename thrust::detail::pointer_traits<void_pointer>::template rebind<T>::other;
using const_pointer = typename thrust::detail::pointer_traits<void_pointer>::template rebind<const T>::other;
using reference = typename thrust::detail::pointer_traits<pointer>::reference;
using const_reference = typename thrust::detail::pointer_traits<const_pointer>::reference;
using size_type = std::size_t;
using difference_type = typename thrust::detail::pointer_traits<pointer>::difference_type;
using propagate_on_container_copy_assignment = detail::true_type;
using propagate_on_container_move_assignment = detail::true_type;
using propagate_on_container_swap = detail::true_type;
template <typename U>
struct rebind
{
using other = allocator<U, MR>;
};
_CCCL_EXEC_CHECK_DISABLE
_CCCL_HOST_DEVICE size_type max_size() const
{
return (std::numeric_limits<size_type>::max)() / sizeof(T);
}
_CCCL_HOST_DEVICE allocator(MR* resource)
: mem_res(resource)
{}
template <typename U>
_CCCL_HOST_DEVICE allocator(const allocator<U, MR>& other)
: mem_res(other.resource())
{}
_CCCL_NODISCARD _CCCL_HOST pointer allocate(size_type n)
{
return static_cast<pointer>(mem_res->do_allocate(n * sizeof(T), alignof(T)));
}
_CCCL_HOST void deallocate(pointer p, size_type n) noexcept
{
return mem_res->do_deallocate(p, n * sizeof(T), alignof(T));
}
_CCCL_HOST_DEVICE MR* resource() const
{
return mem_res;
}
private:
MR* mem_res;
};
template <typename T, typename MR>
_CCCL_HOST_DEVICE bool operator==(const allocator<T, MR>& lhs, const allocator<T, MR>& rhs) noexcept
{
return *lhs.resource() == *rhs.resource();
}
template <typename T, typename MR>
_CCCL_HOST_DEVICE bool operator!=(const allocator<T, MR>& lhs, const allocator<T, MR>& rhs) noexcept
{
return !(lhs == rhs);
}
template <typename T, typename Pointer>
using polymorphic_allocator = allocator<T, polymorphic_adaptor_resource<Pointer>>;
template <typename T, typename Upstream>
class stateless_resource_allocator : public thrust::mr::allocator<T, Upstream>
{
using base = thrust::mr::allocator<T, Upstream>;
public:
template <typename U>
struct rebind
{
using other = stateless_resource_allocator<U, Upstream>;
};
_CCCL_EXEC_CHECK_DISABLE
_CCCL_HOST_DEVICE stateless_resource_allocator()
: base(get_global_resource<Upstream>())
{}
_CCCL_HOST_DEVICE stateless_resource_allocator(const stateless_resource_allocator& other)
: base(other)
{}
template <typename U>
_CCCL_HOST_DEVICE stateless_resource_allocator(const stateless_resource_allocator<U, Upstream>& other)
: base(other)
{}
stateless_resource_allocator& operator=(const stateless_resource_allocator&) = default;
_CCCL_HOST_DEVICE ~stateless_resource_allocator() {}
};
} // namespace mr
THRUST_NAMESPACE_END