1 /** 2 * Copyright (c) 2021-2022 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 #ifndef PANDA_INTERPRETER_CACHE_H_ 16 #define PANDA_INTERPRETER_CACHE_H_ 17 18 #include <array> 19 #include "libpandabase/utils/math_helpers.h" 20 #include "runtime/include/method.h" 21 22 namespace panda { 23 24 class InterpreterCache { 25 public: Has(const void * pc,Method * caller)26 bool Has(const void *pc, Method *caller) const 27 { 28 const auto &entry = data_[GetIndex(pc)]; 29 return entry.pc == pc && entry.caller == caller; 30 } 31 32 template <class T> Get(const void * pc,Method * caller)33 T *Get(const void *pc, Method *caller) const 34 { 35 if (UNLIKELY(!Has(pc, caller))) { 36 return nullptr; 37 } 38 return static_cast<T *>(data_[GetIndex(pc)].item); 39 } 40 41 template <class T> Set(const void * pc,T * item,Method * caller)42 void Set(const void *pc, T *item, Method *caller) 43 { 44 data_[GetIndex(pc)] = {pc, item, caller}; 45 } 46 Clear()47 void Clear() 48 { 49 data_.fill({}); 50 } 51 52 private: GetIndex(const void * pc)53 static size_t GetIndex(const void *pc) 54 { 55 return panda::helpers::math::PowerOfTwoTableSlot(reinterpret_cast<size_t>(pc), N, 2U); 56 } 57 58 struct Entry { 59 const void *pc {nullptr}; 60 void *item {nullptr}; 61 Method *caller {nullptr}; 62 }; 63 64 static constexpr size_t N = 256; 65 static_assert(panda::helpers::math::IsPowerOfTwo(N)); 66 std::array<Entry, N> data_ {}; 67 }; 68 69 } // namespace panda 70 71 #endif // PANDA_INTERPRETER_CACHE_H_ 72