Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [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_GC_SPACE_BITMAP_INL_H_ |
| 18 | #define ART_SRC_GC_SPACE_BITMAP_INL_H_ |
| 19 | |
| 20 | #include "base/logging.h" |
| 21 | #include "cutils/atomic.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | inline bool SpaceBitmap::AtomicTestAndSet(const mirror::Object* obj) { |
| 26 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 27 | DCHECK_GE(addr, heap_begin_); |
| 28 | const uintptr_t offset = addr - heap_begin_; |
| 29 | const size_t index = OffsetToIndex(offset); |
| 30 | const word mask = OffsetToMask(offset); |
| 31 | word* const address = &bitmap_begin_[index]; |
| 32 | DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_; |
| 33 | word old_word; |
| 34 | do { |
| 35 | old_word = *address; |
| 36 | // Fast path: The bit is already set. |
| 37 | if ((old_word & mask) != 0) { |
| 38 | return true; |
| 39 | } |
| 40 | } while (UNLIKELY(android_atomic_cas(old_word, old_word | mask, address) != 0)); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | inline bool SpaceBitmap::Test(const mirror::Object* obj) const { |
| 45 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 46 | DCHECK(HasAddress(obj)) << obj; |
| 47 | DCHECK(bitmap_begin_ != NULL); |
| 48 | DCHECK_GE(addr, heap_begin_); |
| 49 | const uintptr_t offset = addr - heap_begin_; |
| 50 | return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0; |
| 51 | } |
| 52 | |
| 53 | template <typename Visitor, typename FingerVisitor> |
| 54 | void SpaceBitmap::VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end, |
| 55 | const Visitor& visitor, |
| 56 | const FingerVisitor& finger_visitor) const { |
| 57 | DCHECK_LT(visit_begin, visit_end); |
| 58 | |
| 59 | const size_t word_span = kAlignment * kBitsPerWord; // Equals IndexToOffset(1). |
| 60 | const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment; |
| 61 | const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment; |
| 62 | |
| 63 | size_t word_start = bit_index_start / kBitsPerWord; |
| 64 | size_t word_end = bit_index_end / kBitsPerWord; |
| 65 | DCHECK_LT(word_end * kWordSize, Size()); |
| 66 | |
| 67 | // Trim off left_bits of left bits. |
| 68 | size_t edge_word = bitmap_begin_[word_start]; |
| 69 | |
| 70 | // Handle bits on the left first as a special case |
| 71 | size_t left_bits = bit_index_start & (kBitsPerWord - 1); |
| 72 | if (left_bits != 0) { |
| 73 | edge_word &= (1 << (kBitsPerWord - left_bits)) - 1; |
| 74 | } |
| 75 | |
| 76 | // If word_start == word_end then handle this case at the same place we handle the right edge. |
| 77 | if (edge_word != 0 && word_start < word_end) { |
| 78 | uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_; |
| 79 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
| 80 | do { |
| 81 | const size_t shift = CLZ(edge_word); |
| 82 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment); |
| 83 | visitor(obj); |
| 84 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
| 85 | } while (edge_word != 0); |
| 86 | } |
| 87 | word_start++; |
| 88 | |
| 89 | for (size_t i = word_start; i < word_end; i++) { |
| 90 | size_t w = bitmap_begin_[i]; |
| 91 | if (w != 0) { |
| 92 | uintptr_t ptr_base = IndexToOffset(i) + heap_begin_; |
| 93 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
| 94 | do { |
| 95 | const size_t shift = CLZ(w); |
| 96 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment); |
| 97 | visitor(obj); |
| 98 | w ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
| 99 | } while (w != 0); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Handle the right edge, and also the left edge if both edges are on the same word. |
| 104 | size_t right_bits = bit_index_end & (kBitsPerWord - 1); |
| 105 | |
| 106 | // If word_start == word_end then we need to use the word which we removed the left bits. |
| 107 | if (word_start <= word_end) { |
| 108 | edge_word = bitmap_begin_[word_end]; |
| 109 | } |
| 110 | |
| 111 | // Bits that we trim off the right. |
| 112 | edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1); |
| 113 | uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_; |
| 114 | finger_visitor(reinterpret_cast<void*>(ptr_base + word_span)); |
| 115 | while (edge_word != 0) { |
| 116 | const size_t shift = CLZ(edge_word); |
| 117 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment); |
| 118 | visitor(obj); |
| 119 | edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | inline bool SpaceBitmap::Modify(const mirror::Object* obj, bool do_set) { |
| 124 | uintptr_t addr = reinterpret_cast<uintptr_t>(obj); |
| 125 | DCHECK_GE(addr, heap_begin_); |
| 126 | const uintptr_t offset = addr - heap_begin_; |
| 127 | const size_t index = OffsetToIndex(offset); |
| 128 | const word mask = OffsetToMask(offset); |
| 129 | DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_; |
| 130 | word* address = &bitmap_begin_[index]; |
| 131 | word old_word = *address; |
| 132 | if (do_set) { |
| 133 | *address = old_word | mask; |
| 134 | } else { |
| 135 | *address = old_word & ~mask; |
| 136 | } |
| 137 | return (old_word & mask) != 0; |
| 138 | } |
| 139 | } // namespace art |
| 140 | |
| 141 | #endif // ART_SRC_GC_SPACE_BITMAP_INL_H_ |