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