blob: 4f15e39ee18e7329bb0e8d6271fc58a2ba6c1542 [file] [log] [blame]
Matthew Bentham7c1603a2019-06-21 17:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5#include "RefMemoryManager.hpp"
6
7#include <boost/assert.hpp>
8
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{
38 BOOST_ASSERT(pool);
39 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{
Rob Hughes91e1d892019-08-23 10:11:58 +010078 BOOST_ASSERT_MSG(m_Pointer, "RefMemoryManager::Pool::GetPointer() called when memory not acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010079 return m_Pointer;
80}
81
82void RefMemoryManager::Pool::Reserve(unsigned int numBytes)
83{
84 BOOST_ASSERT_MSG(!m_Pointer, "RefMemoryManager::Pool::Reserve() cannot be called after memory acquired");
85 m_Size = std::max(m_Size, numBytes);
86}
87
88void RefMemoryManager::Pool::Acquire()
89{
Rob Hughes91e1d892019-08-23 10:11:58 +010090 BOOST_ASSERT_MSG(!m_Pointer, "RefMemoryManager::Pool::Acquire() called when memory already acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010091 m_Pointer = ::operator new(size_t(m_Size));
92}
93
94void RefMemoryManager::Pool::Release()
95{
Rob Hughes91e1d892019-08-23 10:11:58 +010096 BOOST_ASSERT_MSG(m_Pointer, "RefMemoryManager::Pool::Release() called when memory not acquired");
Matthew Bentham7c1603a2019-06-21 17:22:23 +010097 ::operator delete(m_Pointer);
98 m_Pointer = nullptr;
99}
100
101}