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