1 /*
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_INL_H_
18 #define ART_COMPILER_UTILS_ATOMIC_METHOD_REF_MAP_INL_H_
19 
20 #include "atomic_method_ref_map.h"
21 
22 #include "dex_file-inl.h"
23 
24 namespace art {
25 
26 template <typename T>
Insert(MethodReference ref,const T & expected,const T & desired)27 inline typename AtomicMethodRefMap<T>::InsertResult AtomicMethodRefMap<T>::Insert(
28     MethodReference ref,
29     const T& expected,
30     const T& desired) {
31   ElementArray* const array = GetArray(ref.dex_file);
32   if (array == nullptr) {
33     return kInsertResultInvalidDexFile;
34   }
35   return (*array)[ref.dex_method_index].CompareExchangeStrongSequentiallyConsistent(
36       expected, desired)
37       ? kInsertResultSuccess
38       : kInsertResultCASFailure;
39 }
40 
41 template <typename T>
Get(MethodReference ref,T * out)42 inline bool AtomicMethodRefMap<T>::Get(MethodReference ref, T* out) const {
43   const ElementArray* const array = GetArray(ref.dex_file);
44   if (array == nullptr) {
45     return false;
46   }
47   *out = (*array)[ref.dex_method_index].LoadRelaxed();
48   return true;
49 }
50 
51 template <typename T>
AddDexFile(const DexFile * dex_file)52 inline void AtomicMethodRefMap<T>::AddDexFile(const DexFile* dex_file) {
53   arrays_.Put(dex_file, std::move(ElementArray(dex_file->NumMethodIds())));
54 }
55 
56 template <typename T>
GetArray(const DexFile * dex_file)57 inline typename AtomicMethodRefMap<T>::ElementArray* AtomicMethodRefMap<T>::GetArray(
58     const DexFile* dex_file) {
59   auto it = arrays_.find(dex_file);
60   return (it != arrays_.end()) ? &it->second : nullptr;
61 }
62 
63 template <typename T>
GetArray(const DexFile * dex_file)64 inline const typename AtomicMethodRefMap<T>::ElementArray* AtomicMethodRefMap<T>::GetArray(
65     const DexFile* dex_file) const {
66   auto it = arrays_.find(dex_file);
67   return (it != arrays_.end()) ? &it->second : nullptr;
68 }
69 
70 template <typename T> template <typename Visitor>
Visit(const Visitor & visitor)71 inline void AtomicMethodRefMap<T>::Visit(const Visitor& visitor) {
72   for (auto& pair : arrays_) {
73     const DexFile* dex_file = pair.first;
74     const ElementArray& elements = pair.second;
75     for (size_t i = 0; i < elements.size(); ++i) {
76       visitor(MethodReference(dex_file, i), elements[i].LoadRelaxed());
77     }
78   }
79 }
80 
81 template <typename T>
ClearEntries()82 inline void AtomicMethodRefMap<T>::ClearEntries() {
83   for (auto& it : arrays_) {
84     for (auto& element : it.second) {
85       element.StoreRelaxed(nullptr);
86     }
87   }
88 }
89 
90 }  // namespace art
91 
92 #endif  // ART_COMPILER_UTILS_ATOMIC_METHOD_REF_MAP_INL_H_
93