1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "CacheDataHandler.hpp"
7
8 #include <log/log.h>
9
10 namespace armnn_driver
11 {
12
CacheDataHandlerInstance()13 CacheDataHandler& CacheDataHandlerInstance()
14 {
15 static CacheDataHandler instance;
16 return instance;
17 }
18
Register(const HidlToken token,const size_t hashValue,const size_t cacheSize)19 void CacheDataHandler::Register(const HidlToken token, const size_t hashValue, const size_t cacheSize)
20 {
21 if (m_CacheDataMap.find(hashValue) != m_CacheDataMap.end()
22 && m_CacheDataMap.at(hashValue).GetToken() == token
23 && m_CacheDataMap.at(hashValue).GetCacheSize() == cacheSize)
24 {
25 ALOGV("CacheHandler::Register() Hash value has already registered.");
26 return;
27 }
28 CacheHandle cacheHandle(token, cacheSize);
29 m_CacheDataMap.insert({hashValue, cacheHandle});
30 }
31
Validate(const HidlToken token,const size_t hashValue,const size_t cacheSize) const32 bool CacheDataHandler::Validate(const HidlToken token, const size_t hashValue, const size_t cacheSize) const
33 {
34 return (m_CacheDataMap.find(hashValue) != m_CacheDataMap.end()
35 && m_CacheDataMap.at(hashValue).GetToken() == token
36 && m_CacheDataMap.at(hashValue).GetCacheSize() == cacheSize);
37 }
38
Hash(std::vector<uint8_t> & cacheData)39 size_t CacheDataHandler::Hash(std::vector<uint8_t>& cacheData)
40 {
41 std::size_t hash = cacheData.size();
42 for (auto& i : cacheData)
43 {
44 hash = ((hash << 5) - hash) + i;
45 }
46 return hash;
47 }
48
GetCacheSize(HidlToken token)49 size_t CacheDataHandler::GetCacheSize(HidlToken token)
50 {
51 for (auto i = m_CacheDataMap.begin(); i != m_CacheDataMap.end(); ++i)
52 {
53 if (i->second.GetToken() == token)
54 {
55 return i->second.GetCacheSize();
56 }
57 }
58 return 0;
59 }
60
Clear()61 void CacheDataHandler::Clear()
62 {
63 m_CacheDataMap.clear();
64 }
65
66 } // armnn_driver
67