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