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