blob: 4384b08b4b14247601385fb9c4073bf9465d576a [file] [log] [blame]
Francis Murtagh9270d9e2022-08-12 13:54:17 +01001//
Colm Donelanb4ef1632024-02-01 15:00:43 +00002// Copyright © 2022, 2024 Arm Ltd and Contributors. All rights reserved.
Francis Murtagh9270d9e2022-08-12 13:54:17 +01003// SPDX-License-Identifier: MIT
4//
5#include "TosaRefMemoryManager.hpp"
6
Colm Donelanb4ef1632024-02-01 15:00:43 +00007#include <armnn/Exceptions.hpp>
Francis Murtagh9270d9e2022-08-12 13:54:17 +01008#include <algorithm>
9
10namespace armnn
11{
12
13TosaRefMemoryManager::TosaRefMemoryManager()
14{}
15
16TosaRefMemoryManager::~TosaRefMemoryManager()
17{}
18
19TosaRefMemoryManager::Pool* TosaRefMemoryManager::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
35void TosaRefMemoryManager::Allocate(TosaRefMemoryManager::Pool* pool)
36{
Colm Donelanb4ef1632024-02-01 15:00:43 +000037 ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(pool, "Null memory manager passed to TosaRefMemoryManager.");
Francis Murtagh9270d9e2022-08-12 13:54:17 +010038 m_FreePools.push_back(pool);
39}
40
41void* TosaRefMemoryManager::GetPointer(TosaRefMemoryManager::Pool* pool)
42{
43 return pool->GetPointer();
44}
45
46void TosaRefMemoryManager::Acquire()
47{
48 for (Pool &pool: m_Pools)
49 {
50 pool.Acquire();
51 }
52}
53
54void TosaRefMemoryManager::Release()
55{
56 for (Pool &pool: m_Pools)
57 {
58 pool.Release();
59 }
60}
61
62TosaRefMemoryManager::Pool::Pool(unsigned int numBytes)
63 : m_Size(numBytes),
64 m_Pointer(nullptr)
65{}
66
67TosaRefMemoryManager::Pool::~Pool()
68{
69 if (m_Pointer)
70 {
71 Release();
72 }
73}
74
75void* TosaRefMemoryManager::Pool::GetPointer()
76{
Colm Donelanb4ef1632024-02-01 15:00:43 +000077 ARMNN_THROW_MSG_IF_FALSE(m_Pointer, RuntimeException,
78 "TosaRefMemoryManager::Pool::GetPointer() called when memory not acquired");
Francis Murtagh9270d9e2022-08-12 13:54:17 +010079 return m_Pointer;
80}
81
82void TosaRefMemoryManager::Pool::Reserve(unsigned int numBytes)
83{
Colm Donelanb4ef1632024-02-01 15:00:43 +000084 ARMNN_THROW_MSG_IF_FALSE(!m_Pointer, RuntimeException,
85 "TosaRefMemoryManager::Pool::Reserve() cannot be called after memory acquired");
Francis Murtagh9270d9e2022-08-12 13:54:17 +010086 m_Size = std::max(m_Size, numBytes);
87}
88
89void TosaRefMemoryManager::Pool::Acquire()
90{
Colm Donelanb4ef1632024-02-01 15:00:43 +000091 ARMNN_THROW_MSG_IF_FALSE(!m_Pointer, RuntimeException,
92 "TosaRefMemoryManager::Pool::Acquire() called when memory already acquired");
Francis Murtagh9270d9e2022-08-12 13:54:17 +010093 m_Pointer = ::operator new(size_t(m_Size));
94}
95
96void TosaRefMemoryManager::Pool::Release()
97{
Colm Donelanb4ef1632024-02-01 15:00:43 +000098 ARMNN_THROW_MSG_IF_FALSE(m_Pointer, RuntimeException,
99 "TosaRefMemoryManager::Pool::Release() called when memory not acquired");
Francis Murtagh9270d9e2022-08-12 13:54:17 +0100100 ::operator delete(m_Pointer);
101 m_Pointer = nullptr;
102}
103
104}