Matthew Bentham | 7c1603a | 2019-06-21 17:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #include "RefMemoryManager.hpp" |
| 6 | |
| 7 | #include <boost/assert.hpp> |
| 8 | |
| 9 | namespace armnn |
| 10 | { |
| 11 | |
| 12 | RefMemoryManager::RefMemoryManager() |
| 13 | {} |
| 14 | |
| 15 | RefMemoryManager::~RefMemoryManager() |
| 16 | {} |
| 17 | |
| 18 | RefMemoryManager::Pool* RefMemoryManager::Manage(unsigned int numBytes) |
| 19 | { |
| 20 | if (!m_FreePools.empty()) |
| 21 | { |
| 22 | Pool* res = m_FreePools.back(); |
| 23 | m_FreePools.pop_back(); |
| 24 | res->Reserve(numBytes); |
| 25 | return res; |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | m_Pools.push_front(Pool(numBytes)); |
| 30 | return &m_Pools.front(); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void RefMemoryManager::Allocate(RefMemoryManager::Pool* pool) |
| 35 | { |
| 36 | BOOST_ASSERT(pool); |
| 37 | m_FreePools.push_back(pool); |
| 38 | } |
| 39 | |
| 40 | void* RefMemoryManager::GetPointer(RefMemoryManager::Pool* pool) |
| 41 | { |
| 42 | return pool->GetPointer(); |
| 43 | } |
| 44 | |
| 45 | void RefMemoryManager::Acquire() |
| 46 | { |
| 47 | for (Pool &pool: m_Pools) |
| 48 | { |
| 49 | pool.Acquire(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void RefMemoryManager::Release() |
| 54 | { |
| 55 | for (Pool &pool: m_Pools) |
| 56 | { |
| 57 | pool.Release(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | RefMemoryManager::Pool::Pool(unsigned int numBytes) |
| 62 | : m_Size(numBytes), |
| 63 | m_Pointer(nullptr) |
| 64 | {} |
| 65 | |
| 66 | RefMemoryManager::Pool::~Pool() |
| 67 | { |
| 68 | if (m_Pointer) |
| 69 | { |
| 70 | Release(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void* RefMemoryManager::Pool::GetPointer() |
| 75 | { |
| 76 | BOOST_ASSERT_MSG(m_Pointer, "RefMemoryManager::Pool::GetPointer() called when memory not acquired"); |
| 77 | return m_Pointer; |
| 78 | } |
| 79 | |
| 80 | void RefMemoryManager::Pool::Reserve(unsigned int numBytes) |
| 81 | { |
| 82 | BOOST_ASSERT_MSG(!m_Pointer, "RefMemoryManager::Pool::Reserve() cannot be called after memory acquired"); |
| 83 | m_Size = std::max(m_Size, numBytes); |
| 84 | } |
| 85 | |
| 86 | void RefMemoryManager::Pool::Acquire() |
| 87 | { |
| 88 | BOOST_ASSERT_MSG(!m_Pointer, "RefMemoryManager::Pool::Acquire() called when memory already acquired"); |
| 89 | BOOST_ASSERT(m_Size >= 0); |
| 90 | m_Pointer = ::operator new(size_t(m_Size)); |
| 91 | } |
| 92 | |
| 93 | void RefMemoryManager::Pool::Release() |
| 94 | { |
| 95 | BOOST_ASSERT_MSG(m_Pointer, "RefMemoryManager::Pool::Release() called when memory not acquired"); |
| 96 | ::operator delete(m_Pointer); |
| 97 | m_Pointer = nullptr; |
| 98 | } |
| 99 | |
| 100 | } |