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_RUNTIME_IMAGE_INL_H_
18 #define ART_RUNTIME_IMAGE_INL_H_
19 
20 #include "image.h"
21 
22 #include "art_method.h"
23 #include "imt_conflict_table.h"
24 #include "imtable.h"
25 #include "read_barrier-inl.h"
26 
27 namespace art {
28 
29 template <ReadBarrierOption kReadBarrierOption>
GetImageRoot(ImageRoot image_root)30 inline mirror::Object* ImageHeader::GetImageRoot(ImageRoot image_root) const {
31   mirror::ObjectArray<mirror::Object>* image_roots = GetImageRoots<kReadBarrierOption>();
32   return image_roots->Get<kVerifyNone, kReadBarrierOption>(static_cast<int32_t>(image_root));
33 }
34 
35 template <ReadBarrierOption kReadBarrierOption>
GetImageRoots()36 inline mirror::ObjectArray<mirror::Object>* ImageHeader::GetImageRoots() const {
37   // Need a read barrier as it's not visited during root scan.
38   // Pass in the address of the local variable to the read barrier
39   // rather than image_roots_ because it won't move (asserted below)
40   // and it's a const member.
41   mirror::ObjectArray<mirror::Object>* image_roots =
42       reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(image_roots_);
43   mirror::ObjectArray<mirror::Object>* result =
44       ReadBarrier::BarrierForRoot<mirror::ObjectArray<mirror::Object>, kReadBarrierOption>(
45           &image_roots);
46   DCHECK_EQ(image_roots, result);
47   return image_roots;
48 }
49 
50 template <typename Visitor>
VisitPackedImTables(const Visitor & visitor,uint8_t * base,PointerSize pointer_size)51 inline void ImageHeader::VisitPackedImTables(const Visitor& visitor,
52                                              uint8_t* base,
53                                              PointerSize pointer_size) const {
54   const ImageSection& section = GetImageSection(kSectionImTables);
55   for (size_t pos = 0; pos < section.Size();) {
56     ImTable* imt = reinterpret_cast<ImTable*>(base + section.Offset() + pos);
57     for (size_t i = 0; i < ImTable::kSize; ++i) {
58       ArtMethod* orig = imt->Get(i, pointer_size);
59       ArtMethod* updated = visitor(orig);
60       if (updated != orig) {
61         imt->Set(i, updated, pointer_size);
62       }
63     }
64     pos += ImTable::SizeInBytes(pointer_size);
65   }
66 }
67 
68 template <typename Visitor>
VisitPackedImtConflictTables(const Visitor & visitor,uint8_t * base,PointerSize pointer_size)69 inline void ImageHeader::VisitPackedImtConflictTables(const Visitor& visitor,
70                                                       uint8_t* base,
71                                                       PointerSize pointer_size) const {
72   const ImageSection& section = GetImageSection(kSectionIMTConflictTables);
73   for (size_t pos = 0; pos < section.Size(); ) {
74     auto* table = reinterpret_cast<ImtConflictTable*>(base + section.Offset() + pos);
75     table->Visit([&visitor](const std::pair<ArtMethod*, ArtMethod*>& methods) {
76       return std::make_pair(visitor(methods.first), visitor(methods.second));
77     }, pointer_size);
78     pos += table->ComputeSize(pointer_size);
79   }
80 }
81 
82 }  // namespace art
83 
84 #endif  // ART_RUNTIME_IMAGE_INL_H_
85