blob: 203e484fb79101166e3f6e27316e19b529376aed [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_INL_H_
18#define ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_INL_H_
Mathieu Chartier9df89312016-11-23 13:28:16 -080019
Mathieu Chartier93764b82017-07-17 14:51:53 -070020#include "atomic_dex_ref_map.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080021
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070022#include <type_traits>
23
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070024#include "class_reference.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070026#include "method_reference.h"
27#include "type_reference.h"
Mathieu Chartier9df89312016-11-23 13:28:16 -080028
29namespace art {
30
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070031template <typename DexFileReferenceType, typename Value>
32inline size_t AtomicDexRefMap<DexFileReferenceType, Value>::NumberOfDexIndices(
33 const DexFile* dex_file) {
34 // TODO: Use specialization for this? Not sure if worth it.
35 static_assert(std::is_same<DexFileReferenceType, MethodReference>::value ||
36 std::is_same<DexFileReferenceType, ClassReference>::value ||
37 std::is_same<DexFileReferenceType, TypeReference>::value,
38 "invalid index type");
39 if (std::is_same<DexFileReferenceType, MethodReference>::value) {
40 return dex_file->NumMethodIds();
41 }
42 if (std::is_same<DexFileReferenceType, ClassReference>::value) {
43 return dex_file->NumClassDefs();
44 }
45 if (std::is_same<DexFileReferenceType, TypeReference>::value) {
46 return dex_file->NumTypeIds();
47 }
48 UNREACHABLE();
49}
50
51template <typename DexFileReferenceType, typename Value>
52inline typename AtomicDexRefMap<DexFileReferenceType, Value>::InsertResult
53 AtomicDexRefMap<DexFileReferenceType, Value>::Insert(const DexFileReferenceType& ref,
54 const Value& expected,
55 const Value& desired) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080056 ElementArray* const array = GetArray(ref.dex_file);
57 if (array == nullptr) {
58 return kInsertResultInvalidDexFile;
59 }
Mathieu Chartier93764b82017-07-17 14:51:53 -070060 DCHECK_LT(ref.index, array->size());
Orion Hodson4557b382018-01-03 11:47:54 +000061 return (*array)[ref.index].CompareAndSetStrongSequentiallyConsistent(expected, desired)
Mathieu Chartier9df89312016-11-23 13:28:16 -080062 ? kInsertResultSuccess
63 : kInsertResultCASFailure;
64}
65
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070066template <typename DexFileReferenceType, typename Value>
67inline bool AtomicDexRefMap<DexFileReferenceType, Value>::Get(const DexFileReferenceType& ref,
68 Value* out) const {
Mathieu Chartier9df89312016-11-23 13:28:16 -080069 const ElementArray* const array = GetArray(ref.dex_file);
70 if (array == nullptr) {
Calin Juravle49ebbb22017-03-27 18:10:47 -070071 return false;
Mathieu Chartier9df89312016-11-23 13:28:16 -080072 }
Mathieu Chartier93764b82017-07-17 14:51:53 -070073 *out = (*array)[ref.index].LoadRelaxed();
Mathieu Chartier9df89312016-11-23 13:28:16 -080074 return true;
75}
76
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070077template <typename DexFileReferenceType, typename Value>
78inline void AtomicDexRefMap<DexFileReferenceType, Value>::AddDexFile(const DexFile* dex_file) {
79 arrays_.Put(dex_file, std::move(ElementArray(NumberOfDexIndices(dex_file))));
Mathieu Chartier9df89312016-11-23 13:28:16 -080080}
81
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070082template <typename DexFileReferenceType, typename Value>
83inline void AtomicDexRefMap<DexFileReferenceType, Value>::AddDexFiles(
84 const std::vector<const DexFile*>& dex_files) {
Nicolas Geoffray486dda02017-09-11 14:15:52 +010085 for (const DexFile* dex_file : dex_files) {
86 if (!HaveDexFile(dex_file)) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070087 AddDexFile(dex_file);
Nicolas Geoffray486dda02017-09-11 14:15:52 +010088 }
89 }
90}
91
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070092template <typename DexFileReferenceType, typename Value>
93inline typename AtomicDexRefMap<DexFileReferenceType, Value>::ElementArray*
94 AtomicDexRefMap<DexFileReferenceType, Value>::GetArray(const DexFile* dex_file) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080095 auto it = arrays_.find(dex_file);
96 return (it != arrays_.end()) ? &it->second : nullptr;
97}
98
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070099template <typename DexFileReferenceType, typename Value>
100inline const typename AtomicDexRefMap<DexFileReferenceType, Value>::ElementArray*
101 AtomicDexRefMap<DexFileReferenceType, Value>::GetArray(const DexFile* dex_file) const {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800102 auto it = arrays_.find(dex_file);
103 return (it != arrays_.end()) ? &it->second : nullptr;
104}
105
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700106template <typename DexFileReferenceType, typename Value> template <typename Visitor>
107inline void AtomicDexRefMap<DexFileReferenceType, Value>::Visit(const Visitor& visitor) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800108 for (auto& pair : arrays_) {
109 const DexFile* dex_file = pair.first;
110 const ElementArray& elements = pair.second;
111 for (size_t i = 0; i < elements.size(); ++i) {
Mathieu Chartier93764b82017-07-17 14:51:53 -0700112 visitor(DexFileReference(dex_file, i), elements[i].LoadRelaxed());
Mathieu Chartier9df89312016-11-23 13:28:16 -0800113 }
114 }
115}
116
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700117template <typename DexFileReferenceType, typename Value>
118inline void AtomicDexRefMap<DexFileReferenceType, Value>::ClearEntries() {
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000119 for (auto& it : arrays_) {
120 for (auto& element : it.second) {
121 element.StoreRelaxed(nullptr);
122 }
123 }
124}
125
Mathieu Chartier9df89312016-11-23 13:28:16 -0800126} // namespace art
127
Mathieu Chartier93764b82017-07-17 14:51:53 -0700128#endif // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_INL_H_