blob: 3ff6ec85d9d10e963fd5af63097bedb82c7fe4e8 [file] [log] [blame]
Colm Donelan17948b52022-02-01 23:37:04 +00001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "armnnTestUtils/MockMemoryManager.hpp"
7
8namespace armnn
9{
10
11MockMemoryManager::MockMemoryManager()
12{}
13
14MockMemoryManager::~MockMemoryManager()
15{}
16
17MockMemoryManager::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
33void MockMemoryManager::Allocate(MockMemoryManager::Pool* pool)
34{
35 m_FreePools.push_back(pool);
36}
37
38void* MockMemoryManager::GetPointer(MockMemoryManager::Pool* pool)
39{
40 return pool->GetPointer();
41}
42
43void MockMemoryManager::Acquire()
44{
45 for (Pool& pool : m_Pools)
46 {
47 pool.Acquire();
48 }
49}
50
51void MockMemoryManager::Release()
52{
53 for (Pool& pool : m_Pools)
54 {
55 pool.Release();
56 }
57}
58
59MockMemoryManager::Pool::Pool(unsigned int numBytes)
60 : m_Size(numBytes)
61 , m_Pointer(nullptr)
62{}
63
64MockMemoryManager::Pool::~Pool()
65{
66 if (m_Pointer)
67 {
68 Release();
69 }
70}
71
72void* MockMemoryManager::Pool::GetPointer()
73{
74 return m_Pointer;
75}
76
77void MockMemoryManager::Pool::Reserve(unsigned int numBytes)
78{
79 m_Size = std::max(m_Size, numBytes);
80}
81
82void MockMemoryManager::Pool::Acquire()
83{
84 m_Pointer = ::operator new(size_t(m_Size));
85}
86
87void MockMemoryManager::Pool::Release()
88{
89 ::operator delete(m_Pointer);
90 m_Pointer = nullptr;
91}
92
93} // namespace armnn