aboutsummaryrefslogtreecommitdiff
path: root/CacheDataHandler.cpp
blob: 5f3a30764d401dd494a4a61ca607dd175618b9ce (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
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "CacheDataHandler.hpp"

#include <log/log.h>

namespace armnn_driver
{

CacheDataHandler& CacheDataHandlerInstance()
{
    static CacheDataHandler instance;
    return instance;
}

void CacheDataHandler::Register(const HidlToken token, const size_t hashValue, const size_t cacheSize)
{
    if (m_CacheDataMap.find(hashValue) != m_CacheDataMap.end()
                        && m_CacheDataMap.at(hashValue).GetToken() == token
                        && m_CacheDataMap.at(hashValue).GetCacheSize() == cacheSize)
    {
        ALOGV("CacheHandler::Register() Hash value has already registered.");
        return;
    }
    CacheHandle cacheHandle(token, cacheSize);
    m_CacheDataMap.insert({hashValue, cacheHandle});
}

bool CacheDataHandler::Validate(const HidlToken token, const size_t hashValue, const size_t cacheSize) const
{
    return (m_CacheDataMap.find(hashValue) != m_CacheDataMap.end()
                             && m_CacheDataMap.at(hashValue).GetToken() == token
                             && m_CacheDataMap.at(hashValue).GetCacheSize() == cacheSize);
}

size_t CacheDataHandler::Hash(std::vector<uint8_t>& cacheData)
{
    std::size_t hash = cacheData.size();
    for (auto& i : cacheData)
    {
        hash = ((hash << 5) - hash) + i;
    }
    return hash;
}

size_t CacheDataHandler::GetCacheSize(HidlToken token)
{
    for (auto i = m_CacheDataMap.begin(); i != m_CacheDataMap.end(); ++i)
    {
        if (i->second.GetToken() == token)
        {
            return i->second.GetCacheSize();
        }
    }
    return 0;
}

void CacheDataHandler::Clear()
{
    m_CacheDataMap.clear();
}

} // armnn_driver