blob: 9915498accaf703f02aa44ae09854fc3020dd2be [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
David Sehr312f3b22018-03-19 08:39:26 -070024#include "dex/class_reference.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
David Sehr312f3b22018-03-19 08:39:26 -070026#include "dex/method_reference.h"
27#include "dex/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 }
Orion Hodson88591fe2018-03-06 13:35:43 +000073 *out = (*array)[ref.index].load(std::memory_order_relaxed);
Mathieu Chartier9df89312016-11-23 13:28:16 -080074 return true;
75}
76
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070077template <typename DexFileReferenceType, typename Value>
Mathieu Chartier279e3a32018-01-24 18:17:55 -080078inline bool AtomicDexRefMap<DexFileReferenceType, Value>::Remove(const DexFileReferenceType& ref,
79 Value* out) {
80 ElementArray* const array = GetArray(ref.dex_file);
81 if (array == nullptr) {
82 return false;
83 }
Hans Boehm871bf392018-03-28 17:44:09 -070084 *out = (*array)[ref.index].exchange(nullptr, std::memory_order_seq_cst);
Mathieu Chartier279e3a32018-01-24 18:17:55 -080085 return true;
86}
87
88template <typename DexFileReferenceType, typename Value>
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070089inline void AtomicDexRefMap<DexFileReferenceType, Value>::AddDexFile(const DexFile* dex_file) {
90 arrays_.Put(dex_file, std::move(ElementArray(NumberOfDexIndices(dex_file))));
Mathieu Chartier9df89312016-11-23 13:28:16 -080091}
92
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070093template <typename DexFileReferenceType, typename Value>
94inline void AtomicDexRefMap<DexFileReferenceType, Value>::AddDexFiles(
95 const std::vector<const DexFile*>& dex_files) {
Nicolas Geoffray486dda02017-09-11 14:15:52 +010096 for (const DexFile* dex_file : dex_files) {
97 if (!HaveDexFile(dex_file)) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070098 AddDexFile(dex_file);
Nicolas Geoffray486dda02017-09-11 14:15:52 +010099 }
100 }
101}
102
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700103template <typename DexFileReferenceType, typename Value>
104inline typename AtomicDexRefMap<DexFileReferenceType, Value>::ElementArray*
105 AtomicDexRefMap<DexFileReferenceType, Value>::GetArray(const DexFile* dex_file) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800106 auto it = arrays_.find(dex_file);
107 return (it != arrays_.end()) ? &it->second : nullptr;
108}
109
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700110template <typename DexFileReferenceType, typename Value>
111inline const typename AtomicDexRefMap<DexFileReferenceType, Value>::ElementArray*
112 AtomicDexRefMap<DexFileReferenceType, Value>::GetArray(const DexFile* dex_file) const {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800113 auto it = arrays_.find(dex_file);
114 return (it != arrays_.end()) ? &it->second : nullptr;
115}
116
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700117template <typename DexFileReferenceType, typename Value> template <typename Visitor>
118inline void AtomicDexRefMap<DexFileReferenceType, Value>::Visit(const Visitor& visitor) {
Mathieu Chartier9df89312016-11-23 13:28:16 -0800119 for (auto& pair : arrays_) {
120 const DexFile* dex_file = pair.first;
121 const ElementArray& elements = pair.second;
122 for (size_t i = 0; i < elements.size(); ++i) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000123 visitor(DexFileReference(dex_file, i), elements[i].load(std::memory_order_relaxed));
Mathieu Chartier9df89312016-11-23 13:28:16 -0800124 }
125 }
126}
127
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700128template <typename DexFileReferenceType, typename Value>
129inline void AtomicDexRefMap<DexFileReferenceType, Value>::ClearEntries() {
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000130 for (auto& it : arrays_) {
131 for (auto& element : it.second) {
Orion Hodson88591fe2018-03-06 13:35:43 +0000132 element.store(nullptr, std::memory_order_relaxed);
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +0000133 }
134 }
135}
136
Mathieu Chartier9df89312016-11-23 13:28:16 -0800137} // namespace art
138
Mathieu Chartier93764b82017-07-17 14:51:53 -0700139#endif // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_INL_H_