blob: 11ab2118175ed81f246e773df9207823e3d237a1 [file] [log] [blame]
Mathieu Chartier9df89312016-11-23 13:28:16 -08001/*
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_COMPILER_UTILS_ATOMIC_METHOD_REF_MAP_H_
18#define ART_COMPILER_UTILS_ATOMIC_METHOD_REF_MAP_H_
19
20#include "base/dchecked_vector.h"
21#include "method_reference.h"
22#include "safe_map.h"
23
24namespace art {
25
26class DexFile;
27
28// Used by CompilerCallbacks to track verification information from the Runtime.
29template <typename T>
30class AtomicMethodRefMap {
31 public:
32 explicit AtomicMethodRefMap() {}
33 ~AtomicMethodRefMap() {}
34
35 // Atomically swap the element in if the existing value matches expected.
36 enum InsertResult {
37 kInsertResultInvalidDexFile,
38 kInsertResultCASFailure,
39 kInsertResultSuccess,
40 };
41 InsertResult Insert(MethodReference ref, const T& expected, const T& desired);
42
43 // Retreive an item, returns false if the dex file is not added.
44 bool Get(MethodReference ref, T* out) const;
45
46 // Dex files must be added before method references belonging to them can be used as keys. Not
47 // thread safe.
48 void AddDexFile(const DexFile* dex_file);
49
Mathieu Chartieracab8d42016-11-23 13:45:58 -080050 bool HaveDexFile(const DexFile* dex_file) const {
51 return arrays_.find(dex_file) != arrays_.end();
52 }
53
Mathieu Chartier9df89312016-11-23 13:28:16 -080054 // Visit all of the dex files and elements.
55 template <typename Visitor>
56 void Visit(const Visitor& visitor);
57
58 private:
59 // Verified methods. The method array is fixed to avoid needing a lock to extend it.
60 using ElementArray = dchecked_vector<Atomic<T>>;
61 using DexFileArrays = SafeMap<const DexFile*, ElementArray>;
62
63 const ElementArray* GetArray(const DexFile* dex_file) const;
64 ElementArray* GetArray(const DexFile* dex_file);
65
66 DexFileArrays arrays_;
67};
68
69} // namespace art
70
71#endif // ART_COMPILER_UTILS_ATOMIC_METHOD_REF_MAP_H_