aboutsummaryrefslogtreecommitdiff
path: root/src/backends/reference/RefMemoryManager.cpp
blob: 0f4a289807cfa9fb17b680e2a9cdf131690bbb3a (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "RefMemoryManager.hpp"

#include <boost/assert.hpp>

namespace armnn
{

RefMemoryManager::RefMemoryManager()
{}

RefMemoryManager::~RefMemoryManager()
{}

RefMemoryManager::Pool* RefMemoryManager::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 RefMemoryManager::Allocate(RefMemoryManager::Pool* pool)
{
    BOOST_ASSERT(pool);
    m_FreePools.push_back(pool);
}

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

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

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

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

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

void* RefMemoryManager::Pool::GetPointer()
{
    BOOST_ASSERT_MSG(m_Pointer, "RefMemoryManager::Pool::GetPointer() called when memory not acquired"); 
    return m_Pointer;
}

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

void RefMemoryManager::Pool::Acquire()
{
    BOOST_ASSERT_MSG(!m_Pointer, "RefMemoryManager::Pool::Acquire() called when memory already acquired"); 
    BOOST_ASSERT(m_Size >= 0);
    m_Pointer = ::operator new(size_t(m_Size));
}

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

}