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
25namespace art {
26namespace gc {
27namespace collector {
28
29class BitmapSetSlowPathVisitor {
30 public:
31  explicit BitmapSetSlowPathVisitor(SemiSpace* semi_space) : semi_space_(semi_space) {
32  }
33
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_ALIGNED(obj, kPageSize);
38  }
39
40 private:
41  SemiSpace* const semi_space_;
42};
43
44inline 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.
56template<bool kPoisonReferences>
57inline 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_spaces_.IsInImmuneRegion(obj)) {
78    DCHECK(!to_space_->HasAddress(obj)) << "Tried to mark " << obj << " in to-space";
79    BitmapSetSlowPathVisitor visitor(this);
80    if (!mark_bitmap_->Set(obj, visitor)) {
81      // This object was not previously marked.
82      MarkStackPush(obj);
83    }
84  }
85}
86
87template<bool kPoisonReferences>
88inline void SemiSpace::MarkObjectIfNotInToSpace(
89    mirror::ObjectReference<kPoisonReferences, mirror::Object>* obj_ptr) {
90  if (!to_space_->HasAddress(obj_ptr->AsMirrorPtr())) {
91    MarkObject(obj_ptr);
92  }
93}
94
95}  // namespace collector
96}  // namespace gc
97}  // namespace art
98
99#endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
100