blob: 4af65a9bf1294d647a131223408aec6e25427e0d [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 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#include "space.h"
18
19#include "base/logging.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080020#include "gc/accounting/heap_bitmap.h"
21#include "runtime.h"
22#include "thread-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070023
24namespace art {
25namespace gc {
26namespace space {
27
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070028Space::Space(const std::string& name, GcRetentionPolicy gc_retention_policy)
29 : name_(name), gc_retention_policy_(gc_retention_policy) { }
Ian Rogers1d54e732013-05-02 21:10:01 -070030
31void Space::Dump(std::ostream& os) const {
32 os << GetName() << ":" << GetGcRetentionPolicy();
33}
34
35std::ostream& operator<<(std::ostream& os, const Space& space) {
36 space.Dump(os);
37 return os;
38}
39
Ian Rogers6fac4472014-02-25 17:01:10 -080040DlMallocSpace* Space::AsDlMallocSpace() {
41 LOG(FATAL) << "Unreachable";
42 return nullptr;
43}
44
45RosAllocSpace* Space::AsRosAllocSpace() {
46 LOG(FATAL) << "Unreachable";
47 return nullptr;
48}
49
50ZygoteSpace* Space::AsZygoteSpace() {
51 LOG(FATAL) << "Unreachable";
52 return nullptr;
53}
54
55BumpPointerSpace* Space::AsBumpPointerSpace() {
56 LOG(FATAL) << "Unreachable";
57 return nullptr;
58}
59
60AllocSpace* Space::AsAllocSpace() {
61 LOG(FATAL) << "Unimplemented";
62 return nullptr;
63}
64
65ContinuousMemMapAllocSpace* Space::AsContinuousMemMapAllocSpace() {
66 LOG(FATAL) << "Unimplemented";
67 return nullptr;
68}
69
Ian Rogers1d54e732013-05-02 21:10:01 -070070DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
71 GcRetentionPolicy gc_retention_policy) :
72 Space(name, gc_retention_policy),
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080073 live_objects_(new accounting::ObjectSet("large live objects")),
74 mark_objects_(new accounting::ObjectSet("large marked objects")) {
Ian Rogers1d54e732013-05-02 21:10:01 -070075}
76
Mathieu Chartiera1602f22014-01-13 17:19:19 -080077void ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps, size_t* freed_objects, size_t* freed_bytes) {
78 DCHECK(freed_objects != nullptr);
79 DCHECK(freed_bytes != nullptr);
80 accounting::SpaceBitmap* live_bitmap = GetLiveBitmap();
81 accounting::SpaceBitmap* mark_bitmap = GetMarkBitmap();
82 // If the bitmaps are bound then sweeping this space clearly won't do anything.
83 if (live_bitmap == mark_bitmap) {
84 return;
85 }
86 SweepCallbackContext scc;
87 scc.swap_bitmaps = swap_bitmaps;
88 scc.heap = Runtime::Current()->GetHeap();
89 scc.self = Thread::Current();
90 scc.space = this;
91 scc.freed_objects = 0;
92 scc.freed_bytes = 0;
93 if (swap_bitmaps) {
94 std::swap(live_bitmap, mark_bitmap);
95 }
96 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
97 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
98 reinterpret_cast<uintptr_t>(Begin()),
99 reinterpret_cast<uintptr_t>(End()),
100 GetSweepCallback(),
101 reinterpret_cast<void*>(&scc));
102 *freed_objects += scc.freed_objects;
103 *freed_bytes += scc.freed_bytes;
104}
105
106// Returns the old mark bitmap.
107void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() {
108 CHECK(!HasBoundBitmaps());
109 accounting::SpaceBitmap* live_bitmap = GetLiveBitmap();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800110 if (live_bitmap != mark_bitmap_.get()) {
111 accounting::SpaceBitmap* mark_bitmap = mark_bitmap_.release();
112 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
113 temp_bitmap_.reset(mark_bitmap);
114 mark_bitmap_.reset(live_bitmap);
115 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800116}
117
118bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const {
119 return temp_bitmap_.get() != nullptr;
120}
121
122void ContinuousMemMapAllocSpace::UnBindBitmaps() {
123 CHECK(HasBoundBitmaps());
124 // At this point, the temp_bitmap holds our old mark bitmap.
125 accounting::SpaceBitmap* new_bitmap = temp_bitmap_.release();
126 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap);
127 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
128 mark_bitmap_.reset(new_bitmap);
129 DCHECK(temp_bitmap_.get() == nullptr);
130}
131
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800132void ContinuousMemMapAllocSpace::SwapBitmaps() {
133 live_bitmap_.swap(mark_bitmap_);
134 // Swap names to get more descriptive diagnostics.
135 std::string temp_name(live_bitmap_->GetName());
136 live_bitmap_->SetName(mark_bitmap_->GetName());
137 mark_bitmap_->SetName(temp_name);
138}
139
Ian Rogers1d54e732013-05-02 21:10:01 -0700140} // namespace space
141} // namespace gc
142} // namespace art