Home | History | Annotate | Download | only in utils
      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_DEX_REF_MAP_INL_H_
     18 #define ART_COMPILER_UTILS_ATOMIC_DEX_REF_MAP_INL_H_
     19 
     20 #include "atomic_dex_ref_map.h"
     21 
     22 #include "dex_file-inl.h"
     23 
     24 namespace art {
     25 
     26 template <typename T>
     27 inline typename AtomicDexRefMap<T>::InsertResult AtomicDexRefMap<T>::Insert(
     28     DexFileReference 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   DCHECK_LT(ref.index, array->size());
     36   return (*array)[ref.index].CompareExchangeStrongSequentiallyConsistent(expected, desired)
     37       ? kInsertResultSuccess
     38       : kInsertResultCASFailure;
     39 }
     40 
     41 template <typename T>
     42 inline bool AtomicDexRefMap<T>::Get(DexFileReference 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.index].LoadRelaxed();
     48   return true;
     49 }
     50 
     51 template <typename T>
     52 inline void AtomicDexRefMap<T>::AddDexFile(const DexFile* dex_file, size_t max_index) {
     53   arrays_.Put(dex_file, std::move(ElementArray(max_index)));
     54 }
     55 
     56 template <typename T>
     57 inline typename AtomicDexRefMap<T>::ElementArray* AtomicDexRefMap<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>
     64 inline const typename AtomicDexRefMap<T>::ElementArray* AtomicDexRefMap<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>
     71 inline void AtomicDexRefMap<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(DexFileReference(dex_file, i), elements[i].LoadRelaxed());
     77     }
     78   }
     79 }
     80 
     81 template <typename T>
     82 inline void AtomicDexRefMap<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_DEX_REF_MAP_INL_H_
     93