2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/Exceptions.hpp>
7 #include <backendsCommon/CpuTensorHandle.hpp>
14 ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
15 : m_TensorInfo(tensorInfo)
21 const void* ConstCpuTensorHandle::GetConstTensor() const
26 CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
27 : ConstCpuTensorHandle(tensorInfo)
28 , m_MutableMemory(nullptr)
33 void* CpuTensorHandle::GetTensor() const
35 return m_MutableMemory;
38 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
39 : CpuTensorHandle(tensorInfo)
43 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
44 : ScopedCpuTensorHandle(tensor.GetInfo())
46 CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
49 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
50 : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
52 CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
55 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
56 : CpuTensorHandle(other.GetTensorInfo())
61 ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
63 ::operator delete(GetTensor<void>());
69 ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
71 ::operator delete(GetTensor<void>());
74 void ScopedCpuTensorHandle::Allocate()
76 if (GetTensor<void>() == nullptr)
78 SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
82 throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle"
83 "that already has allocated memory.");
87 void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
89 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
92 void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
94 BOOST_ASSERT(GetTensor<void>() == nullptr);
95 BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
100 memcpy(GetTensor<void>(), srcMemory, numBytes);
104 void PassthroughCpuTensorHandle::Allocate()
106 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
109 void ConstPassthroughCpuTensorHandle::Allocate()
111 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");