Release 18.08
[platform/upstream/armnn.git] / src / armnn / memory / OffsetMemoryPool.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 "OffsetMemoryPool.hpp"
6
7 #include "boost/assert.hpp"
8
9 #include <algorithm>
10
11 namespace armnn
12 {
13
14 OffsetMemoryPool::OffsetMemoryPool(arm_compute::IAllocator* allocator, size_t blobSize)
15     : m_Allocator(allocator)
16     , m_Blob()
17     , m_BlobSize(blobSize)
18     , m_MemoryAllocated(false)
19 {
20     AllocatePool();
21 }
22
23 OffsetMemoryPool::~OffsetMemoryPool()
24 {
25     ReleasePool();
26 }
27
28 void OffsetMemoryPool::acquire(arm_compute::MemoryMappings& handles)
29 {
30     BOOST_ASSERT(m_Blob);
31
32     // Set memory to handlers
33     for(auto& handle : handles)
34     {
35         BOOST_ASSERT(handle.first);
36         *handle.first = reinterpret_cast<uint8_t*>(m_Blob) + handle.second;
37     }
38 }
39
40 void OffsetMemoryPool::release(arm_compute::MemoryMappings &handles)
41 {
42     for(auto& handle : handles)
43     {
44         BOOST_ASSERT(handle.first);
45         *handle.first = nullptr;
46     }
47 }
48
49 arm_compute::MappingType OffsetMemoryPool::mapping_type() const
50 {
51     return arm_compute::MappingType::OFFSETS;
52 }
53
54 std::unique_ptr<arm_compute::IMemoryPool> OffsetMemoryPool::duplicate()
55 {
56     BOOST_ASSERT(m_Allocator);
57     return std::make_unique<OffsetMemoryPool>(m_Allocator, m_BlobSize);
58 }
59
60 void OffsetMemoryPool::AllocatePool()
61 {
62     if (!m_MemoryAllocated)
63     {
64         BOOST_ASSERT(m_Allocator);
65         m_Blob = m_Allocator->allocate(m_BlobSize, 0);
66
67         m_MemoryAllocated = true;
68     }
69 }
70
71 void OffsetMemoryPool::ReleasePool()
72 {
73     if (m_MemoryAllocated)
74     {
75         BOOST_ASSERT(m_Allocator);
76
77         m_Allocator->free(m_Blob);
78         m_Blob = nullptr;
79
80         m_MemoryAllocated = false;
81     }
82 }
83
84 } // namespace armnn