NVIDIA NvNeural SDK  2021.1
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ScopedMemoryAllocation.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_SCOPEDMEMORYALLOCATION_H
27 #define NVNEURAL_SCOPEDMEMORYALLOCATION_H
28 
29 namespace nvneural {
30 
40  {
41  public:
47  ScopedMemoryAllocation(INetworkBackend* pBackend, size_t bufferSize)
48  : m_pBackend{pBackend}
49  , m_hBuffer{nullptr}
50  {
51  const NeuralResult status = pBackend->allocateMemoryBlock(&m_hBuffer, bufferSize);
52  if (failed(status))
53  {
54  m_hBuffer = nullptr;
55  }
56  }
57 
58  ScopedMemoryAllocation(const ScopedMemoryAllocation& copyFrom) = delete;
59 
62  : m_pBackend{moveFrom.m_pBackend}
63  , m_hBuffer{moveFrom.m_hBuffer}
64  {
65  }
66 
69  {
70  if (m_hBuffer)
71  {
72  m_pBackend->freeMemoryBlock(m_hBuffer);
73  }
74  }
75 
76  ScopedMemoryAllocation& operator=(const ScopedMemoryAllocation& copyFrom) = delete;
77 
82  {
83  std::swap(m_pBackend, moveFrom.m_pBackend);
84  std::swap(m_hBuffer, moveFrom.m_hBuffer);
85  return *this;
86  }
87 
89  explicit operator bool() const
90  {
91  return nullptr != m_hBuffer;
92  }
93 
98  {
99  return m_hBuffer;
100  }
101 
105  void* asPointer()
106  {
107  return m_pBackend->getAddressForMemoryBlock(m_hBuffer);
108  }
109 
110  private:
111  INetworkBackend* m_pBackend;
112  MemoryHandle m_hBuffer;
113  };
114 
115 } // namespace nvneural
116 
117 #endif // NVNEURAL_SCOPEDMEMORYALLOCATION_H
constexpr bool failed(NeuralResult result) noexcept
Helper function akin to COM's FAILED() macro.
Definition: CoreTypes.h:287
MemoryHandle__type * MemoryHandle
Opaque typedef used to represent INetworkBackend memory handles.
Definition: CoreTypes.h:624
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:273
INetworkBackend is a runtime-specific interface for CUDA, DirectX, or other system- specific operatio...
Definition: CoreTypes.h:641
virtual NeuralResult allocateMemoryBlock(MemoryHandle *pHandle, size_t byteCount) noexcept=0
Allocates a memory block of the requested size.
virtual NeuralResult freeMemoryBlock(MemoryHandle handle) noexcept=0
Frees a memory block that was allocated with allocateMemoryBlock.
virtual void * getAddressForMemoryBlock(MemoryHandle handle) noexcept=0
Retrieves the raw address corresponding to a MemoryHandle.
ScopedMemoryAllocation is a helper function for allocating memory blocks from the backend while addin...
Definition: ScopedMemoryAllocation.h:40
MemoryHandle handle() const
Returns the memory handle of the allocated memory block.
Definition: ScopedMemoryAllocation.h:97
void * asPointer()
Returns the pointer to the allocated memory block.
Definition: ScopedMemoryAllocation.h:105
~ScopedMemoryAllocation()
Frees the allocation.
Definition: ScopedMemoryAllocation.h:68
ScopedMemoryAllocation & operator=(ScopedMemoryAllocation &&moveFrom)
Takes ownership of an allocation from another ScopedMemoryAllocation.
Definition: ScopedMemoryAllocation.h:81
ScopedMemoryAllocation(INetworkBackend *pBackend, size_t bufferSize)
Creates a temporary memory allocation using a network backend.
Definition: ScopedMemoryAllocation.h:47
ScopedMemoryAllocation(ScopedMemoryAllocation &&moveFrom)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: ScopedMemoryAllocation.h:61