thrust/mr/memory_resource.h

File members: thrust/mr/memory_resource.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/memory_resource.h>
#ifdef THRUST_MR_STD_MR_HEADER
#  include THRUST_MR_STD_MR_HEADER
#endif

THRUST_NAMESPACE_BEGIN
namespace mr
{

template <typename Pointer = void*>
class memory_resource
{
public:
  using pointer = Pointer;

  virtual ~memory_resource() = default;

  _CCCL_NODISCARD pointer allocate(std::size_t bytes, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT)
  {
    return do_allocate(bytes, alignment);
  }

  void deallocate(pointer p, std::size_t bytes, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT) noexcept
  {
    do_deallocate(p, bytes, alignment);
  }

  _CCCL_HOST_DEVICE bool is_equal(const memory_resource& other) const noexcept
  {
    return do_is_equal(other);
  }

  virtual pointer do_allocate(std::size_t bytes, std::size_t alignment) = 0;

  virtual void do_deallocate(pointer p, std::size_t bytes, std::size_t alignment) = 0;

  _CCCL_HOST_DEVICE virtual bool do_is_equal(const memory_resource& other) const noexcept
  {
    return this == &other;
  }
};

template <>
class memory_resource<void*>
#ifdef THRUST_STD_MR_NS
    : THRUST_STD_MR_NS::memory_resource
#endif
{
public:
  using pointer = void*;

  virtual ~memory_resource() = default;

  _CCCL_NODISCARD pointer allocate(std::size_t bytes, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT)
  {
    return do_allocate(bytes, alignment);
  }

  void deallocate(pointer p, std::size_t bytes, std::size_t alignment = THRUST_MR_DEFAULT_ALIGNMENT) noexcept
  {
    do_deallocate(p, bytes, alignment);
  }

  _CCCL_HOST_DEVICE bool is_equal(const memory_resource& other) const noexcept
  {
    return do_is_equal(other);
  }

  virtual pointer do_allocate(std::size_t bytes, std::size_t alignment)           = 0;
  virtual void do_deallocate(pointer p, std::size_t bytes, std::size_t alignment) = 0;
  _CCCL_HOST_DEVICE virtual bool do_is_equal(const memory_resource& other) const noexcept
  {
    return this == &other;
  }

#ifdef THRUST_STD_MR_NS
  // the above do_is_equal is a different function than the one from the standard memory resource
  // can't implement this reasonably without RTTI though; it's reasonable to assume false otherwise

  virtual bool do_is_equal(const THRUST_STD_MR_NS::memory_resource& other) const noexcept override
  {
#  ifndef _CCCL_NO_RTTI
    auto mr_resource = dynamic_cast<memory_resource<>*>(&other);
    return mr_resource && do_is_equal(*mr_resource);
#  else
    return this == &other;
#  endif
  }
#endif
};

template <typename Pointer>
_CCCL_HOST_DEVICE bool operator==(const memory_resource<Pointer>& lhs, const memory_resource<Pointer>& rhs) noexcept
{
  return &lhs == &rhs || rhs.is_equal(rhs);
}

template <typename Pointer>
_CCCL_HOST_DEVICE bool operator!=(const memory_resource<Pointer>& lhs, const memory_resource<Pointer>& rhs) noexcept
{
  return !(lhs == rhs);
}

template <typename MR>
_CCCL_HOST MR* get_global_resource()
{
  static MR resource;
  return &resource;
}

} // namespace mr
THRUST_NAMESPACE_END