Release 18.08
[platform/upstream/armnn.git] / src / armnn / backends / CpuTensorHandle.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "armnn/Exceptions.hpp"
6 #include "CpuTensorHandle.hpp"
7
8 #include <cstring>
9
10 namespace armnn
11 {
12
13 ConstCpuTensorHandle::ConstCpuTensorHandle(const TensorInfo& tensorInfo)
14 : m_TensorInfo(tensorInfo)
15 , m_Memory(nullptr)
16 {
17 }
18
19 template <>
20 const void* ConstCpuTensorHandle::GetConstTensor() const
21 {
22     return m_Memory;
23 }
24
25 CpuTensorHandle::CpuTensorHandle(const TensorInfo& tensorInfo)
26 : ConstCpuTensorHandle(tensorInfo)
27 , m_MutableMemory(nullptr)
28 {
29 }
30
31 template <>
32 void* CpuTensorHandle::GetTensor() const
33 {
34     return m_MutableMemory;
35 }
36
37 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const TensorInfo& tensorInfo)
38 : CpuTensorHandle(tensorInfo)
39 {
40 }
41
42 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstTensor& tensor)
43 : ScopedCpuTensorHandle(tensor.GetInfo())
44 {
45     CopyFrom(tensor.GetMemoryArea(), tensor.GetNumBytes());
46 }
47
48 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ConstCpuTensorHandle& tensorHandle)
49 : ScopedCpuTensorHandle(tensorHandle.GetTensorInfo())
50 {
51     CopyFrom(tensorHandle.GetConstTensor<void>(), tensorHandle.GetTensorInfo().GetNumBytes());
52 }
53
54 ScopedCpuTensorHandle::ScopedCpuTensorHandle(const ScopedCpuTensorHandle& other)
55 : CpuTensorHandle(other.GetTensorInfo())
56 {
57     CopyFrom(other);
58 }
59
60 ScopedCpuTensorHandle& ScopedCpuTensorHandle::operator=(const ScopedCpuTensorHandle& other)
61 {
62     ::operator delete(GetTensor<void>());
63     SetMemory(nullptr);
64     CopyFrom(other);
65     return *this;
66 }
67
68 ScopedCpuTensorHandle::~ScopedCpuTensorHandle()
69 {
70     ::operator delete(GetTensor<void>());
71 }
72
73 void ScopedCpuTensorHandle::Allocate()
74 {
75     if (GetTensor<void>() == nullptr)
76     {
77         SetMemory(::operator new(GetTensorInfo().GetNumBytes()));
78     }
79     else
80     {
81         throw InvalidArgumentException("CpuTensorHandle::Allocate Trying to allocate a CpuTensorHandle"
82             "that already has allocated memory.");
83     }
84 }
85
86 void ScopedCpuTensorHandle::CopyFrom(const ScopedCpuTensorHandle& other)
87 {
88     CopyFrom(other.GetTensor<void>(), other.GetTensorInfo().GetNumBytes());
89 }
90
91 void ScopedCpuTensorHandle::CopyFrom(const void* srcMemory, unsigned int numBytes)
92 {
93     BOOST_ASSERT(GetTensor<void>() == nullptr);
94     BOOST_ASSERT(GetTensorInfo().GetNumBytes() == numBytes);
95
96     if (srcMemory)
97     {
98         Allocate();
99         memcpy(GetTensor<void>(), srcMemory, numBytes);
100     }
101 }
102
103 void PassthroughCpuTensorHandle::Allocate()
104 {
105     throw InvalidArgumentException("PassthroughCpuTensorHandle::Allocate() should never be called");
106 }
107
108 void ConstPassthroughCpuTensorHandle::Allocate()
109 {
110     throw InvalidArgumentException("ConstPassthroughCpuTensorHandle::Allocate() should never be called");
111 }
112
113 } // namespace armnn