1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
6 * @brief A header file for generic LockedMemory<> and different variations of locks
8 * @file ie_locked_memory.hpp
15 #include "ie_allocator.hpp"
17 namespace InferenceEngine {
20 * @brief This class is a LockedMemory concept for hardware memory
23 class LockedMemoryBase {
24 IAllocator* _allocator = nullptr;
25 void* _handle = nullptr;
26 mutable T* _locked = nullptr;
27 LockOp _lockFlag = LOCK_FOR_WRITE;
31 * @brief An offset size
33 * The default value is 0.
39 * @brief A constructor
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
46 LockedMemoryBase(IAllocator* ptr, void* handle, LockOp lockFlag, size_t offsetInBytes)
47 : _allocator(ptr), _handle(handle), _lockFlag(lockFlag), _offset(offsetInBytes) {}
50 * @brief A copy constructor
52 * @param that An rvalue reference for the other LockedMemoryBase instance
54 LockedMemoryBase(LockedMemoryBase&& that)
55 : _allocator(that._allocator), _handle(that._handle), _lockFlag(that._lockFlag), _offset(that._offset) {
56 that._locked = nullptr;
60 * @brief A virtual destructor
62 virtual ~LockedMemoryBase() {
63 if (_locked != nullptr) {
64 _allocator->unlock(_handle);
70 * @brief Compares referenced values
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
75 bool isEqualTo(const T* pointer) const {
76 if (pointer == nullptr && (_allocator == nullptr || _handle == nullptr)) {
79 return dereference() == pointer;
83 * @brief Gets the locked object.
85 * Locks the handler and casts memory to the object of the given template type.
87 * @return The pointer to the locked object, nullptr otherwise
89 virtual T* dereference() const {
90 if (_locked != nullptr) return _locked;
92 if (_allocator == nullptr) {
96 if (_handle == nullptr) {
100 uint8_t* pBytes = reinterpret_cast<uint8_t*>(_allocator->lock(_handle, _lockFlag));
102 return _locked = reinterpret_cast<T*>(pBytes + _offset);
105 } // namespace details
108 * @brief This class represents locked memory for read/write memory
111 class LockedMemory : public details::LockedMemoryBase<T> {
112 using base = details::LockedMemoryBase<T>;
116 * @brief A constructor
118 * @param ptr Pointer to IAllocator object
119 * @param handle Handle provided by allocator
120 * @param offsetInBytes Offset in originally locked region
122 LockedMemory(IAllocator* ptr, void* handle, size_t offsetInBytes = 0)
123 : base(ptr, handle, LOCK_FOR_WRITE, offsetInBytes) {}
126 * @brief A default copy constructor, accepting rvalue
128 LockedMemory(LockedMemory<T>&&) = default;
131 * @brief A default copy constructor that accepts rvalue
133 * Also sets the offset value for the new memory object
135 * @param that Rvalue reference for the other LockedMemoryBase instance
136 * @param offset Offset value
138 LockedMemory(LockedMemory<T>&& that, size_t offset): base(std::move(that)) {
139 base::_offset = offset;
143 * @brief A disabled copy constructor for lvalue
145 LockedMemory(const LockedMemory<T>&) = delete;
148 * @brief Gets a pointer to the stored object
150 * Dereferences from the base class.
152 * @return The pointer to the object of the given template type
155 return base::dereference();
159 * @brief Gets the const pointer to the stored object
161 * Dereferences from the base class.
162 * @return The const pointer object of the given template type.
164 operator const T*() const {
165 return base::dereference();
169 * @brief Compares stored object with the given one
171 * @return true if objects are equal, false otherwise
173 bool operator==(const T* pointer) const {
174 // special case with nullptr
175 return base::isEqualTo(pointer);
179 * @brief Compares the object with the one stored in the memory.
181 * @return true if objects are equal, false otherwise
183 friend bool operator==(const T* pointer, const LockedMemory<T>& lm) {
184 return lm.operator==(pointer);
188 * @brief Casts stored object to any provided type.
190 * Uses reinterpret_cast.
192 * @tparam S Type to be casted to
193 * @return Casted to the given type object
195 template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
197 return reinterpret_cast<S>(base::dereference());
201 * @brief Casts stored object to any provided type.
203 * Uses reinterpret_cast.
205 * @tparam S Type to be casted to
206 * @return Casted to the given type const object
208 template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
210 return reinterpret_cast<S>(base::dereference());
215 * @brief This class is for <void*> data and allows casting to any pointers
218 class LockedMemory<void> : public details::LockedMemoryBase<void> {
219 using base = details::LockedMemoryBase<void>;
223 * @brief A constructor
225 * @param ptr Pointer to IAllocator object
226 * @param handle Handle provided by allocator
227 * @param offsetInBytes Offset in originally locked region
229 LockedMemory(IAllocator* ptr, void* handle, size_t offsetInBytes)
230 : base(ptr, handle, LOCK_FOR_WRITE, offsetInBytes) {}
233 * @brief A default copy constructor that accepts rvalue
235 LockedMemory(LockedMemory<void>&&) = default;
238 * @brief A default copy constructor that accepts rvalue
240 * Also sets the offset value for the new memory object
242 * @param that Rvalue reference for the other LockedMemoryBase instance
243 * @param offset Offset value
245 LockedMemory(LockedMemory<void>&& that, size_t offset): base(std::move(that)) {
246 base::_offset = offset;
250 * @brief A disabled copy constructor for lvalue
252 LockedMemory(const LockedMemory<void>&) = delete;
255 * @brief Gets the pointer to the stored object of the given template type
257 * Dereferences from the base class.
259 * @tparam S Type to be casted to
260 * @return The pointer to the object of the given template type
264 return reinterpret_cast<S*>(base::dereference());
268 * @brief Compares stored object with the given one
270 * @return true if objects are equal, false otherwise
272 bool operator==(const void* pointer) const {
273 // special case with nullptr
274 return base::isEqualTo(pointer);
278 * @brief Compares the object with the one stored in the memory
280 * @return true if objects are equal, false otherwise
282 friend bool operator==(const void* pointer, const LockedMemory<void>& lm) {
283 return lm.operator==(pointer);
287 * @brief Casts stored object to any given type
289 * Uses reinterpret_cast.
291 * @tparam S Type to be casted to
292 * @return Casted to the given type object
294 template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
296 return reinterpret_cast<S>(dereference());
300 * @brief Casts stored object to any given type
302 * Uses reinterpret_cast.
304 * @tparam S Type to be casted to
305 * @return Casted to the given type const object
307 template <class S, typename = std::enable_if<std::is_pointer<S>::value>>
309 return reinterpret_cast<S>(dereference());
314 * @brief This class is for read-only segments
317 class LockedMemory<const T> : public details::LockedMemoryBase<T> {
318 using base = details::LockedMemoryBase<T>;
322 * @brief A constructor
324 * @param ptr Pointer to IAllocator object
325 * @param handle Handle provided by allocator
326 * @param offsetInBytes Offset in originally locked region
328 LockedMemory(IAllocator* ptr, void* handle, size_t offset): base(ptr, handle, LOCK_FOR_READ, offset) {}
331 * @brief A default copy constructor that accepts rvalue
333 LockedMemory(LockedMemory<const T>&&) = default;
336 * @brief A default copy constructor that accepts rvalue.
338 * Also sets the offset value for the new memory object
340 * @param that Rvalue reference for the other LockedMemoryBase instance
341 * @param offset Offset value
343 LockedMemory(LockedMemory<const T>&& that, size_t offset): base(std::move(that)) {
344 base::_offset = offset;
348 * @brief A disabled copy constructor for lvalue
350 LockedMemory(const LockedMemory<const T>&) = delete;
353 * @brief Gets the const pointer to the stored object
355 * Dereferences from the base class.
357 * @return The pointer to the object.
359 operator const T*() const {
360 return base::dereference();
364 * @brief Compares stored object with the given one
366 * @return true if objects are equal, false otherwise
368 bool operator==(const T* pointer) const {
369 // special case with nullptr
370 return base::isEqualTo(pointer);
374 * @brief Compares the object with the one stored in the memory
376 * @return true if objects are equal, false otherwise
378 friend bool operator==(const T* pointer, const LockedMemory<const T>& lm) {
379 return lm.operator==(pointer);
383 * @brief Casts stored object to any given type.
385 * Uses reinterpret_cast.
387 * @tparam S Type to be casted to
388 * @return Casted to the given type object
390 template <class S, typename = std::enable_if<std::is_pointer<S>::value && std::is_const<S>::value>>
392 return reinterpret_cast<S>(base::dereference());
395 } // namespace InferenceEngine