Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | namespace art { |
| 31 | |
| 32 | class Object; |
| 33 | |
| 34 | class 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 Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 63 | return static_cast<uintptr_t>(kWordHighBitMask) >> ((offset_ / kAlignment) % kBitsPerWord); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 64 | } |
| 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_); |
| 81 | if (addr <= heap_end_) { |
| 82 | const uintptr_t offset = addr - heap_begin_; |
| 83 | return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0; |
| 84 | } else { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool HasAddress(const void* addr) const; |
| 90 | |
| 91 | void VisitRange(uintptr_t base, uintptr_t max, Callback* visitor, void* arg) const; |
| 92 | |
| 93 | class ClearVisitor { |
| 94 | public: |
| 95 | explicit ClearVisitor(SpaceBitmap* const bitmap) |
| 96 | : bitmap_(bitmap) { |
| 97 | } |
| 98 | |
| 99 | void operator ()(Object* obj) const { |
| 100 | bitmap_->Clear(obj); |
| 101 | } |
| 102 | private: |
| 103 | SpaceBitmap* const bitmap_; |
| 104 | }; |
| 105 | |
| 106 | template <typename Visitor> |
| 107 | void VisitRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const { |
| 108 | for (; visit_begin < visit_end; visit_begin += kAlignment ) { |
| 109 | visitor(reinterpret_cast<Object*>(visit_begin)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | template <typename Visitor> |
| 114 | void VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, const Visitor& visitor) const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 115 | DCHECK_LT(visit_begin, visit_end); |
| 116 | |
| 117 | const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment; |
| 118 | const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment; |
| 119 | |
| 120 | size_t word_start = bit_index_start / kBitsPerWord; |
| 121 | size_t word_end = bit_index_end / kBitsPerWord; |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 122 | DCHECK_LT(word_end * kWordSize, Size()); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 123 | |
| 124 | // Trim off left_bits of left bits. |
| 125 | size_t edge_word = bitmap_begin_[word_start]; |
| 126 | |
| 127 | // Handle bits on the left first as a special case |
| 128 | size_t left_bits = bit_index_start & (kBitsPerWord - 1); |
| 129 | if (left_bits != 0) { |
| 130 | edge_word &= (1 << (kBitsPerWord - left_bits)) - 1; |
| 131 | } |
| 132 | |
| 133 | // If word_start == word_end then handle this case at the same place we handle the right edge. |
| 134 | if (edge_word != 0 && word_start < word_end) { |
| 135 | uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_; |
| 136 | do { |
| 137 | const size_t shift = CLZ(edge_word); |
| 138 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 139 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 140 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 141 | } 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 Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 147 | if (w != 0) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 148 | uintptr_t ptr_base = IndexToOffset(i) + heap_begin_; |
| 149 | do { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 150 | const size_t shift = CLZ(w); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 151 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 152 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 153 | w ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 154 | } while (w != 0); |
| 155 | } |
| 156 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 157 | |
| 158 | // Handle the right edge, and also the left edge if both edges are on the same word. |
| 159 | size_t right_bits = bit_index_end & (kBitsPerWord - 1); |
| 160 | |
| 161 | // If word_start == word_end then we need to use the word which we removed the left bits. |
| 162 | if (word_start <= word_end) { |
| 163 | edge_word = bitmap_begin_[word_end]; |
| 164 | } |
| 165 | |
| 166 | // Bits that we trim off the right. |
| 167 | const size_t trim_bits = kBitsPerWord - 1 - right_bits; |
| 168 | edge_word &= ~((1 << trim_bits) - 1); |
| 169 | uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_; |
| 170 | while (edge_word != 0) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 171 | const size_t shift = CLZ(edge_word); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 172 | Object* obj = reinterpret_cast<Object*>(ptr_base + shift * kAlignment); |
| 173 | visitor(obj); |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 174 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 175 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void Walk(Callback* callback, void* arg); |
| 179 | |
| 180 | void InOrderWalk(Callback* callback, void* arg); |
| 181 | |
| 182 | void ScanWalk(uintptr_t base, uintptr_t max, ScanCallback* thunk, void* arg); |
| 183 | |
| 184 | static void SweepWalk(const SpaceBitmap& live, |
| 185 | const SpaceBitmap& mark, |
| 186 | uintptr_t base, uintptr_t max, |
| 187 | SweepCallback* thunk, void* arg); |
| 188 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 189 | // Starting address of our internal storage. |
| 190 | word* Begin() { |
| 191 | return bitmap_begin_; |
| 192 | } |
| 193 | |
| 194 | // Size of our internal storage |
| 195 | size_t Size() const { |
| 196 | return bitmap_size_; |
| 197 | } |
| 198 | |
| 199 | // Size in bytes of the memory that the bitmaps spans. |
| 200 | size_t HeapSize() const { |
| 201 | return IndexToOffset(Size() / kWordSize); |
| 202 | } |
| 203 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 204 | uintptr_t HeapBegin() const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 205 | return heap_begin_; |
| 206 | } |
| 207 | |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame^] | 208 | // The maximum address which the bitmap can span. (HeapBegin() <= object < HeapLimit()). |
| 209 | uintptr_t HeapLimit() const { |
| 210 | return HeapBegin() + static_cast<uintptr_t>(HeapSize()); |
| 211 | } |
| 212 | |
| 213 | // Set the max address which can covered by the bitmap. |
| 214 | void SetHeapLimit(uintptr_t new_end); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 215 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 216 | private: |
| 217 | // TODO: heap_end_ is initialized so that the heap bitmap is empty, this doesn't require the -1, |
| 218 | // however, we document that this is expected on heap_end_ |
| 219 | SpaceBitmap(const std::string& name, MemMap* mem_map, word* bitmap_begin, size_t bitmap_size, const void* heap_begin) |
| 220 | : mem_map_(mem_map), bitmap_begin_(bitmap_begin), bitmap_size_(bitmap_size), |
| 221 | heap_begin_(reinterpret_cast<uintptr_t>(heap_begin)), heap_end_(heap_begin_ - 1), |
| 222 | name_(name) {} |
| 223 | |
| 224 | inline void Modify(const Object* obj, bool do_set) { |
| 225 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 226 | DCHECK_GE(addr, heap_begin_); |
| 227 | const uintptr_t offset = addr - heap_begin_; |
| 228 | const size_t index = OffsetToIndex(offset); |
| 229 | const word mask = OffsetToMask(offset); |
| 230 | DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_; |
| 231 | if (do_set) { |
| 232 | if (addr > heap_end_) { |
| 233 | heap_end_ = addr; |
| 234 | } |
| 235 | bitmap_begin_[index] |= mask; |
| 236 | } else { |
| 237 | bitmap_begin_[index] &= ~mask; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Backing storage for bitmap. |
| 242 | UniquePtr<MemMap> mem_map_; |
| 243 | |
| 244 | // This bitmap itself, word sized for efficiency in scanning. |
| 245 | word* const bitmap_begin_; |
| 246 | |
| 247 | // Size of this bitmap. |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 248 | size_t bitmap_size_; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 249 | |
| 250 | // The base address of the heap, which corresponds to the word containing the first bit in the |
| 251 | // bitmap. |
| 252 | const uintptr_t heap_begin_; |
| 253 | |
| 254 | // The highest pointer value ever returned by an allocation from |
| 255 | // this heap. I.e., the highest address that may correspond to a |
| 256 | // set bit. If there are no bits set, (heap_end_ < heap_begin_). |
| 257 | uintptr_t heap_end_; |
| 258 | |
| 259 | // Name of this bitmap. |
| 260 | std::string name_; |
| 261 | }; |
| 262 | |
| 263 | } // namespace art |
| 264 | |
| 265 | #endif // ART_SRC_SPACE_BITMAP_H_ |