aboutsummaryrefslogtreecommitdiff
path: root/shim/sl/canonical/CacheDataHandler.cpp
blob: 930a8e4264b05a8b59f45c5b44c85ad2e3862727 (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
//
// Copyright © 2022 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 android::nn::CacheToken token, const size_t hashValue, const size_t cacheSize)
{
    if (!m_CacheDataMap.empty()
            && m_CacheDataMap.find(hashValue) != m_CacheDataMap.end()
            && m_CacheDataMap.at(hashValue).GetToken() == token
            && m_CacheDataMap.at(hashValue).GetCacheSize() == cacheSize)
    {
        return;
    }
    CacheHandle cacheHandle(token, cacheSize);
    m_CacheDataMap.insert({hashValue, cacheHandle});
}

bool CacheDataHandler::Validate(const android::nn::CacheToken token,
                                const size_t hashValue,
                                const size_t cacheSize) const
{
    return (!m_CacheDataMap.empty()
            && 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(android::nn::CacheToken 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