6 #include "armnnTestUtils/MockTensorHandle.hpp"
11 MockTensorHandle::MockTensorHandle(
const TensorInfo& tensorInfo, std::shared_ptr<MockMemoryManager>& memoryManager)
12 : m_TensorInfo(tensorInfo)
13 , m_MemoryManager(memoryManager)
15 , m_UnmanagedMemory(nullptr)
18 , m_IsImportEnabled(false)
21 MockTensorHandle::MockTensorHandle(
const TensorInfo& tensorInfo,
MemorySourceFlags importFlags)
22 : m_TensorInfo(tensorInfo)
24 , m_UnmanagedMemory(nullptr)
25 , m_ImportFlags(importFlags)
27 , m_IsImportEnabled(true)
30 MockTensorHandle::~MockTensorHandle()
37 ::operator
delete(m_UnmanagedMemory);
42 void MockTensorHandle::Manage()
44 if (!m_IsImportEnabled)
47 ARMNN_ASSERT_MSG(!m_UnmanagedMemory,
"MockTensorHandle::Manage() called after Allocate()");
49 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
53 void MockTensorHandle::Allocate()
56 if (!m_IsImportEnabled)
59 if (!m_UnmanagedMemory)
64 m_UnmanagedMemory = ::operator
new(m_TensorInfo.GetNumBytes());
68 m_MemoryManager->Allocate(m_Pool);
73 throw InvalidArgumentException(
"MockTensorHandle::Allocate Trying to allocate a MockTensorHandle"
74 "that already has allocated memory.");
79 const void* MockTensorHandle::Map(
bool )
const
84 void* MockTensorHandle::GetPointer()
const
86 if (m_UnmanagedMemory)
88 return m_UnmanagedMemory;
92 return m_MemoryManager->GetPointer(m_Pool);
96 throw NullPointerException(
"MockTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
100 void MockTensorHandle::CopyOutTo(
void* dest)
const
102 const void* src = GetPointer();
104 memcpy(dest, src, m_TensorInfo.GetNumBytes());
107 void MockTensorHandle::CopyInFrom(
const void* src)
109 void* dest = GetPointer();
111 memcpy(dest, src, m_TensorInfo.GetNumBytes());
114 bool MockTensorHandle::Import(
void* memory,
MemorySource source)
118 if (m_IsImportEnabled && source == MemorySource::Malloc)
121 if (!CanBeImported(memory, source))
126 m_UnmanagedMemory =
nullptr;
133 if (!m_Imported && !m_UnmanagedMemory)
135 m_UnmanagedMemory = memory;
141 if (!m_Imported && m_UnmanagedMemory)
149 m_UnmanagedMemory = memory;
158 bool MockTensorHandle::CanBeImported(
void* memory,
MemorySource source)
162 if (m_IsImportEnabled && source == MemorySource::Malloc)
165 if (
reinterpret_cast<uintptr_t
>(memory) % alignment)