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 (from_space_->HasAddress(obj)) {
64     mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
65     // If the object has already been moved, return the new forward address.
66     if (UNLIKELY(forward_address == nullptr)) {
67       forward_address = MarkNonForwardedObject(obj);
68       DCHECK(forward_address != nullptr);
69       // Make sure to only update the forwarding address AFTER you copy the object so that the
70       // monitor word doesn't Get stomped over.
71       obj->SetLockWord(
72           LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)), false);
73       // Push the object onto the mark stack for later processing.
74       MarkStackPush(forward_address);
75     }
76     obj_ptr->Assign(forward_address);
77   } else if (!collect_from_space_only_ && !immune_region_.ContainsObject(obj)) {
78     BitmapSetSlowPathVisitor visitor(this);
79     if (!mark_bitmap_->Set(obj, visitor)) {
80       // This object was not previously marked.
81       MarkStackPush(obj);
82     }
83   }
84 }
85 
86 }  // namespace collector
87 }  // namespace gc
88 }  // namespace art
89 
90 #endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
91