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