blob: 32a00bc4a1b0c0eb9c7836f7af05bb574dc904b6 [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 Rogers1d54e732013-05-02 21:10:01 -070040DiscontinuousSpace::DiscontinuousSpace(const std::string& name,
41 GcRetentionPolicy gc_retention_policy) :
42 Space(name, gc_retention_policy),
Mathieu Chartierdb7f37d2014-01-10 11:09:06 -080043 live_objects_(new accounting::ObjectSet("large live objects")),
44 mark_objects_(new accounting::ObjectSet("large marked objects")) {
Ian Rogers1d54e732013-05-02 21:10:01 -070045}
46
Mathieu Chartiera1602f22014-01-13 17:19:19 -080047void ContinuousMemMapAllocSpace::Sweep(bool swap_bitmaps, size_t* freed_objects, size_t* freed_bytes) {
48 DCHECK(freed_objects != nullptr);
49 DCHECK(freed_bytes != nullptr);
50 accounting::SpaceBitmap* live_bitmap = GetLiveBitmap();
51 accounting::SpaceBitmap* mark_bitmap = GetMarkBitmap();
52 // If the bitmaps are bound then sweeping this space clearly won't do anything.
53 if (live_bitmap == mark_bitmap) {
54 return;
55 }
56 SweepCallbackContext scc;
57 scc.swap_bitmaps = swap_bitmaps;
58 scc.heap = Runtime::Current()->GetHeap();
59 scc.self = Thread::Current();
60 scc.space = this;
61 scc.freed_objects = 0;
62 scc.freed_bytes = 0;
63 if (swap_bitmaps) {
64 std::swap(live_bitmap, mark_bitmap);
65 }
66 // Bitmaps are pre-swapped for optimization which enables sweeping with the heap unlocked.
67 accounting::SpaceBitmap::SweepWalk(*live_bitmap, *mark_bitmap,
68 reinterpret_cast<uintptr_t>(Begin()),
69 reinterpret_cast<uintptr_t>(End()),
70 GetSweepCallback(),
71 reinterpret_cast<void*>(&scc));
72 *freed_objects += scc.freed_objects;
73 *freed_bytes += scc.freed_bytes;
74}
75
76// Returns the old mark bitmap.
77void ContinuousMemMapAllocSpace::BindLiveToMarkBitmap() {
78 CHECK(!HasBoundBitmaps());
79 accounting::SpaceBitmap* live_bitmap = GetLiveBitmap();
Mathieu Chartier1f3b5352014-02-03 14:00:42 -080080 if (live_bitmap != mark_bitmap_.get()) {
81 accounting::SpaceBitmap* mark_bitmap = mark_bitmap_.release();
82 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
83 temp_bitmap_.reset(mark_bitmap);
84 mark_bitmap_.reset(live_bitmap);
85 }
Mathieu Chartiera1602f22014-01-13 17:19:19 -080086}
87
88bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const {
89 return temp_bitmap_.get() != nullptr;
90}
91
92void ContinuousMemMapAllocSpace::UnBindBitmaps() {
93 CHECK(HasBoundBitmaps());
94 // At this point, the temp_bitmap holds our old mark bitmap.
95 accounting::SpaceBitmap* new_bitmap = temp_bitmap_.release();
96 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap);
97 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
98 mark_bitmap_.reset(new_bitmap);
99 DCHECK(temp_bitmap_.get() == nullptr);
100}
101
Mathieu Chartier1f3b5352014-02-03 14:00:42 -0800102void ContinuousMemMapAllocSpace::SwapBitmaps() {
103 live_bitmap_.swap(mark_bitmap_);
104 // Swap names to get more descriptive diagnostics.
105 std::string temp_name(live_bitmap_->GetName());
106 live_bitmap_->SetName(mark_bitmap_->GetName());
107 mark_bitmap_->SetName(temp_name);
108}
109
Ian Rogers1d54e732013-05-02 21:10:01 -0700110} // namespace space
111} // namespace gc
112} // namespace art