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