Project
Loading...
Searching...
No Matches
UniquePointer.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
15
16#ifndef ITSTRACKINGGPU_UNIQUEPOINTER_H_
17#define ITSTRACKINGGPU_UNIQUEPOINTER_H_
18
19#include "Utils.h"
20
21namespace o2
22{
23namespace its
24{
25namespace gpu
26{
27
28namespace
29{
30template <typename T>
31struct UniquePointerTraits final {
32 typedef T* InternalPointer;
33
34 GPUhd() static constexpr T& getReference(const InternalPointer& internalPointer) noexcept
35 {
36 return const_cast<T&>(*internalPointer);
37 }
38
39 GPUhd() static constexpr T* getPointer(const InternalPointer& internalPointer) noexcept
40 {
41 return const_cast<T*>(internalPointer);
42 }
43};
44} // namespace
45
46template <typename T>
47class UniquePointer final
48{
49 typedef UniquePointerTraits<T> PointerTraits;
50
51 public:
53 explicit UniquePointer(const T&);
55
56 UniquePointer(const UniquePointer&) = delete;
58
61
62 GPUhd() T* get() noexcept;
63 GPUhd() const T* get() const noexcept;
64 GPUhd() T& operator*() noexcept;
65 GPUhd() const T& operator*() const noexcept;
66
68 void destroy();
69
70 private:
71 typename PointerTraits::InternalPointer mDevicePointer;
72};
73
74template <typename T>
75UniquePointer<T>::UniquePointer() : mDevicePointer{nullptr}
76{
77 // Nothing to do
78}
79
80template <typename T>
82{
83 try {
84
85 utils::host::gpuMalloc(reinterpret_cast<void**>(&mDevicePointer), sizeof(T));
86 utils::host::gpuMemcpyHostToDevice(mDevicePointer, &ref, sizeof(T));
87
88 } catch (...) {
89
90 destroy();
91
92 throw;
93 }
94}
95
96template <typename T>
101
102template <typename T>
104{
105 // Nothing to do
106}
107
108template <typename T>
110{
111 mDevicePointer = other.mDevicePointer;
112 other.mDevicePointer = nullptr;
113
114 return *this;
115}
116
117template <typename T>
119{
120 if (mDevicePointer != nullptr) {
121
122 utils::host::gpuFree(mDevicePointer);
123 }
124}
125
126template <typename T>
127GPUhd() T* UniquePointer<T>::get() noexcept
128{
129 return PointerTraits::getPointer(mDevicePointer);
130}
131
132template <typename T>
133GPUhd() const T* UniquePointer<T>::get() const noexcept
134{
135 return PointerTraits::getPointer(mDevicePointer);
136}
137
138template <typename T>
139GPUhd() T& UniquePointer<T>::operator*() noexcept
140{
141 return PointerTraits::getReference(mDevicePointer);
142}
143
144template <typename T>
145GPUhd() const T& UniquePointer<T>::operator*() const noexcept
146{
147 return PointerTraits::getReference(mDevicePointer);
148}
149} // namespace gpu
150} // namespace its
151} // namespace o2
152
153#endif
#define protected
UniquePointer(UniquePointer &&)
GPUhd() T *get() noexcept
UniquePointer & operator=(UniquePointer &&)
UniquePointer(const UniquePointer &)=delete
UniquePointer & operator=(const UniquePointer &)=delete
auto get(const std::byte *buffer, size_t=0)
Definition DataHeader.h:454
GPUhd() T *getPtrFromRuler(int index
Definition Vector.h:126
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
VectorOfTObjectPtrs other