blob: db1a5eb326d578e33cedc19f45e1c22cf0cfe3ed [file] [log] [blame]
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001/*
2 * Copyright (C) 2008 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#ifndef ART_SRC_SPACE_BITMAP_H_
18#define ART_SRC_SPACE_BITMAP_H_
19
20#include <limits.h>
21#include <stdint.h>
22#include <vector>
23
24#include "UniquePtr.h"
25#include "globals.h"
26#include "logging.h"
27#include "mem_map.h"
28#include "utils.h"
29
30namespace art {
31
32class Object;
33
34class SpaceBitmap {
35 public:
36 static const size_t kAlignment = 8;
37
38 typedef void Callback(Object* obj, void* arg);
39
40 typedef void ScanCallback(Object* obj, void* finger, void* arg);
41
42 typedef void SweepCallback(size_t ptr_count, Object** ptrs, void* arg);
43
44 // Initialize a HeapBitmap so that it points to a bitmap large enough to cover a heap at
45 // heap_begin of heap_capacity bytes, where objects are guaranteed to be kAlignment-aligned.
46 static SpaceBitmap* Create(const std::string& name, byte* heap_begin, size_t heap_capacity);
47
48 ~SpaceBitmap();
49
50 // <offset> is the difference from .base to a pointer address.
51 // <index> is the index of .bits that contains the bit representing
52 // <offset>.
53 static size_t OffsetToIndex(size_t offset) {
54 return offset / kAlignment / kBitsPerWord;
55 }
56
57 static uintptr_t IndexToOffset(size_t index) {
58 return static_cast<uintptr_t>(index * kAlignment * kBitsPerWord);
59 }
60
61 // Pack the bits in backwards so they come out in address order when using CLZ.
62 static word OffsetToMask(uintptr_t offset_) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070063 return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070064 }
65
66 inline void Set(const Object* obj) {
67 Modify(obj, true);
68 }
69
70 inline void Clear(const Object* obj) {
71 Modify(obj, false);
72 }
73
74 void Clear();
75
76 inline bool Test(const Object* obj) const {
77 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
78 DCHECK(HasAddress(obj)) << obj;
79 DCHECK(bitmap_begin_ != NULL);
80 DCHECK_GE(addr, heap_begin_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070081 const uintptr_t offset = addr - heap_begin_;
82 return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070083 }
84
85 bool HasAddress(const void* addr) const;
86
87 void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const;
88
89 class ClearVisitor {
90 public:
91 explicit ClearVisitor(SpaceBitmap* const bitmap)
92 : bitmap_(bitmap) {
93 }
94
95 void operator ()(Object* obj) const {
96 bitmap_->Clear(obj);
97 }
98 private:
99 SpaceBitmap* const bitmap_;
100 };
101
102 template <typename Visitor>
103 void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const {
104 for (; visit_begin < visit_end; visit_begin += kAlignment ) {
105 visitor(reinterpret_cast<Object*>(visit_begin));
106 }
107 }
108
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700109 template <typename Visitor, typename FingerVisitor>
110 void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
111 const Visitor& visitor, const FingerVisitor& finger_visitor) const
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700113 DCHECK_LT(visit_begin, visit_end);
114
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700115 const size_t word_span = kAlignment * kBitsPerWord; // Equals IndexToOffset(1).
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700116 const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment;
117 const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment;
118
119 size_t word_start = bit_index_start / kBitsPerWord;
120 size_t word_end = bit_index_end / kBitsPerWord;
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700121 DCHECK_LT(word_end * kWordSize, Size());
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700122
123 // Trim off left_bits of left bits.
124 size_t edge_word = bitmap_begin_[word_start];
125
126 // Handle bits on the left first as a special case
127 size_t left_bits = bit_index_start & (kBitsPerWord - 1);
128 if (left_bits != 0) {
129 edge_word &= (1 << (kBitsPerWord - left_bits)) - 1;
130 }
131
132 // If word_start == word_end then handle this case at the same place we handle the right edge.
133 if (edge_word != 0 && word_start < word_end) {
134 uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700135 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700136 do {
137 const size_t shift = CLZ(edge_word);
138 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
139 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700140 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700141 } while (edge_word != 0);
142 }
143 word_start++;
144
145 for (size_t i = word_start; i < word_end; i++) {
146 size_t w = bitmap_begin_[i];
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700147 if (w != 0) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700148 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700149 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700150 do {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700151 const size_t shift = CLZ(w);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700152 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
153 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700154 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700155 } while (w != 0);
156 }
157 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700158
159 // Handle the right edge, and also the left edge if both edges are on the same word.
160 size_t right_bits = bit_index_end & (kBitsPerWord - 1);
161
162 // If word_start == word_end then we need to use the word which we removed the left bits.
163 if (word_start <= word_end) {
164 edge_word = bitmap_begin_[word_end];
165 }
166
167 // Bits that we trim off the right.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700168 edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700169 uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700170 finger_visitor(reinterpret_cast<void*>(ptr_base + word_span));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700171 while (edge_word != 0) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700172 const size_t shift = CLZ(edge_word);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700173 Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment);
174 visitor(obj);
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700175 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700176 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700177 }
178
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700179 void Walk(Callback* callback, void* arg)
180 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700182 void InOrderWalk(Callback* callback, void* arg)
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700183 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700184 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700185
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700186 static void SweepWalk(const SpaceBitmap& live,
187 const SpaceBitmap& mark,
188 uintptr_t base, uintptr_t max,
189 SweepCallback* thunk, void* arg);
190
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700191 void CopyFrom(SpaceBitmap* source_bitmap);
192
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700193 // Starting address of our internal storage.
194 word* Begin() {
195 return bitmap_begin_;
196 }
197
198 // Size of our internal storage
199 size_t Size() const {
200 return bitmap_size_;
201 }
202
203 // Size in bytes of the memory that the bitmaps spans.
204 size_t HeapSize() const {
205 return IndexToOffset(Size() / kWordSize);
206 }
207
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700208 uintptr_t HeapBegin() const {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700209 return heap_begin_;
210 }
211
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700212 // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()).
213 uintptr_t HeapLimit() const {
214 return HeapBegin() + static_cast<uintptr_t>(HeapSize());
215 }
216
217 // Set the max address which can covered by the bitmap.
218 void SetHeapLimit(uintptr_t new_end);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700219
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700220 std::string GetName() const;
221 void SetName(const std::string& name);
222
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700223 private:
224 // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1,
225 // however, we document that this is expected on heap_end_
226 SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin)
227 : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700228 heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)),
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700229 name_(name) {}
230
231 inline void Modify(const Object* obj, bool do_set) {
232 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
233 DCHECK_GE(addr, heap_begin_);
234 const uintptr_t offset = addr - heap_begin_;
235 const size_t index = OffsetToIndex(offset);
236 const word mask = OffsetToMask(offset);
237 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
238 if (do_set) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700239 bitmap_begin_[index] |= mask;
240 } else {
241 bitmap_begin_[index] &= ~mask;
242 }
243 }
244
245 // Backing storage for bitmap.
246 UniquePtr<MemMap> mem_map_;
247
248 // This bitmap itself, word sized for efficiency in scanning.
249 word* const bitmap_begin_;
250
251 // Size of this bitmap.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700252 size_t bitmap_size_;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700253
254 // The base address of the heap, which corresponds to the word containing the first bit in the
255 // bitmap.
256 const uintptr_t heap_begin_;
257
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700258 // Name of this bitmap.
259 std::string name_;
260};
261
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700262std::ostream& operator << (std::ostream& stream, const SpaceBitmap& bitmap);
263
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700264} // namespace art
265
266#endif // ART_SRC_SPACE_BITMAP_H_