blob: 80f3531df8a3b0f286409e3496480e0ef84ecf50 [file] [log] [blame]
Matthew Bentham7c1603a2019-06-21 17:22:23 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
Matthew Bentham7c1603a2019-06-21 17:22:23 +01003// SPDX-License-Identifier: MIT
4//
5#include "RefMemoryManager.hpp"
6
Colm Donelanb4ef1632024-02-01 15:00:43 +00007#include <armnn/Exceptions.hpp>
Matthew Bentham7c1603a2019-06-21 17:22:23 +01008
Rob Hughes91e1d892019-08-23 10:11:58 +01009#include <algorithm>
10
Matthew Bentham7c1603a2019-06-21 17:22:23 +010011namespace armnn
12{
13
14RefMemoryManager::RefMemoryManager()
15{}
16
17RefMemoryManager::~RefMemoryManager()
18{}
19
20RefMemoryManager::Pool* RefMemoryManager::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
36void RefMemoryManager::Allocate(RefMemoryManager::Pool* pool)
37{
Colm Donelanb4ef1632024-02-01 15:00:43 +000038 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(pool, "Null memory manager passed to RefMemoryManager.");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010039 m_FreePools.push_back(pool);
40}
41
42void* RefMemoryManager::GetPointer(RefMemoryManager::Pool* pool)
43{
44 return pool->GetPointer();
45}
46
47void RefMemoryManager::Acquire()
48{
49 for (Pool &pool: m_Pools)
50 {
51 pool.Acquire();
52 }
53}
54
55void RefMemoryManager::Release()
56{
57 for (Pool &pool: m_Pools)
58 {
59 pool.Release();
60 }
61}
62
63RefMemoryManager::Pool::Pool(unsigned int numBytes)
64 : m_Size(numBytes),
65 m_Pointer(nullptr)
66{}
67
68RefMemoryManager::Pool::~Pool()
69{
70 if (m_Pointer)
71 {
72 Release();
73 }
74}
75
76void* RefMemoryManager::Pool::GetPointer()
77{
Colm Donelanb4ef1632024-02-01 15:00:43 +000078 ARMNN_THROW_MSG_IF_FALSE(m_Pointer, RuntimeException,
79 "RefMemoryManager::Pool::GetPointer() called when memory not acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010080 return m_Pointer;
81}
82
83void RefMemoryManager::Pool::Reserve(unsigned int numBytes)
84{
Colm Donelanb4ef1632024-02-01 15:00:43 +000085 ARMNN_THROW_MSG_IF_FALSE(!m_Pointer, RuntimeException,
86 "RefMemoryManager::Pool::Reserve() cannot be called after memory acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010087 m_Size = std::max(m_Size, numBytes);
88}
89
90void RefMemoryManager::Pool::Acquire()
91{
Colm Donelanb4ef1632024-02-01 15:00:43 +000092 ARMNN_THROW_MSG_IF_FALSE(!m_Pointer, RuntimeException,
93 "RefMemoryManager::Pool::Acquire() called when memory already acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010094 m_Pointer = ::operator new(size_t(m_Size));
95}
96
97void RefMemoryManager::Pool::Release()
98{
Colm Donelanb4ef1632024-02-01 15:00:43 +000099 ARMNN_THROW_MSG_IF_FALSE(m_Pointer, RuntimeException,
100 "RefMemoryManager::Pool::Release() called when memory not acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +0100101 ::operator delete(m_Pointer);
102 m_Pointer = nullptr;
103}
104
105}