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