blob: 14f1f0b98123a98703e4a3dc1fc7fcbbd71b42cc [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
22#include "dex_file-inl.h"
23
24namespace art {
25
26template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070027inline typename AtomicDexRefMap<T>::InsertResult AtomicDexRefMap<T>::Insert(
28 DexFileReference ref,
Mathieu Chartier9df89312016-11-23 13:28:16 -080029 const T& expected,
30 const T& desired) {
31 ElementArray* const array = GetArray(ref.dex_file);
32 if (array == nullptr) {
33 return kInsertResultInvalidDexFile;
34 }
Mathieu Chartier93764b82017-07-17 14:51:53 -070035 DCHECK_LT(ref.index, array->size());
36 return (*array)[ref.index].CompareExchangeStrongSequentiallyConsistent(expected, desired)
Mathieu Chartier9df89312016-11-23 13:28:16 -080037 ? kInsertResultSuccess
38 : kInsertResultCASFailure;
39}
40
41template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070042inline bool AtomicDexRefMap<T>::Get(DexFileReference ref, T* out) const {
Mathieu Chartier9df89312016-11-23 13:28:16 -080043 const ElementArray* const array = GetArray(ref.dex_file);
44 if (array == nullptr) {
Calin Juravle49ebbb22017-03-27 18:10:47 -070045 return false;
Mathieu Chartier9df89312016-11-23 13:28:16 -080046 }
Mathieu Chartier93764b82017-07-17 14:51:53 -070047 *out = (*array)[ref.index].LoadRelaxed();
Mathieu Chartier9df89312016-11-23 13:28:16 -080048 return true;
49}
50
51template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070052inline void AtomicDexRefMap<T>::AddDexFile(const DexFile* dex_file, size_t max_index) {
53 arrays_.Put(dex_file, std::move(ElementArray(max_index)));
Mathieu Chartier9df89312016-11-23 13:28:16 -080054}
55
56template <typename T>
Nicolas Geoffray486dda02017-09-11 14:15:52 +010057inline void AtomicDexRefMap<T>::AddDexFiles(const std::vector<const DexFile*>& dex_files) {
58 for (const DexFile* dex_file : dex_files) {
59 if (!HaveDexFile(dex_file)) {
60 AddDexFile(dex_file, dex_file->NumClassDefs());
61 }
62 }
63}
64
65template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070066inline typename AtomicDexRefMap<T>::ElementArray* AtomicDexRefMap<T>::GetArray(
Mathieu Chartier9df89312016-11-23 13:28:16 -080067 const DexFile* dex_file) {
68 auto it = arrays_.find(dex_file);
69 return (it != arrays_.end()) ? &it->second : nullptr;
70}
71
72template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070073inline const typename AtomicDexRefMap<T>::ElementArray* AtomicDexRefMap<T>::GetArray(
Mathieu Chartier9df89312016-11-23 13:28:16 -080074 const DexFile* dex_file) const {
75 auto it = arrays_.find(dex_file);
76 return (it != arrays_.end()) ? &it->second : nullptr;
77}
78
79template <typename T> template <typename Visitor>
Mathieu Chartier93764b82017-07-17 14:51:53 -070080inline void AtomicDexRefMap<T>::Visit(const Visitor& visitor) {
Mathieu Chartier9df89312016-11-23 13:28:16 -080081 for (auto& pair : arrays_) {
82 const DexFile* dex_file = pair.first;
83 const ElementArray& elements = pair.second;
84 for (size_t i = 0; i < elements.size(); ++i) {
Mathieu Chartier93764b82017-07-17 14:51:53 -070085 visitor(DexFileReference(dex_file, i), elements[i].LoadRelaxed());
Mathieu Chartier9df89312016-11-23 13:28:16 -080086 }
87 }
88}
89
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000090template <typename T>
Mathieu Chartier93764b82017-07-17 14:51:53 -070091inline void AtomicDexRefMap<T>::ClearEntries() {
Nicolas Geoffray1d0ae3f2016-12-06 13:40:16 +000092 for (auto& it : arrays_) {
93 for (auto& element : it.second) {
94 element.StoreRelaxed(nullptr);
95 }
96 }
97}
98
Mathieu Chartier9df89312016-11-23 13:28:16 -080099} // namespace art
100
Mathieu Chartier93764b82017-07-17 14:51:53 -0700101#endif // ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_INL_H_