blob: fc2437999e0df4d3ffdda8845686f94db19dd720 [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
David Sehrc431b9d2018-03-02 12:01:51 -080020#include "base/atomic.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080021#include "base/dchecked_vector.h"
David Sehr67bf42e2018-02-26 16:43:04 -080022#include "base/safe_map.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_reference.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080024
25namespace art {
26
27class DexFile;
28
29// Used by CompilerCallbacks to track verification information from the Runtime.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070030template <typename DexFileReferenceType, typename Value>
Mathieu Chartier93764b82017-07-17 14:51:53 -070031class AtomicDexRefMap {
Mathieu Chartier9df89312016-11-23 13:28:16 -080032 public:
Igor Murashkin2ffb7032017-11-08 13:35:21 -080033 AtomicDexRefMap() {}
Mathieu Chartier93764b82017-07-17 14:51:53 -070034 ~AtomicDexRefMap() {}
Mathieu Chartier9df89312016-11-23 13:28:16 -080035
36 // Atomically swap the element in if the existing value matches expected.
37 enum InsertResult {
38 kInsertResultInvalidDexFile,
39 kInsertResultCASFailure,
40 kInsertResultSuccess,
41 };
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070042 InsertResult Insert(const DexFileReferenceType& ref,
43 const Value& expected,
44 const Value& desired);
Mathieu Chartier9df89312016-11-23 13:28:16 -080045
46 // Retreive an item, returns false if the dex file is not added.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070047 bool Get(const DexFileReferenceType& ref, Value* out) const;
Mathieu Chartier9df89312016-11-23 13:28:16 -080048
Mathieu Chartier279e3a32018-01-24 18:17:55 -080049 // Remove an item and return the existing value. Returns false if the dex file is not added.
50 bool Remove(const DexFileReferenceType& ref, Value* out);
51
Mathieu Chartier9df89312016-11-23 13:28:16 -080052 // Dex files must be added before method references belonging to them can be used as keys. Not
53 // thread safe.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070054 void AddDexFile(const DexFile* dex_file);
Nicolas Geoffray486dda02017-09-11 14:15:52 +010055 void AddDexFiles(const std::vector<const DexFile*>& dex_files);
Mathieu Chartier9df89312016-11-23 13:28:16 -080056
Mathieu Chartieracab8d42016-11-23 13:45:58 -080057 bool HaveDexFile(const DexFile* dex_file) const {
58 return arrays_.find(dex_file) != arrays_.end();
59 }
60
Mathieu Chartier9df89312016-11-23 13:28:16 -080061 // Visit all of the dex files and elements.
62 template <typename Visitor>
63 void Visit(const Visitor& visitor);
64
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000065 void ClearEntries();
66
Mathieu Chartier9df89312016-11-23 13:28:16 -080067 private:
68 // Verified methods. The method array is fixed to avoid needing a lock to extend it.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070069 using ElementArray = dchecked_vector<Atomic<Value>>;
Mathieu Chartier9df89312016-11-23 13:28:16 -080070 using DexFileArrays = SafeMap<const DexFile*, ElementArray>;
71
72 const ElementArray* GetArray(const DexFile* dex_file) const;
73 ElementArray* GetArray(const DexFile* dex_file);
74
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070075 static size_t NumberOfDexIndices(const DexFile* dex_file);
76
Mathieu Chartier9df89312016-11-23 13:28:16 -080077 DexFileArrays arrays_;
78};
79
80} // namespace art
81
Mathieu Chartier93764b82017-07-17 14:51:53 -070082#endif // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_H_