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 
29 class BitmapSetSlowPathVisitor {
30  public:
BitmapSetSlowPathVisitor(SemiSpace * semi_space)31   explicit BitmapSetSlowPathVisitor(SemiSpace* semi_space) : semi_space_(semi_space) {
32   }
33 
operator()34   void operator()(const mirror::Object* obj) const {
35     CHECK(!semi_space_->to_space_->HasAddress(obj)) << "Marking " << obj << " in to_space_";
36     // Marking a large object, make sure its aligned as a sanity check.
37     CHECK(IsAligned<kPageSize>(obj));
38   }
39 
40  private:
41   SemiSpace* const semi_space_;
42 };
43 
GetForwardingAddressInFromSpace(mirror::Object * obj)44 inline mirror::Object* SemiSpace::GetForwardingAddressInFromSpace(mirror::Object* obj) const {
45   DCHECK(from_space_->HasAddress(obj));
46   LockWord lock_word = obj->GetLockWord(false);
47   if (lock_word.GetState() != LockWord::kForwardingAddress) {
48     return nullptr;
49   }
50   return reinterpret_cast<mirror::Object*>(lock_word.ForwardingAddress());
51 }
52 
53 // Used to mark and copy objects. Any newly-marked objects who are in the from space Get moved to
54 // the to-space and have their forward address updated. Objects which have been newly marked are
55 // pushed on the mark stack.
56 template<bool kPoisonReferences>
MarkObject(mirror::ObjectReference<kPoisonReferences,mirror::Object> * obj_ptr)57 inline void SemiSpace::MarkObject(
58     mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr) {
59   mirror::Object* obj = obj_ptr->AsMirrorPtr();
60   if (obj == nullptr) {
61     return;
62   }
63   if (kUseBakerOrBrooksReadBarrier) {
64     // Verify all the objects have the correct forward pointer installed.
65     obj->AssertReadBarrierPointer();
66   }
67   if (from_space_->HasAddress(obj)) {
68     mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
69     // If the object has already been moved, return the new forward address.
70     if (UNLIKELY(forward_address == nullptr)) {
71       forward_address = MarkNonForwardedObject(obj);
72       DCHECK(forward_address != nullptr);
73       // Make sure to only update the forwarding address AFTER you copy the object so that the
74       // monitor word doesn't Get stomped over.
75       obj->SetLockWord(
76           LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)), false);
77       // Push the object onto the mark stack for later processing.
78       MarkStackPush(forward_address);
79     }
80     obj_ptr->Assign(forward_address);
81   } else if (!collect_from_space_only_ && !immune_region_.ContainsObject(obj)) {
82     BitmapSetSlowPathVisitor visitor(this);
83     if (!mark_bitmap_->Set(obj, visitor)) {
84       // This object was not previously marked.
85       MarkStackPush(obj);
86     }
87   }
88 }
89 
90 }  // namespace collector
91 }  // namespace gc
92 }  // namespace art
93 
94 #endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
95