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