aboutsummaryrefslogtreecommitdiff
path: root/src/backends/gpuFsa/GpuFsaMemoryManager.cpp
blob: 4eefb87d88a76d6b114e522a95299e1e54ab4912 (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
//
// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "GpuFsaMemoryManager.hpp"

#include <armnn/utility/Assert.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)
{
    ARMNN_ASSERT(pool);
    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()
{
    ARMNN_ASSERT_MSG(m_Pointer, "GpuFsaMemoryManager::Pool::GetPointer() called when memory not acquired");
    return m_Pointer;
}

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

void GpuFsaMemoryManager::Pool::Acquire()
{
    ARMNN_ASSERT_MSG(!m_Pointer, "GpuFsaMemoryManager::Pool::Acquire() called when memory already acquired");
    m_Pointer = ::operator new(size_t(m_Size));
}

void GpuFsaMemoryManager::Pool::Release()
{
    ARMNN_ASSERT_MSG(m_Pointer, "GpuFsaMemoryManager::Pool::Release() called when memory not acquired");
    ::operator delete(m_Pointer);
    m_Pointer = nullptr;
}

}