semi_space-inl.h revision eb8167a4f4d27fce0530f6724ab8032610cd146b
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(IsAligned<kPageSize>(obj));
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 (kUseBakerOrBrooksReadBarrier) {
64    // Verify all the objects have the correct forward pointer installed.
65    obj->AssertReadBarrierPointer();
66  }
67  if (!immune_region_.ContainsObject(obj)) {
68    if (from_space_->HasAddress(obj)) {
69      mirror::Object* forward_address = GetForwardingAddressInFromSpace(obj);
70      // If the object has already been moved, return the new forward address.
71      if (UNLIKELY(forward_address == nullptr)) {
72        forward_address = MarkNonForwardedObject(obj);
73        DCHECK(forward_address != nullptr);
74        // Make sure to only update the forwarding address AFTER you copy the object so that the
75        // monitor word doesn't Get stomped over.
76        obj->SetLockWord(
77            LockWord::FromForwardingAddress(reinterpret_cast<size_t>(forward_address)), false);
78        // Push the object onto the mark stack for later processing.
79        MarkStackPush(forward_address);
80      }
81      obj_ptr->Assign(forward_address);
82    } else {
83      BitmapSetSlowPathVisitor visitor(this);
84      if (kIsDebugBuild && mark_bitmap_->GetContinuousSpaceBitmap(obj) != nullptr) {
85        // If a bump pointer space only collection, we should not
86        // reach here as we don't/won't mark the objects in the
87        // non-moving space (except for the promoted objects.)  Note
88        // the non-moving space is added to the immune space.
89        DCHECK(!generational_ || whole_heap_collection_);
90      }
91      if (!mark_bitmap_->Set(obj, visitor)) {
92        // This object was not previously marked.
93        MarkStackPush(obj);
94      }
95    }
96  }
97}
98
99}  // namespace collector
100}  // namespace gc
101}  // namespace art
102
103#endif  // ART_RUNTIME_GC_COLLECTOR_SEMI_SPACE_INL_H_
104