Narumol Prangnawarat | 867eba5 | 2020-02-03 12:29:56 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "SampleMemoryManager.hpp" |
| 7 | |
| 8 | #include <algorithm> |
| 9 | |
Jan Eilers | c84e45d | 2020-08-19 14:14:36 +0100 | [diff] [blame] | 10 | namespace sdb // sample dynamic backend |
Narumol Prangnawarat | 867eba5 | 2020-02-03 12:29:56 +0000 | [diff] [blame] | 11 | { |
| 12 | |
| 13 | SampleMemoryManager::SampleMemoryManager() |
| 14 | {} |
| 15 | |
| 16 | SampleMemoryManager::~SampleMemoryManager() |
| 17 | {} |
| 18 | |
| 19 | SampleMemoryManager::Pool* SampleMemoryManager::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 | |
| 35 | void SampleMemoryManager::Allocate(SampleMemoryManager::Pool* pool) |
| 36 | { |
| 37 | m_FreePools.push_back(pool); |
| 38 | } |
| 39 | |
| 40 | void* SampleMemoryManager::GetPointer(SampleMemoryManager::Pool* pool) |
| 41 | { |
| 42 | return pool->GetPointer(); |
| 43 | } |
| 44 | |
| 45 | void SampleMemoryManager::Acquire() |
| 46 | { |
| 47 | for (Pool &pool: m_Pools) |
| 48 | { |
| 49 | pool.Acquire(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void SampleMemoryManager::Release() |
| 54 | { |
| 55 | for (Pool &pool: m_Pools) |
| 56 | { |
| 57 | pool.Release(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | SampleMemoryManager::Pool::Pool(unsigned int numBytes) |
| 62 | : m_Size(numBytes), |
| 63 | m_Pointer(nullptr) |
| 64 | {} |
| 65 | |
| 66 | SampleMemoryManager::Pool::~Pool() |
| 67 | { |
| 68 | if (m_Pointer) |
| 69 | { |
| 70 | Release(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void* SampleMemoryManager::Pool::GetPointer() |
| 75 | { |
| 76 | return m_Pointer; |
| 77 | } |
| 78 | |
| 79 | void SampleMemoryManager::Pool::Reserve(unsigned int numBytes) |
| 80 | { |
| 81 | m_Size = std::max(m_Size, numBytes); |
| 82 | } |
| 83 | |
| 84 | void SampleMemoryManager::Pool::Acquire() |
| 85 | { |
| 86 | m_Pointer = ::operator new(size_t(m_Size)); |
| 87 | } |
| 88 | |
| 89 | void SampleMemoryManager::Pool::Release() |
| 90 | { |
| 91 | ::operator delete(m_Pointer); |
| 92 | m_Pointer = nullptr; |
| 93 | } |
| 94 | |
Jan Eilers | c84e45d | 2020-08-19 14:14:36 +0100 | [diff] [blame] | 95 | } // namespace sdb |