24.08
RefMemoryManager.cpp
Go to the documentation of this file.
1
//
2
// Copyright © 2017, 2024 Arm Ltd. All rights reserved.
3
// SPDX-License-Identifier: MIT
4
//
5
#include "
RefMemoryManager.hpp
"
6
7
#include <
armnn/Exceptions.hpp
>
8
9
#include <algorithm>
10
11
namespace
armnn
12
{
13
14
RefMemoryManager::RefMemoryManager
()
15
{}
16
17
RefMemoryManager::~RefMemoryManager
()
18
{}
19
20
RefMemoryManager::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
36
void
RefMemoryManager::Allocate
(
RefMemoryManager::Pool
* pool)
37
{
38
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
(pool,
"Null memory manager passed to RefMemoryManager."
);
39
m_FreePools.push_back(pool);
40
}
41
42
void
*
RefMemoryManager::GetPointer
(
RefMemoryManager::Pool
* pool)
43
{
44
return
pool->
GetPointer
();
45
}
46
47
void
RefMemoryManager::Acquire
()
48
{
49
for
(
Pool
&pool: m_Pools)
50
{
51
pool.Acquire();
52
}
53
}
54
55
void
RefMemoryManager::Release
()
56
{
57
for
(
Pool
&pool: m_Pools)
58
{
59
pool.Release();
60
}
61
}
62
63
RefMemoryManager::Pool::Pool
(
unsigned
int
numBytes)
64
: m_Size(numBytes),
65
m_Pointer(nullptr)
66
{}
67
68
RefMemoryManager::Pool::~Pool
()
69
{
70
if
(m_Pointer)
71
{
72
Release
();
73
}
74
}
75
76
void
*
RefMemoryManager::Pool::GetPointer
()
77
{
78
ARMNN_THROW_MSG_IF_FALSE
(m_Pointer,
RuntimeException
,
79
"RefMemoryManager::Pool::GetPointer() called when memory not acquired"
);
80
return
m_Pointer;
81
}
82
83
void
RefMemoryManager::Pool::Reserve
(
unsigned
int
numBytes)
84
{
85
ARMNN_THROW_MSG_IF_FALSE
(!m_Pointer,
RuntimeException
,
86
"RefMemoryManager::Pool::Reserve() cannot be called after memory acquired"
);
87
m_Size = std::max(m_Size, numBytes);
88
}
89
90
void
RefMemoryManager::Pool::Acquire
()
91
{
92
ARMNN_THROW_MSG_IF_FALSE
(!m_Pointer,
RuntimeException
,
93
"RefMemoryManager::Pool::Acquire() called when memory already acquired"
);
94
m_Pointer = ::operator
new
(size_t(m_Size));
95
}
96
97
void
RefMemoryManager::Pool::Release
()
98
{
99
ARMNN_THROW_MSG_IF_FALSE
(m_Pointer,
RuntimeException
,
100
"RefMemoryManager::Pool::Release() called when memory not acquired"
);
101
::operator
delete
(m_Pointer);
102
m_Pointer =
nullptr
;
103
}
104
105
}
armnn::RefMemoryManager::RefMemoryManager
RefMemoryManager()
Definition:
RefMemoryManager.cpp:14
armnn::RefMemoryManager::Release
void Release() override
Definition:
RefMemoryManager.cpp:55
armnn::RefMemoryManager::GetPointer
void * GetPointer(Pool *pool)
Definition:
RefMemoryManager.cpp:42
armnn::RefMemoryManager::Manage
Pool * Manage(unsigned int numBytes)
Definition:
RefMemoryManager.cpp:20
armnn::RefMemoryManager::Pool::Acquire
void Acquire()
Definition:
RefMemoryManager.cpp:90
armnn::RefMemoryManager::Pool::Release
void Release()
Definition:
RefMemoryManager.cpp:97
armnn::RefMemoryManager::Acquire
void Acquire() override
Definition:
RefMemoryManager.cpp:47
armnn::RefMemoryManager::Pool
Definition:
RefMemoryManager.hpp:33
armnn::RefMemoryManager::Pool::Pool
Pool(unsigned int numBytes)
Definition:
RefMemoryManager.cpp:63
RefMemoryManager.hpp
ARMNN_THROW_MSG_IF_FALSE
#define ARMNN_THROW_MSG_IF_FALSE(_cond, _except, _str)
Definition:
Exceptions.hpp:206
armnn::RuntimeException
Definition:
Exceptions.hpp:120
armnn::RefMemoryManager::Pool::GetPointer
void * GetPointer()
Definition:
RefMemoryManager.cpp:76
armnn::RefMemoryManager::Pool::~Pool
~Pool()
Definition:
RefMemoryManager.cpp:68
armnn::RefMemoryManager::Allocate
void Allocate(Pool *pool)
Definition:
RefMemoryManager.cpp:36
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition:
01_00_quick_start.dox:6
armnn::RefMemoryManager::Pool::Reserve
void Reserve(unsigned int numBytes)
Definition:
RefMemoryManager.cpp:83
armnn::RefMemoryManager::~RefMemoryManager
virtual ~RefMemoryManager()
Definition:
RefMemoryManager.cpp:17
ARMNN_THROW_INVALIDARG_MSG_IF_FALSE
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
Definition:
Exceptions.hpp:210
src
backends
reference
RefMemoryManager.cpp
Generated on Wed Aug 28 2024 14:31:51 for Arm NN by
1.8.17