blob: 01e8b044badf67e3e530164f58cfff96923f65a6 [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"
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070021#include "gc/accounting/space_bitmap-inl.h"
Mathieu Chartiera1602f22014-01-13 17:19:19 -080022#include "runtime.h"
23#include "thread-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024
25namespace art {
26namespace gc {
27namespace space {
28
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070029Space::Space(const std::string& name, GcRetentionPolicy gc_retention_policy)
30 : name_(name), gc_retention_policy_(gc_retention_policy) { }
Ian Rogers1d54e732013-05-02 21:10:01 -070031
32void Space::Dump(std::ostream& os) const {
33 os << GetName() << ":" << GetGcRetentionPolicy();
34}
35
36std::ostream& operator<<(std::ostream& os, const Space& space) {
37 space.Dump(os);
38 return os;
39}
40
Ian Rogers6fac4472014-02-25 17:01:10 -080041DlMallocSpace* Space::AsDlMallocSpace() {
42 LOG(FATAL) << "Unreachable";
43 return nullptr;
44}
45
46RosAllocSpace* Space::AsRosAllocSpace() {
47 LOG(FATAL) << "Unreachable";
48 return nullptr;
49}
50
51ZygoteSpace* Space::AsZygoteSpace() {
52 LOG(FATAL) << "Unreachable";
53 return nullptr;
54}
55
56BumpPointerSpace* Space::AsBumpPointerSpace() {
57 LOG(FATAL) << "Unreachable";
58 return nullptr;
59}
60
61AllocSpace* Space::AsAllocSpace() {
62 LOG(FATAL) << "Unimplemented";
63 return nullptr;
64}
65
66ContinuousMemMapAllocSpace* Space::AsContinuousMemMapAllocSpace() {
67 LOG(FATAL) << "Unimplemented";
68 return nullptr;
69}
70
Ian Rogers1d54e732013-05-02 21:10:01 -070071DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
72 GcRetentionPolicy gc_retention_policy) :
73 Space(name, gc_retention_policy),
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080074 live_objects_(new accounting::ObjectSet("large live objects")),
75 mark_objects_(new accounting::ObjectSet("large marked objects")) {
Ian Rogers1d54e732013-05-02 21:10:01 -070076}
77
Mathieu Chartiera1602f22014-01-13 17:19:19 -080078void ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps, size_t* freed_objects, size_t* freed_bytes) {
79 DCHECK(freed_objects != nullptr);
80 DCHECK(freed_bytes != nullptr);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070081 accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap();
82 accounting::ContinuousSpaceBitmap* mark_bitmap = GetMarkBitmap();
Mathieu Chartiera1602f22014-01-13 17:19:19 -080083 // If the bitmaps are bound then sweeping this space clearly won't do anything.
84 if (live_bitmap == mark_bitmap) {
85 return;
86 }
87 SweepCallbackContext scc;
88 scc.swap_bitmaps = swap_bitmaps;
89 scc.heap = Runtime::Current()->GetHeap();
90 scc.self = Thread::Current();
91 scc.space = this;
92 scc.freed_objects = 0;
93 scc.freed_bytes = 0;
94 if (swap_bitmaps) {
95 std::swap(live_bitmap, mark_bitmap);
96 }
97 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070098 accounting::ContinuousSpaceBitmap::SweepWalk(
99 *live_bitmap, *mark_bitmap, reinterpret_cast<uintptr_t>(Begin()),
100 reinterpret_cast<uintptr_t>(End()), GetSweepCallback(), reinterpret_cast<void*>(&scc));
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800101 *freed_objects += scc.freed_objects;
102 *freed_bytes += scc.freed_bytes;
103}
104
105// Returns the old mark bitmap.
106void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() {
107 CHECK(!HasBoundBitmaps());
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700108 accounting::ContinuousSpaceBitmap* live_bitmap = GetLiveBitmap();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800109 if (live_bitmap != mark_bitmap_.get()) {
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700110 accounting::ContinuousSpaceBitmap* mark_bitmap = mark_bitmap_.release();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800111 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
112 temp_bitmap_.reset(mark_bitmap);
113 mark_bitmap_.reset(live_bitmap);
114 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800115}
116
117bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const {
118 return temp_bitmap_.get() != nullptr;
119}
120
121void ContinuousMemMapAllocSpace::UnBindBitmaps() {
122 CHECK(HasBoundBitmaps());
123 // At this point, the temp_bitmap holds our old mark bitmap.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700124 accounting::ContinuousSpaceBitmap* new_bitmap = temp_bitmap_.release();
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800125 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap);
126 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
127 mark_bitmap_.reset(new_bitmap);
128 DCHECK(temp_bitmap_.get() == nullptr);
129}
130
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800131void ContinuousMemMapAllocSpace::SwapBitmaps() {
132 live_bitmap_.swap(mark_bitmap_);
133 // Swap names to get more descriptive diagnostics.
134 std::string temp_name(live_bitmap_->GetName());
135 live_bitmap_->SetName(mark_bitmap_->GetName());
136 mark_bitmap_->SetName(temp_name);
137}
138
Ian Rogers1d54e732013-05-02 21:10:01 -0700139} // namespace space
140} // namespace gc
141} // namespace art