ArmNN
 24.02
GpuFsaMemoryManager.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
6 #include "Exceptions.hpp"
7 
8 #include <algorithm>
9 
10 namespace armnn
11 {
12 
14 {}
15 
17 {}
18 
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 
36 {
37  if (pool == nullptr)
38  {
40  "GpuFsaMemoryManager: Allocate: Attempting to allocate a null memory pool ptr");
41  }
42  m_FreePools.push_back(pool);
43 }
44 
46 {
47  return pool->GetPointer();
48 }
49 
51 {
52  for (Pool &pool: m_Pools)
53  {
54  pool.Acquire();
55  }
56 }
57 
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 
72 {
73  if (m_Pointer)
74  {
75  Release();
76  }
77 }
78 
80 {
81  if (m_Pointer == nullptr)
82  {
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  {
94  "GpuFsaMemoryManager::Pool::Reserve() cannot be called after memory acquired");
95  }
96  m_Size = std::max(m_Size, numBytes);
97 }
98 
100 {
101  if (m_Pointer != nullptr)
102  {
104  "GpuFsaMemoryManager::Pool::Acquire() called when memory already acquired");
105  }
106  m_Pointer = ::operator new(size_t(m_Size));
107 }
108 
110 {
111  if (m_Pointer == nullptr)
112  {
114  "GpuFsaMemoryManager::Pool::Release() called when memory not acquired");
115  }
116  ::operator delete(m_Pointer);
117  m_Pointer = nullptr;
118 }
119 
120 }
GpuFsaMemoryManager.hpp
armnn::GpuFsaMemoryManager::Acquire
void Acquire() override
Definition: GpuFsaMemoryManager.cpp:50
armnn::GpuFsaMemoryManager::Manage
Pool * Manage(unsigned int numBytes)
Definition: GpuFsaMemoryManager.cpp:19
armnn::GpuFsaMemoryManager::~GpuFsaMemoryManager
virtual ~GpuFsaMemoryManager()
Definition: GpuFsaMemoryManager.cpp:16
armnn::GpuFsaMemoryManager::Pool::Release
void Release()
Definition: GpuFsaMemoryManager.cpp:109
armnn::GpuFsaMemoryManager::Pool
Definition: GpuFsaMemoryManager.hpp:33
armnn::GpuFsaMemoryManager::GetPointer
void * GetPointer(Pool *pool)
Definition: GpuFsaMemoryManager.cpp:45
armnn::MemoryValidationException
Definition: Exceptions.hpp:158
armnn::GpuFsaMemoryManager::GpuFsaMemoryManager
GpuFsaMemoryManager()
Definition: GpuFsaMemoryManager.cpp:13
armnn::GpuFsaMemoryManager::Pool::~Pool
~Pool()
Definition: GpuFsaMemoryManager.cpp:71
armnn::GpuFsaMemoryManager::Allocate
void Allocate(Pool *pool)
Definition: GpuFsaMemoryManager.cpp:35
armnn::GpuFsaMemoryManager::Pool::Reserve
void Reserve(unsigned int numBytes)
Definition: GpuFsaMemoryManager.cpp:89
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::GpuFsaMemoryManager::Pool::Pool
Pool(unsigned int numBytes)
Definition: GpuFsaMemoryManager.cpp:66
armnn::GpuFsaMemoryManager::Pool::GetPointer
void * GetPointer()
Definition: GpuFsaMemoryManager.cpp:79
armnn::GpuFsaMemoryManager::Release
void Release() override
Definition: GpuFsaMemoryManager.cpp:58
armnn::GpuFsaMemoryManager::Pool::Acquire
void Acquire()
Definition: GpuFsaMemoryManager.cpp:99