2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/Exceptions.hpp>
6 #include <backends/CpuTensorHandle.hpp>
13 ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
14 : m_TensorInfo(tensorInfo)
20 const void* ConstCpuTensorHandle::GetConstTensor() const
25 CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
26 : ConstCpuTensorHandle(tensorInfo)
27 , m_MutableMemory(nullptr)
32 void* CpuTensorHandle::GetTensor() const
34 return m_MutableMemory;
37 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
38 : CpuTensorHandle(tensorInfo)
42 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
43 : ScopedCpuTensorHandle(tensor.GetInfo())
45 CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
48 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
49 : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
51 CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
54 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
55 : CpuTensorHandle(other.GetTensorInfo())
60 ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
62 ::operator delete(GetTensor<void>());
68 ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
70 ::operator delete(GetTensor<void>());
73 void ScopedCpuTensorHandle::Allocate()
75 if (GetTensor<void>() == nullptr)
77 SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
81 throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle"
82 "that already has allocated memory.");
86 void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
88 CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
91 void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
93 BOOST_ASSERT(GetTensor<void>() == nullptr);
94 BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
99 memcpy(GetTensor<void>(), srcMemory, numBytes);
103 void PassthroughCpuTensorHandle::Allocate()
105 throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
108 void ConstPassthroughCpuTensorHandle::Allocate()
110 throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");