blob: b02c9b634e4f97642dd42f3fbb344ee003ed6d0e [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
Mathieu Chartier93764b82017-07-17 14:51:53 -070017#ifndef ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_
18#define ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_
Mathieu Chartier9df89312016-11-23 13:28:16 -080019
20#include "base/dchecked_vector.h"
Mathieu Chartier93764b82017-07-17 14:51:53 -070021#include "dex_file.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080022#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>
Mathieu Chartier93764b82017-07-17 14:51:53 -070030class AtomicDexRefMap {
Mathieu Chartier9df89312016-11-23 13:28:16 -080031 public:
Mathieu Chartier93764b82017-07-17 14:51:53 -070032 explicit AtomicDexRefMap() {}
33 ~AtomicDexRefMap() {}
Mathieu Chartier9df89312016-11-23 13:28:16 -080034
35 // Atomically swap the element in if the existing value matches expected.
36 enum InsertResult {
37 kInsertResultInvalidDexFile,
38 kInsertResultCASFailure,
39 kInsertResultSuccess,
40 };
Mathieu Chartier93764b82017-07-17 14:51:53 -070041 InsertResult Insert(DexFileReference ref, const T& expected, const T& desired);
Mathieu Chartier9df89312016-11-23 13:28:16 -080042
43 // Retreive an item, returns false if the dex file is not added.
Mathieu Chartier93764b82017-07-17 14:51:53 -070044 bool Get(DexFileReference ref, T* out) const;
Mathieu Chartier9df89312016-11-23 13:28:16 -080045
46 // Dex files must be added before method references belonging to them can be used as keys. Not
47 // thread safe.
Mathieu Chartier93764b82017-07-17 14:51:53 -070048 void AddDexFile(const DexFile* dex_file, size_t max_index);
Nicolas Geoffray486dda02017-09-11 14:15:52 +010049 void AddDexFiles(const std::vector<const DexFile*>& dex_files);
Mathieu Chartier9df89312016-11-23 13:28:16 -080050
Mathieu Chartieracab8d42016-11-23 13:45:58 -080051 bool HaveDexFile(const DexFile* dex_file) const {
52 return arrays_.find(dex_file) != arrays_.end();
53 }
54
Mathieu Chartier9df89312016-11-23 13:28:16 -080055 // Visit all of the dex files and elements.
56 template <typename Visitor>
57 void Visit(const Visitor& visitor);
58
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000059 void ClearEntries();
60
Mathieu Chartier9df89312016-11-23 13:28:16 -080061 private:
62 // Verified methods. The method array is fixed to avoid needing a lock to extend it.
63 using ElementArray = dchecked_vector<Atomic<T>>;
64 using DexFileArrays = SafeMap<const DexFile*, ElementArray>;
65
66 const ElementArray* GetArray(const DexFile* dex_file) const;
67 ElementArray* GetArray(const DexFile* dex_file);
68
69 DexFileArrays arrays_;
70};
71
72} // namespace art
73
Mathieu Chartier93764b82017-07-17 14:51:53 -070074#endif // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_