blob: 06d86d643e39d92d787b79679da013028d778408 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright (C) 2008 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070015#include "object_bitmap.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
17#include <sys/mman.h>
18
Elliott Hughes90a33692011-08-30 13:27:07 -070019#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "logging.h"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070021#include "utils.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
23namespace art {
24
Carl Shapiro69759ea2011-07-21 18:13:35 -070025HeapBitmap* HeapBitmap::Create(byte* base, size_t length) {
Elliott Hughes90a33692011-08-30 13:27:07 -070026 UniquePtr<HeapBitmap> bitmap(new HeapBitmap(base, length));
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 if (!bitmap->Init(base, length)) {
28 return NULL;
29 } else {
30 return bitmap.release();
31 }
32}
33
34// Initialize a HeapBitmap so that it points to a bitmap large enough
Brian Carlstrom1f870082011-08-23 16:02:11 -070035// to cover a heap at <base> of <max_size> bytes, where objects are
Carl Shapiro69759ea2011-07-21 18:13:35 -070036// guaranteed to be kAlignment-aligned.
37bool HeapBitmap::Init(const byte* base, size_t max_size) {
38 CHECK(base != NULL);
39 size_t length = HB_OFFSET_TO_INDEX(max_size) * kWordSize;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070040 mem_map_.reset(MemMap::Map(length, PROT_READ | PROT_WRITE));
Elliott Hughes90a33692011-08-30 13:27:07 -070041 if (mem_map_.get() == NULL) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070042 return false;
43 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 words_ = reinterpret_cast<word*>(mem_map_->GetAddress());
Carl Shapiro69759ea2011-07-21 18:13:35 -070045 num_bytes_ = length;
46 base_ = reinterpret_cast<uintptr_t>(base);
47 max_ = base_ - 1;
48 return true;
49}
50
51// Clean up any resources associated with the bitmap.
Brian Carlstromdb4d5402011-08-09 12:18:28 -070052HeapBitmap::~HeapBitmap() {}
Carl Shapiro69759ea2011-07-21 18:13:35 -070053
54// Fill the bitmap with zeroes. Returns the bitmap's memory to the
55// system as a side-effect.
56void HeapBitmap::Clear() {
57 if (words_ != NULL) {
58 // This returns the memory to the system. Successive page faults
59 // will return zeroed memory.
60 int result = madvise(words_, num_bytes_, MADV_DONTNEED);
61 if (result == -1) {
62 PLOG(WARNING) << "madvise failed";
63 }
64 max_ = base_ - 1;
65 }
66}
67
68// Return true iff <obj> is within the range of pointers that this
69// bitmap could potentially cover, even if a bit has not been set for
70// it.
71bool HeapBitmap::HasAddress(const void* obj) const {
72 if (obj != NULL) {
73 const uintptr_t offset = (uintptr_t)obj - base_;
74 const size_t index = HB_OFFSET_TO_INDEX(offset);
75 return index < num_bytes_ / kWordSize;
76 }
77 return false;
78}
79
80// Visits set bits in address order. The callback is not permitted to
81// change the bitmap bits or max during the traversal.
82void HeapBitmap::Walk(HeapBitmap::Callback* callback, void* arg) {
83 CHECK(words_ != NULL);
84 CHECK(callback != NULL);
85 uintptr_t end = HB_OFFSET_TO_INDEX(max_ - base_);
86 for (uintptr_t i = 0; i <= end; ++i) {
87 unsigned long word = words_[i];
88 if (word != 0) {
89 unsigned long high_bit = 1 << (kBitsPerWord - 1);
90 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + base_;
91 while (word != 0) {
92 const int shift = CLZ(word);
Brian Carlstrom4873d462011-08-21 15:23:39 -070093 Object* obj = (Object*) (ptr_base + shift * kAlignment);
Carl Shapiro69759ea2011-07-21 18:13:35 -070094 (*callback)(obj, arg);
95 word &= ~(high_bit >> shift);
96 }
97 }
98 }
99}
100
101// Similar to Walk but the callback routine is permitted to change the
102// bitmap bits and max during traversal. Used by the the root marking
103// scan exclusively.
104//
105// The callback is invoked with a finger argument. The finger is a
106// pointer to an address not yet visited by the traversal. If the
107// callback sets a bit for an address at or above the finger, this
108// address will be visited by the traversal. If the callback sets a
109// bit for an address below the finger, this address will not be
110// visited.
Brian Carlstrom1f870082011-08-23 16:02:11 -0700111void HeapBitmap::ScanWalk(uintptr_t base, ScanCallback* callback, void* arg) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700112 CHECK(words_ != NULL);
113 CHECK(callback != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700114 CHECK(base >= base_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700115 uintptr_t end = HB_OFFSET_TO_INDEX(max_ - base);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700116 for (uintptr_t i = 0; i <= end; ++i) {
117 unsigned long word = words_[i];
118 if (word != 0) {
119 unsigned long high_bit = 1 << (kBitsPerWord - 1);
120 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + base_;
121 void* finger = (void*)(HB_INDEX_TO_OFFSET(i + 1) + base_);
122 while (word != 0) {
123 const int shift = CLZ(word);
124 Object* obj = (Object*)(ptr_base + shift * kAlignment);
125 (*callback)(obj, finger, arg);
126 word &= ~(high_bit >> shift);
127 }
128 end = HB_OFFSET_TO_INDEX(max_ - base_);
129 }
130 }
131}
132
133// Walk through the bitmaps in increasing address order, and find the
134// object pointers that correspond to garbage objects. Call
135// <callback> zero or more times with lists of these object pointers.
136//
137// The callback is not permitted to increase the max of either bitmap.
138void HeapBitmap::SweepWalk(const HeapBitmap& live_bitmap,
139 const HeapBitmap& mark_bitmap,
140 uintptr_t base, uintptr_t max,
141 HeapBitmap::SweepCallback* callback, void* arg) {
142 CHECK(live_bitmap.words_ != NULL);
143 CHECK(mark_bitmap.words_ != NULL);
144 CHECK(live_bitmap.base_ == mark_bitmap.base_);
145 CHECK(live_bitmap.num_bytes_ == mark_bitmap.num_bytes_);
146 CHECK(callback != NULL);
147 CHECK(base <= max);
148 CHECK(base >= live_bitmap.base_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700149 max = std::min(max-1, live_bitmap.max_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150 if (live_bitmap.max_ < live_bitmap.base_) {
151 // Easy case; both are obviously empty.
152 // TODO: this should never happen
153 return;
154 }
155 void* pointer_buf[4 * kBitsPerWord];
156 void** pb = pointer_buf;
157 size_t start = HB_OFFSET_TO_INDEX(base - live_bitmap.base_);
158 size_t end = HB_OFFSET_TO_INDEX(max - live_bitmap.base_);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700159 word* live = live_bitmap.words_;
160 word* mark = mark_bitmap.words_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 for (size_t i = start; i <= end; i++) {
162 unsigned long garbage = live[i] & ~mark[i];
163 if (garbage != 0) {
164 unsigned long high_bit = 1 << (kBitsPerWord - 1);
165 uintptr_t ptr_base = HB_INDEX_TO_OFFSET(i) + live_bitmap.base_;
166 while (garbage != 0) {
167 int shift = CLZ(garbage);
168 garbage &= ~(high_bit >> shift);
169 *pb++ = (void*)(ptr_base + shift * kAlignment);
170 }
171 // Make sure that there are always enough slots available for an
172 // entire word of one bits.
173 if (pb >= &pointer_buf[ARRAYSIZE_UNSAFE(pointer_buf) - kBitsPerWord]) {
174 (*callback)(pb - pointer_buf, pointer_buf, arg);
175 pb = pointer_buf;
176 }
177 }
178 }
179 if (pb > pointer_buf) {
180 (*callback)(pb - pointer_buf, pointer_buf, arg);
181 }
182}
183
184} // namespace art