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