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<bool kPoisonReferences>
MarkObject(mirror::ObjectReference<kPoisonReferences,mirror::Object> * obj_ptr)42 inline void SemiSpace::MarkObject(
43 mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr) {
44 mirror::Object* obj = obj_ptr->AsMirrorPtr();
45 if (obj == nullptr) {
46 return;
47 }
48 if (from_space_->HasAddress(obj)) {
49 mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
50 // If the object has already been moved, return the new forward address.
51 if (UNLIKELY(forward_address == nullptr)) {
52 forward_address = MarkNonForwardedObject(obj);
53 DCHECK(forward_address != nullptr);
54 // Make sure to only update the forwarding address AFTER you copy the object so that the
55 // monitor word doesn't Get stomped over.
56 obj->SetLockWord(
57 LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)), false);
58 // Push the object onto the mark stack for later processing.
59 MarkStackPush(forward_address);
60 }
61 obj_ptr->Assign(forward_address);
62 } else if (!collect_from_space_only_ && !immune_spaces_.IsInImmuneRegion(obj)) {
63 DCHECK(!to_space_->HasAddress(obj)) << "Tried to mark " << obj << " in to-space";
64 auto slow_path = [this](const mirror::Object* ref) {
65 CHECK(!to_space_->HasAddress(ref)) << "Marking " << ref << " in to_space_";
66 // Marking a large object, make sure its aligned as a sanity check.
67 CHECK_ALIGNED(ref, kPageSize);
68 };
69 if (!mark_bitmap_->Set(obj, slow_path)) {
70 // This object was not previously marked.
71 MarkStackPush(obj);
72 }
73 }
74 }
75
76 template<bool kPoisonReferences>
MarkObjectIfNotInToSpace(mirror::ObjectReference<kPoisonReferences,mirror::Object> * obj_ptr)77 inline void SemiSpace::MarkObjectIfNotInToSpace(
78 mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr) {
79 if (!to_space_->HasAddress(obj_ptr->AsMirrorPtr())) {
80 MarkObject(obj_ptr);
81 }
82 }
83
84 } // namespace collector
85 } // namespace gc
86 } // namespace art
87
88 #endif // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
89