ArmNN
 24.02
MockMemoryManager.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "armnnTestUtils/MockMemoryManager.hpp"
7 
8 namespace armnn
9 {
10 
11 MockMemoryManager::MockMemoryManager()
12 {}
13 
14 MockMemoryManager::~MockMemoryManager()
15 {}
16 
17 MockMemoryManager::Pool* MockMemoryManager::Manage(unsigned int numBytes)
18 {
19  if (!m_FreePools.empty())
20  {
21  Pool* res = m_FreePools.back();
22  m_FreePools.pop_back();
23  res->Reserve(numBytes);
24  return res;
25  }
26  else
27  {
28  m_Pools.push_front(Pool(numBytes));
29  return &m_Pools.front();
30  }
31 }
32 
33 void MockMemoryManager::Allocate(MockMemoryManager::Pool* pool)
34 {
35  m_FreePools.push_back(pool);
36 }
37 
38 void* MockMemoryManager::GetPointer(MockMemoryManager::Pool* pool)
39 {
40  return pool->GetPointer();
41 }
42 
43 void MockMemoryManager::Acquire()
44 {
45  for (Pool& pool : m_Pools)
46  {
47  pool.Acquire();
48  }
49 }
50 
51 void MockMemoryManager::Release()
52 {
53  for (Pool& pool : m_Pools)
54  {
55  pool.Release();
56  }
57 }
58 
59 MockMemoryManager::Pool::Pool(unsigned int numBytes)
60  : m_Size(numBytes)
61  , m_Pointer(nullptr)
62 {}
63 
64 MockMemoryManager::Pool::~Pool()
65 {
66  if (m_Pointer)
67  {
68  Release();
69  }
70 }
71 
72 void* MockMemoryManager::Pool::GetPointer()
73 {
74  return m_Pointer;
75 }
76 
77 void MockMemoryManager::Pool::Reserve(unsigned int numBytes)
78 {
79  m_Size = std::max(m_Size, numBytes);
80 }
81 
82 void MockMemoryManager::Pool::Acquire()
83 {
84  m_Pointer = ::operator new(size_t(m_Size));
85 }
86 
87 void MockMemoryManager::Pool::Release()
88 {
89  ::operator delete(m_Pointer);
90  m_Pointer = nullptr;
91 }
92 
93 } // namespace armnn
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6