new_delete_resource.hpp
1 /*
2  * Copyright (c) 2020-2021, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/mr/host/host_memory_resource.hpp>
19 
20 #include <rmm/detail/aligned.hpp>
21 
22 #include <cstddef>
23 #include <utility>
24 
25 namespace rmm::mr {
26 
32  public:
33  new_delete_resource() = default;
34  ~new_delete_resource() override = default;
35  new_delete_resource(new_delete_resource const&) = default;
37  new_delete_resource& operator=(new_delete_resource const&) = default;
38  new_delete_resource& operator=(new_delete_resource&&) = default;
39 
40  private:
53  void* do_allocate(std::size_t bytes,
54  std::size_t alignment = rmm::detail::RMM_DEFAULT_HOST_ALIGNMENT) override
55  {
56  // If the requested alignment isn't supported, use default
57  alignment = (rmm::detail::is_supported_alignment(alignment))
58  ? alignment
59  : rmm::detail::RMM_DEFAULT_HOST_ALIGNMENT;
60 
61  return rmm::detail::aligned_allocate(
62  bytes, alignment, [](std::size_t size) { return ::operator new(size); });
63  }
64 
80  void do_deallocate(void* ptr,
81  std::size_t bytes,
82  std::size_t alignment = rmm::detail::RMM_DEFAULT_HOST_ALIGNMENT) override
83  {
84  rmm::detail::aligned_deallocate(
85  ptr, bytes, alignment, [](void* ptr) { ::operator delete(ptr); });
86  }
87 };
88 
89 } // namespace rmm::mr
rmm::mr::host_memory_resource
Base class for host memory allocation.
Definition: host_memory_resource.hpp:47
rmm::mr::new_delete_resource
A host_memory_resource that uses the global operator new and operator delete to allocate host memory.
Definition: new_delete_resource.hpp:31