1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_VERIFICATION_CACHE_FILE_ENTITY_CACHE_H_ 17 #define PANDA_VERIFICATION_CACHE_FILE_ENTITY_CACHE_H_ 18 19 #include "macros.h" 20 21 #include "verification/util/misc.h" 22 #include "verification/util/invalid_ref.h" 23 24 #include "libpandafile/file.h" 25 26 #include "runtime/include/mem/panda_containers.h" 27 28 #include <cstdint> 29 #include <tuple> 30 #include <type_traits> 31 32 namespace panda::verifier { 33 34 template <typename... CachedTypes> 35 class FileEntityCache { 36 using Storage = PandaUnorderedMap<std::pair<uint64_t, std::pair<uint32_t, size_t>>, void *>; 37 using EntityTypes = std::tuple<CachedTypes...>; 38 39 public: 40 template <typename Entity> GetCached(const panda_file::File * pf,uint32_t file_offset)41 Entity &GetCached(const panda_file::File *pf, uint32_t file_offset) const 42 { 43 const auto it = storage.find(std::make_pair( 44 pf->GetUniqId(), std::make_pair(file_offset, std::tuple_type_index<Entity, EntityTypes>::value))); 45 if (it != storage.cend()) { 46 return *reinterpret_cast<Entity *>(it->second); 47 } 48 return Invalid<Entity>(); 49 } 50 51 template <typename Entity> AddToCache(const panda_file::File * pf,uint32_t file_offset,const Entity & entity)52 void AddToCache(const panda_file::File *pf, uint32_t file_offset, const Entity &entity) 53 { 54 storage.insert_or_assign( 55 std::make_pair(pf->GetUniqId(), 56 std::make_pair(file_offset, std::tuple_type_index<Entity, EntityTypes>::value)), 57 const_cast<void *>(reinterpret_cast<const void *>(&entity))); 58 } 59 60 private: 61 Storage storage; 62 }; 63 64 } // namespace panda::verifier 65 66 #endif // PANDA_VERIFICATION_CACHE_FILE_ENTITY_CACHE_H_ 67