10 #ifndef vtk_m_cont_internal_DeviceAdapterMemoryManager_h
11 #define vtk_m_cont_internal_DeviceAdapterMemoryManager_h
13 #include <vtkm/cont/vtkm_cont_export.h>
34 struct TransferredBuffer;
39 struct BufferInfoInternals;
43 class VTKM_CONT_EXPORT BufferInfo
65 VTKM_CONT BufferInfo(
const BufferInfo& src);
68 VTKM_CONT BufferInfo& operator=(
const BufferInfo& src);
69 VTKM_CONT BufferInfo& operator=(BufferInfo&& src);
80 using Deleter = void(
void* container);
84 using Reallocater = void(
void*& memory,
98 Reallocater reallocater);
110 VTKM_CONT TransferredBuffer TransferOwnership();
113 detail::BufferInfoInternals* Internals;
127 struct TransferredBuffer
131 BufferInfo::Deleter* Delete;
132 BufferInfo::Reallocater* Reallocate;
138 VTKM_CONT_EXPORT
VTKM_CONT vtkm::cont::internal::BufferInfo AllocateOnHost(
146 class VTKM_CONT_EXPORT DeviceAdapterMemoryManagerBase
149 VTKM_CONT virtual ~DeviceAdapterMemoryManagerBase();
157 VTKM_CONT void Reallocate(vtkm::cont::internal::BufferInfo& buffer,
161 VTKM_CONT BufferInfo ManageArray(
void* memory,
164 vtkm::cont::internal::BufferInfo::Deleter deleter,
165 vtkm::cont::internal::BufferInfo::Reallocater reallocater)
const;
172 VTKM_CONT virtual vtkm::cont::internal::BufferInfo CopyHostToDevice(
173 const vtkm::cont::internal::BufferInfo& src)
const = 0;
177 VTKM_CONT virtual void CopyHostToDevice(
const vtkm::cont::internal::BufferInfo& src,
178 const vtkm::cont::internal::BufferInfo& dest)
const = 0;
182 VTKM_CONT virtual vtkm::cont::internal::BufferInfo CopyDeviceToHost(
183 const vtkm::cont::internal::BufferInfo& src)
const = 0;
187 VTKM_CONT virtual void CopyDeviceToHost(
const vtkm::cont::internal::BufferInfo& src,
188 const vtkm::cont::internal::BufferInfo& dest)
const = 0;
192 VTKM_CONT virtual vtkm::cont::internal::BufferInfo CopyDeviceToDevice(
193 const vtkm::cont::internal::BufferInfo& src)
const = 0;
197 VTKM_CONT virtual void CopyDeviceToDevice(
const vtkm::cont::internal::BufferInfo& src,
198 const vtkm::cont::internal::BufferInfo& dest)
const = 0;
207 template <
typename DeviceAdapterTag>
208 class DeviceAdapterMemoryManager;
210 VTKM_CONT_EXPORT
VTKM_CONT void InvalidRealloc(
void*&,
217 template <
typename T>
218 VTKM_CONT inline void SimpleArrayDeleter(
void* container_)
220 T* container =
reinterpret_cast<T*
>(container_);
226 template <
typename T>
227 VTKM_CONT inline void SimpleArrayReallocater(
void*& memory,
233 VTKM_ASSERT(
static_cast<std::size_t
>(newSize) %
sizeof(T) == 0);
237 if ((newSize > ((3 * oldSize) / 4)) && (newSize <= oldSize))
242 void* newBuffer =
new T[
static_cast<std::size_t
>(newSize) /
sizeof(T)];
243 std::memcpy(newBuffer, memory,
static_cast<std::size_t
>(newSize < oldSize ? newSize : oldSize));
245 if (memory !=
nullptr)
247 SimpleArrayDeleter<T>(memory);
250 memory = container = newBuffer;
255 template <
typename T>
256 VTKM_CONT inline void CastDeleter(
void* container_)
258 T* container =
reinterpret_cast<T*
>(container_);
262 template <
typename T,
typename Allocator>
263 VTKM_CONT inline void StdVectorDeleter(
void* container)
265 CastDeleter<std::vector<T, Allocator>>(container);
268 template <
typename T,
typename Allocator>
269 VTKM_CONT inline void StdVectorReallocater(
void*& memory,
276 VTKM_ASSERT(vector->empty() || (memory == vector->data()));
279 vector->resize(
static_cast<std::size_t
>(newSize));
280 memory = vector->data();
286 #endif //vtk_m_cont_internal_DeviceAdapterMemoryManager_h