blob: fe9310f42365f0a4557e7da6de500cddd85f7448 [file] [log] [blame]
Matthew Bentham4cefc412019-06-18 16:14:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include "RefTensorHandle.hpp"
6
7namespace armnn
8{
9
Matthew Bentham7c1603a2019-06-21 17:22:23 +010010RefTensorHandle::RefTensorHandle(const TensorInfo &tensorInfo, std::shared_ptr<RefMemoryManager> &memoryManager):
Matthew Bentham4cefc412019-06-18 16:14:34 +010011 m_TensorInfo(tensorInfo),
Matthew Bentham7c1603a2019-06-21 17:22:23 +010012 m_MemoryManager(memoryManager),
13 m_Pool(nullptr),
14 m_UnmanagedMemory(nullptr)
Matthew Bentham4cefc412019-06-18 16:14:34 +010015{
16
17}
18
19RefTensorHandle::~RefTensorHandle()
20{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010021 if (!m_Pool)
22 {
23 // unmanaged
24 ::operator delete(m_UnmanagedMemory);
25 }
26}
27
28void RefTensorHandle::Manage()
29{
30 BOOST_ASSERT_MSG(!m_Pool, "RefTensorHandle::Manage() called twice");
31 BOOST_ASSERT_MSG(!m_UnmanagedMemory, "RefTensorHandle::Manage() called after Allocate()");
32
33 m_Pool = m_MemoryManager->Manage(m_TensorInfo.GetNumBytes());
Matthew Bentham4cefc412019-06-18 16:14:34 +010034}
35
36void RefTensorHandle::Allocate()
37{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010038 if (!m_UnmanagedMemory)
Matthew Bentham4cefc412019-06-18 16:14:34 +010039 {
Matthew Bentham7c1603a2019-06-21 17:22:23 +010040 if (!m_Pool)
41 {
42 // unmanaged
43 m_UnmanagedMemory = ::operator new(m_TensorInfo.GetNumBytes());
44 }
45 else
46 {
47 m_MemoryManager->Allocate(m_Pool);
48 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010049 }
50 else
51 {
52 throw InvalidArgumentException("RefTensorHandle::Allocate Trying to allocate a RefTensorHandle"
Matthew Bentham7c1603a2019-06-21 17:22:23 +010053 "that already has allocated memory.");
Matthew Bentham4cefc412019-06-18 16:14:34 +010054 }
55}
56
Matthew Bentham7c1603a2019-06-21 17:22:23 +010057const void* RefTensorHandle::Map(bool /*unused*/) const
Matthew Bentham4cefc412019-06-18 16:14:34 +010058{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010059 return GetPointer();
Matthew Bentham4cefc412019-06-18 16:14:34 +010060}
61
Matthew Bentham7c1603a2019-06-21 17:22:23 +010062void* RefTensorHandle::GetPointer() const
Matthew Bentham4cefc412019-06-18 16:14:34 +010063{
Matthew Bentham7c1603a2019-06-21 17:22:23 +010064 if (m_UnmanagedMemory)
65 {
66 return m_UnmanagedMemory;
67 }
68 else
69 {
70 BOOST_ASSERT_MSG(m_Pool, "RefTensorHandle::GetPointer called on unmanaged, unallocated tensor handle");
71 return m_MemoryManager->GetPointer(m_Pool);
72 }
Matthew Bentham4cefc412019-06-18 16:14:34 +010073}
74
Matthew Bentham7c1603a2019-06-21 17:22:23 +010075void RefTensorHandle::CopyOutTo(void* dest) const
76{
77 const void *src = GetPointer();
78 BOOST_ASSERT(src);
79 memcpy(dest, src, m_TensorInfo.GetNumBytes());
80}
81
82void RefTensorHandle::CopyInFrom(const void* src)
83{
84 void *dest = GetPointer();
85 BOOST_ASSERT(dest);
86 memcpy(dest, src, m_TensorInfo.GetNumBytes());
87}
88
89}