ie_locked_memory.hpp
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for generic LockedMemory<> and different variations of locks
7  * @file ie_locked_memory.hpp
8  */
9 #pragma once
10 
11 #include <iostream>
12 #include <utility>
13 
14 #include "ie_allocator.hpp"
15 
16 namespace InferenceEngine {
17 namespace details {
18 /**
19  * @brief This class is a LockedMemory concept for hardware memory
20  */
21 template <class T>
22 class LockedMemoryBase {
23  IAllocator* _allocator = nullptr;
24  void* _handle = nullptr;
25  mutable T* _locked = nullptr;
26  LockOp _lockFlag = LOCK_FOR_WRITE;
27 
28 protected:
29  /**
30  * @brief An offset size.
31  * The default value is 0.
32  */
33  size_t _offset = 0;
34 
35 public:
36  /**
37  * @brief A constructor
38  * @param ptr Pointer to an IAllocator object
39  * @param handle Handle provided by allocator->Alloc()
40  * @param lockFlag Read/Write type of mapping
41  * @param offsetInBytes Offset in originally locked region
42  */
43  LockedMemoryBase(IAllocator* ptr, void* handle, LockOp lockFlag, size_t offsetInBytes)
44  : _allocator(ptr), _handle(handle), _lockFlag(lockFlag), _offset(offsetInBytes) {}
45 
46  /**
47  * @brief A copy constructor
48  * @param that An rvalue reference for the other LockedMemoryBase instance
49  */
50  LockedMemoryBase(LockedMemoryBase&& that)
51  : _allocator(that._allocator), _handle(that._handle), _lockFlag(that._lockFlag), _offset(that._offset) {
52  that._locked = nullptr;
53  }
54 
55  /**
56  * @brief A virtual destructor
57  */
58  virtual ~LockedMemoryBase() {
59  if (_locked != nullptr) {
60  _allocator->unlock(_handle);
61  }
62  }
63 
64 protected:
65  /**
66  * @brief Compares referenced values
67  * @param pointer Pointer to the object to compare with
68  * @return True if all handlers are nullptr or referenced values are equal, false otherwise
69  */
70  bool isEqualTo(const T* pointer) const {
71  if (pointer == nullptr && (_allocator == nullptr || _handle == nullptr)) {
72  return true;
73  }
74  return dereference() == pointer;
75  }
76 
77  /**
78  * @brief Gets the locked object.
79  * Locks the handler and casts memory to the object of the given template type.
80  * @return The pointer to the locked object, nullptr otherwise
81  */
82  virtual T* dereference() const {
83  if (_locked != nullptr) return _locked;
84 
85  if (_allocator == nullptr) {
86  return nullptr;
87  }
88 
89  if (_handle == nullptr) {
90  return nullptr;
91  }
92 
93  uint8_t* pBytes = reinterpret_cast<uint8_t*>(_allocator->lock(_handle, _lockFlag));
94 
95  return _locked = reinterpret_cast<T*>(pBytes + _offset);
96  }
97 };
98 } // namespace details
99 
100 /**
101  * @brief This class represents locked memory for read/write memory
102  */
103 template <class T>
104 class LockedMemory : public details::LockedMemoryBase<T> {
105  using base = details::LockedMemoryBase<T>;
106 
107 public:
108  /**
109  * @brief A constructor
110  * @param ptr Pointer to IAllocator object
111  * @param handle Handle provided by allocator
112  * @param offsetInBytes Offset in originally locked region
113  */
114  LockedMemory(IAllocator* ptr, void* handle, size_t offsetInBytes = 0)
115  : base(ptr, handle, LOCK_FOR_WRITE, offsetInBytes) {}
116 
117  /**
118  * @brief A default copy constructor, accepting rvalue
119  */
120  LockedMemory(LockedMemory<T>&&) = default;
121 
122  /**
123  * @brief A default copy constructor that accepts rvalue
124  * Also sets the offset value for the new memory object
125  * @param that Rvalue reference for the other LockedMemoryBase instance
126  * @param offset Offset value
127  */
128  LockedMemory(LockedMemory<T>&& that, size_t offset): base(std::move(that)) {
129  base::_offset = offset;
130  }
131 
132  /**
133  * @brief A disabled copy constructor for lvalue
134  */
135  LockedMemory(const LockedMemory<T>&) = delete;
136 
137  /**
138  * @brief Gets a pointer to the stored object.
139  * Dereferences from the base class.
140  * @return The pointer to the object of the given template type
141  */
142  operator T*() {
143  return base::dereference();
144  }
145 
146  /**
147  * @brief Gets the const pointer to the stored object.
148  * Dereferences from the base class.
149  * @return The const pointer object of the given template type.
150  */
151  operator const T*() const {
152  return base::dereference();
153  }
154 
155  /**
156  * @brief Compares stored object with the given one
157  * @return true if objects are equal, false otherwise
158  */
159  bool operator==(const T* pointer) const {
160  // special case with nullptr
161  return base::isEqualTo(pointer);
162  }
163 
164  /**
165  * @brief Compares the object with the one stored in the memory.
166  * @return true if objects are equal, false otherwise
167  */
168  friend bool operator==(const T* pointer, const LockedMemory<T>& lm) {
169  return lm.operator==(pointer);
170  }
171 
172  /**
173  * @brief Casts stored object to any provided type.
174  * Uses reinterpret_cast.
175  * @tparam S Type to be casted to
176  * @return Casted to the given type object
177  */
178  template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
179  S as() {
180  return reinterpret_cast<S>(base::dereference());
181  }
182 
183  /**
184  * @brief Casts stored object to any provided type.
185  * Uses reinterpret_cast.
186  * @tparam S Type to be casted to
187  * @return Casted to the given type const object
188  */
189  template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
190  const S as() const {
191  return reinterpret_cast<S>(base::dereference());
192  }
193 };
194 
195 /**
196  * @brief This class is for <void*> data and allows casting to any pointers
197  */
198 template <>
199 class LockedMemory<void> : public details::LockedMemoryBase<void> {
200  using base = details::LockedMemoryBase<void>;
201 
202 public:
203  /**
204  * @brief A constructor
205  * @param ptr Pointer to IAllocator object
206  * @param handle Handle provided by allocator
207  * @param offsetInBytes Offset in originally locked region
208  */
209  LockedMemory(IAllocator* ptr, void* handle, size_t offsetInBytes)
210  : base(ptr, handle, LOCK_FOR_WRITE, offsetInBytes) {}
211 
212  /**
213  * @brief A default copy constructor that accepts rvalue
214  */
215  LockedMemory(LockedMemory<void>&&) = default;
216 
217  /**
218  * @brief A default copy constructor that accepts rvalue
219  * Also sets the offset value for the new memory object
220  * @param that Rvalue reference for the other LockedMemoryBase instance
221  * @param offset Offset value
222  */
223  LockedMemory(LockedMemory<void>&& that, size_t offset): base(std::move(that)) {
224  base::_offset = offset;
225  }
226 
227  /**
228  * @brief A disabled copy constructor for lvalue
229  */
230  LockedMemory(const LockedMemory<void>&) = delete;
231 
232  /**
233  * @brief Gets the pointer to the stored object of the given template type.
234  * Dereferences from the base class.
235  * @tparam S Type to be casted to
236  * @return The pointer to the object of the given template type
237  */
238  template <class S>
239  operator S*() {
240  return reinterpret_cast<S*>(base::dereference());
241  }
242 
243  /**
244  * @brief Compares stored object with the given one.
245  * @return true if objects are equal, false otherwise
246  */
247  bool operator==(const void* pointer) const {
248  // special case with nullptr
249  return base::isEqualTo(pointer);
250  }
251 
252  /**
253  * @brief Compares the object with the one stored in the memory.
254  * @return true if objects are equal, false otherwise
255  */
256  friend bool operator==(const void* pointer, const LockedMemory<void>& lm) {
257  return lm.operator==(pointer);
258  }
259 
260  /**
261  * @brief Casts stored object to any given type.
262  * Uses reinterpret_cast.
263  * @tparam S Type to be casted to
264  * @return Casted to the given type object
265  */
266  template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
267  S as() {
268  return reinterpret_cast<S>(dereference());
269  }
270 
271  /**
272  * @brief Casts stored object to any given type.
273  * Uses reinterpret_cast.
274  * @tparam S Type to be casted to
275  * @return Casted to the given type const object
276  */
277  template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
278  const S as() const {
279  return reinterpret_cast<S>(dereference());
280  }
281 };
282 
283 /**
284  * @brief This class is for read-only segments
285  */
286 template <class T>
287 class LockedMemory<const T> : public details::LockedMemoryBase<T> {
288  using base = details::LockedMemoryBase<T>;
289 
290 public:
291  /**
292  * @brief A constructor
293  * @param ptr Pointer to IAllocator object
294  * @param handle Handle provided by allocator
295  * @param offsetInBytes Offset in originally locked region
296  */
297  LockedMemory(IAllocator* ptr, void* handle, size_t offset): base(ptr, handle, LOCK_FOR_READ, offset) {}
298 
299  /**
300  * @brief A default copy constructor that accepts rvalue
301  */
302  LockedMemory(LockedMemory<const T>&&) = default;
303 
304  /**
305  * @brief A default copy constructor that accepts rvalue.
306  * Also sets the offset value for the new memory object
307  * @param that Rvalue reference for the other LockedMemoryBase instance
308  * @param offset Offset value
309  */
310  LockedMemory(LockedMemory<const T>&& that, size_t offset): base(std::move(that)) {
311  base::_offset = offset;
312  }
313 
314  /**
315  * @brief A disabled copy constructor for lvalue
316  */
317  LockedMemory(const LockedMemory<const T>&) = delete;
318 
319  /**
320  * @brief Gets the const pointer to the stored object.
321  * Dereferences from the base class.
322  * @return The pointer to the object.
323  */
324  operator const T*() const {
325  return base::dereference();
326  }
327 
328  /**
329  * @brief Compares stored object with the given one
330  * @return true if objects are equal, false otherwise
331  */
332  bool operator==(const T* pointer) const {
333  // special case with nullptr
334  return base::isEqualTo(pointer);
335  }
336 
337  /**
338  * @brief Compares the object with the one stored in the memory.
339  * @return true if objects are equal, false otherwise
340  */
341  friend bool operator==(const T* pointer, const LockedMemory<const T>& lm) {
342  return lm.operator==(pointer);
343  }
344 
345  /**
346  * @brief Casts stored object to any given type.
347  * Uses reinterpret_cast.
348  * @tparam S Type to be casted to
349  * @return Casted to the given type object
350  */
351  template <class S, typename = std::enable_if<std::is_pointer<S>::value && std::is_const<S>::value>>
352  S as() const {
353  return reinterpret_cast<S>(base::dereference());
354  }
355 };
356 } // namespace InferenceEngine
LockedMemory(IAllocator *ptr, void *handle, size_t offsetInBytes=0)
A constructor.
Definition: ie_locked_memory.hpp:114
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:104
LockOp
Allocator handle mapping type.
Definition: ie_allocator.hpp:20
Inference Engine API.
Definition: ie_argmax_layer.hpp:11
friend bool operator==(const T *pointer, const LockedMemory< const T > &lm)
Compares the object with the one stored in the memory.
Definition: ie_locked_memory.hpp:341
bool operator==(const T *pointer) const
Compares stored object with the given one.
Definition: ie_locked_memory.hpp:332
friend bool operator==(const T *pointer, const LockedMemory< T > &lm)
Compares the object with the one stored in the memory.
Definition: ie_locked_memory.hpp:168
bool operator==(const void *pointer) const
Compares stored object with the given one.
Definition: ie_locked_memory.hpp:247
S as()
Casts stored object to any provided type. Uses reinterpret_cast.
Definition: ie_locked_memory.hpp:179
LockedMemory(LockedMemory< void > &&that, size_t offset)
A default copy constructor that accepts rvalue Also sets the offset value for the new memory object...
Definition: ie_locked_memory.hpp:223
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:199
S as() const
Casts stored object to any given type. Uses reinterpret_cast.
Definition: ie_locked_memory.hpp:352
This class is for read-only segments.
Definition: ie_locked_memory.hpp:287
LockedMemory(IAllocator *ptr, void *handle, size_t offset)
A constructor.
Definition: ie_locked_memory.hpp:297
const S as() const
Casts stored object to any given type. Uses reinterpret_cast.
Definition: ie_locked_memory.hpp:278
A header file that provides Allocator interface.
S as()
Casts stored object to any given type. Uses reinterpret_cast.
Definition: ie_locked_memory.hpp:267
LockedMemory(IAllocator *ptr, void *handle, size_t offsetInBytes)
A constructor.
Definition: ie_locked_memory.hpp:209
bool operator==(const T *pointer) const
Compares stored object with the given one.
Definition: ie_locked_memory.hpp:159
friend bool operator==(const void *pointer, const LockedMemory< void > &lm)
Compares the object with the one stored in the memory.
Definition: ie_locked_memory.hpp:256
Allocator concept to be used for memory management and is used as part of the Blob.
Definition: ie_allocator.hpp:25
LockedMemory(LockedMemory< T > &&that, size_t offset)
A default copy constructor that accepts rvalue Also sets the offset value for the new memory object...
Definition: ie_locked_memory.hpp:128
const S as() const
Casts stored object to any provided type. Uses reinterpret_cast.
Definition: ie_locked_memory.hpp:190
LockedMemory(LockedMemory< const T > &&that, size_t offset)
A default copy constructor that accepts rvalue. Also sets the offset value for the new memory object...
Definition: ie_locked_memory.hpp:310