xref: /aosp_15_r20/art/compiler/utils/atomic_dex_ref_map.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/dchecked_vector.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
23*795d594fSAndroid Build Coastguard Worker #include "base/safe_map.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_reference.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker class DexFile;
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker // Used by CompilerCallbacks to track verification information from the Runtime.
31*795d594fSAndroid Build Coastguard Worker template <typename DexFileReferenceType, typename Value>
32*795d594fSAndroid Build Coastguard Worker class AtomicDexRefMap {
33*795d594fSAndroid Build Coastguard Worker  public:
AtomicDexRefMap()34*795d594fSAndroid Build Coastguard Worker   AtomicDexRefMap() {}
~AtomicDexRefMap()35*795d594fSAndroid Build Coastguard Worker   ~AtomicDexRefMap() {}
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker   // Atomically swap the element in if the existing value matches expected.
38*795d594fSAndroid Build Coastguard Worker   enum InsertResult {
39*795d594fSAndroid Build Coastguard Worker     kInsertResultInvalidDexFile,
40*795d594fSAndroid Build Coastguard Worker     kInsertResultCASFailure,
41*795d594fSAndroid Build Coastguard Worker     kInsertResultSuccess,
42*795d594fSAndroid Build Coastguard Worker   };
43*795d594fSAndroid Build Coastguard Worker   InsertResult Insert(const DexFileReferenceType& ref,
44*795d594fSAndroid Build Coastguard Worker                       const Value& expected,
45*795d594fSAndroid Build Coastguard Worker                       const Value& desired);
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker   // Retreive an item, returns false if the dex file is not added.
48*795d594fSAndroid Build Coastguard Worker   bool Get(const DexFileReferenceType& ref, Value* out) const;
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker   // Remove an item and return the existing value. Returns false if the dex file is not added.
51*795d594fSAndroid Build Coastguard Worker   bool Remove(const DexFileReferenceType& ref, Value* out);
52*795d594fSAndroid Build Coastguard Worker 
53*795d594fSAndroid Build Coastguard Worker   // Dex files must be added before method references belonging to them can be used as keys. Not
54*795d594fSAndroid Build Coastguard Worker   // thread safe.
55*795d594fSAndroid Build Coastguard Worker   void AddDexFile(const DexFile* dex_file);
56*795d594fSAndroid Build Coastguard Worker   void AddDexFiles(const std::vector<const DexFile*>& dex_files);
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker   // Return a vector of all dex files which were added to the map.
59*795d594fSAndroid Build Coastguard Worker   std::vector<const DexFile*> GetDexFiles() const;
60*795d594fSAndroid Build Coastguard Worker 
HaveDexFile(const DexFile * dex_file)61*795d594fSAndroid Build Coastguard Worker   bool HaveDexFile(const DexFile* dex_file) const {
62*795d594fSAndroid Build Coastguard Worker     return arrays_.find(dex_file) != arrays_.end();
63*795d594fSAndroid Build Coastguard Worker   }
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker   // Visit all of the dex files and elements.
66*795d594fSAndroid Build Coastguard Worker   template <typename Visitor>
67*795d594fSAndroid Build Coastguard Worker   void Visit(const Visitor& visitor);
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker   void ClearEntries();
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker  private:
72*795d594fSAndroid Build Coastguard Worker   // Verified methods. The method array is fixed to avoid needing a lock to extend it.
73*795d594fSAndroid Build Coastguard Worker   using ElementArray = dchecked_vector<Atomic<Value>>;
74*795d594fSAndroid Build Coastguard Worker   using DexFileArrays = SafeMap<const DexFile*, ElementArray>;
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   const ElementArray* GetArray(const DexFile* dex_file) const;
77*795d594fSAndroid Build Coastguard Worker   ElementArray* GetArray(const DexFile* dex_file);
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker   static size_t NumberOfDexIndices(const DexFile* dex_file);
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker   DexFileArrays arrays_;
82*795d594fSAndroid Build Coastguard Worker };
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker }  // namespace art
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker #endif  // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_
87