blob: 5478d5b1b49779c4ffa8bb0ba58c881eeb693284 [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();
80 accounting::SpaceBitmap* mark_bitmap = mark_bitmap_.release();
81 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap, live_bitmap);
82 temp_bitmap_.reset(mark_bitmap);
83 mark_bitmap_.reset(live_bitmap);
84}
85
86bool ContinuousMemMapAllocSpace::HasBoundBitmaps() const {
87 return temp_bitmap_.get() != nullptr;
88}
89
90void ContinuousMemMapAllocSpace::UnBindBitmaps() {
91 CHECK(HasBoundBitmaps());
92 // At this point, the temp_bitmap holds our old mark bitmap.
93 accounting::SpaceBitmap* new_bitmap = temp_bitmap_.release();
94 Runtime::Current()->GetHeap()->GetMarkBitmap()->ReplaceBitmap(mark_bitmap_.get(), new_bitmap);
95 CHECK_EQ(mark_bitmap_.release(), live_bitmap_.get());
96 mark_bitmap_.reset(new_bitmap);
97 DCHECK(temp_bitmap_.get() == nullptr);
98}
99
Ian Rogers1d54e732013-05-02 21:10:01 -0700100} // namespace space
101} // namespace gc
102} // namespace art