telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #include "BlobMemoryPool.hpp" |
| 6 | |
| 7 | #include <boost/assert.hpp> |
| 8 | |
| 9 | namespace armnn |
| 10 | { |
| 11 | |
| 12 | BlobMemoryPool::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 | |
| 21 | BlobMemoryPool::~BlobMemoryPool() |
| 22 | { |
| 23 | ReleasePool(); |
| 24 | } |
| 25 | |
| 26 | void 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 | |
| 36 | void 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 | |
| 45 | arm_compute::MappingType BlobMemoryPool::mapping_type() const |
| 46 | { |
| 47 | return arm_compute::MappingType::BLOBS; |
| 48 | } |
| 49 | |
| 50 | std::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 | |
| 56 | void 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 | |
| 71 | void 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 |