blob: 48bea5e845f072d64d370e23ed837eae99c42a30 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include "OffsetMemoryPool.hpp"
6
Aron Virginas-Tarf9aeef02018-10-12 15:18:03 +01007#include <boost/assert.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01008
9#include <algorithm>
10
11namespace armnn
12{
13
14OffsetMemoryPool::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
23OffsetMemoryPool::~OffsetMemoryPool()
24{
25 ReleasePool();
26}
27
28void 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
40void 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
49arm_compute::MappingType OffsetMemoryPool::mapping_type() const
50{
51 return arm_compute::MappingType::OFFSETS;
52}
53
54std::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
60void 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
71void 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