1 /*
2  * Copyright (C) 2013 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_GC_COLLECTOR_SEMI_SPACE_INL_H_
18 #define ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
19 
20 #include "semi_space.h"
21 
22 #include "gc/accounting/heap_bitmap.h"
23 #include "mirror/object-inl.h"
24 
25 namespace art {
26 namespace gc {
27 namespace collector {
28 
GetForwardingAddressInFromSpace(mirror::Object * obj)29 inline mirror::Object* SemiSpace::GetForwardingAddressInFromSpace(mirror::Object* obj) const {
30   DCHECK(from_space_->HasAddress(obj));
31   LockWord lock_word = obj->GetLockWord(false);
32   if (lock_word.GetState() != LockWord::kForwardingAddress) {
33     return nullptr;
34   }
35   return reinterpret_cast<mirror::Object*>(lock_word.ForwardingAddress());
36 }
37 
38 // Used to mark and copy objects. Any newly-marked objects who are in the from space Get moved to
39 // the to-space and have their forward address updated. Objects which have been newly marked are
40 // pushed on the mark stack.
41 template<typename CompressedReferenceType>
MarkObject(CompressedReferenceType * obj_ptr)42 inline void SemiSpace::MarkObject(CompressedReferenceType* obj_ptr) {
43   mirror::Object* obj = obj_ptr->AsMirrorPtr();
44   if (obj == nullptr) {
45     return;
46   }
47   if (from_space_->HasAddress(obj)) {
48     mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
49     // If the object has already been moved, return the new forward address.
50     if (UNLIKELY(forward_address == nullptr)) {
51       forward_address = MarkNonForwardedObject(obj);
52       DCHECK(forward_address != nullptr);
53       // Make sure to only update the forwarding address AFTER you copy the object so that the
54       // monitor word doesn't Get stomped over.
55       obj->SetLockWord(
56           LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)), false);
57       // Push the object onto the mark stack for later processing.
58       MarkStackPush(forward_address);
59     }
60     obj_ptr->Assign(forward_address);
61   } else if (!immune_spaces_.IsInImmuneRegion(obj)) {
62     DCHECK(!to_space_->HasAddress(obj)) << "Tried to mark " << obj << " in to-space";
63     auto slow_path = [this](const mirror::Object* ref) {
64       CHECK(!to_space_->HasAddress(ref)) << "Marking " << ref << " in to_space_";
65       // Marking a large object, make sure its aligned as a sanity check.
66       CHECK_ALIGNED(ref, kPageSize);
67     };
68     if (!mark_bitmap_->Set(obj, slow_path)) {
69       // This object was not previously marked.
70       MarkStackPush(obj);
71     }
72   }
73 }
74 
75 template<typename CompressedReferenceType>
MarkObjectIfNotInToSpace(CompressedReferenceType * obj_ptr)76 inline void SemiSpace::MarkObjectIfNotInToSpace(CompressedReferenceType* obj_ptr) {
77   if (!to_space_->HasAddress(obj_ptr->AsMirrorPtr())) {
78     MarkObject(obj_ptr);
79   }
80 }
81 
82 }  // namespace collector
83 }  // namespace gc
84 }  // namespace art
85 
86 #endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
87