aboutsummaryrefslogtreecommitdiff
path: root/src/backends/gpuFsa/GpuFsaMemoryManager.cpp
blob: e16c02d18e2c51663fc8b63419e5083432d3fe8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "GpuFsaMemoryManager.hpp"
#include "Exceptions.hpp"

#include <algorithm>

namespace armnn
{

GpuFsaMemoryManager::GpuFsaMemoryManager()
{}

GpuFsaMemoryManager::~GpuFsaMemoryManager()
{}

GpuFsaMemoryManager::Pool* GpuFsaMemoryManager::Manage(unsigned int numBytes)
{
    if (!m_FreePools.empty())
    {
        Pool* res = m_FreePools.back();
        m_FreePools.pop_back();
        res->Reserve(numBytes);
        return res;
    }
    else
    {
        m_Pools.push_front(Pool(numBytes));
        return &m_Pools.front();
    }
}

void GpuFsaMemoryManager::Allocate(GpuFsaMemoryManager::Pool* pool)
{
    if (pool == nullptr)
    {
        throw armnn::MemoryValidationException(
            "GpuFsaMemoryManager: Allocate: Attempting to allocate a null memory pool ptr");
    }
    m_FreePools.push_back(pool);
}

void* GpuFsaMemoryManager::GetPointer(GpuFsaMemoryManager::Pool* pool)
{
    return pool->GetPointer();
}

void GpuFsaMemoryManager::Acquire()
{
    for (Pool &pool: m_Pools)
    {
        pool.Acquire();
    }
}

void GpuFsaMemoryManager::Release()
{
    for (Pool &pool: m_Pools)
    {
        pool.Release();
    }
}

GpuFsaMemoryManager::Pool::Pool(unsigned int numBytes)
        : m_Size(numBytes),
          m_Pointer(nullptr)
{}

GpuFsaMemoryManager::Pool::~Pool()
{
    if (m_Pointer)
    {
        Release();
    }
}

void* GpuFsaMemoryManager::Pool::GetPointer()
{
    if (m_Pointer == nullptr)
    {
        throw armnn::MemoryValidationException(
            "GpuFsaMemoryManager::Pool::GetPointer() called when memory not acquired");
    }
    return m_Pointer;
}

void GpuFsaMemoryManager::Pool::Reserve(unsigned int numBytes)
{
    if (m_Pointer != nullptr)
    {
        throw armnn::MemoryValidationException(
            "GpuFsaMemoryManager::Pool::Reserve() cannot be called after memory acquired");
    }
    m_Size = std::max(m_Size, numBytes);
}

void GpuFsaMemoryManager::Pool::Acquire()
{
    if (m_Pointer != nullptr)
    {
        throw armnn::MemoryValidationException(
            "GpuFsaMemoryManager::Pool::Acquire() called when memory already acquired");
    }
    m_Pointer = ::operator new(size_t(m_Size));
}

void GpuFsaMemoryManager::Pool::Release()
{
    if (m_Pointer == nullptr)
    {
        throw armnn::MemoryValidationException(
            "GpuFsaMemoryManager::Pool::Release() called when memory not acquired");
    }
    ::operator delete(m_Pointer);
    m_Pointer = nullptr;
}

}