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