blob: 01c70facdfb4e2508d0c2c566173eaa34f12556f [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 Rogers1d54e732013-05-02 21:10:01 -070021#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022
23namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070024namespace gc {
25namespace accounting {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026
27inline 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) {
40 return true;
41 }
Ian Rogers55b27642014-01-23 16:37:07 -080042 } while (!__sync_bool_compare_and_swap(address, old_word, old_word | mask));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043 return false;
44}
45
46inline bool SpaceBitmap::Test(const mirror::Object* obj) const {
47 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
48 DCHECK(HasAddress(obj)) << obj;
49 DCHECK(bitmap_begin_ != NULL);
50 DCHECK_GE(addr, heap_begin_);
51 const uintptr_t offset = addr - heap_begin_;
52 return (bitmap_begin_[OffsetToIndex(offset)] & OffsetToMask(offset)) != 0;
53}
54
Mathieu Chartier184e3222013-08-03 14:02:57 -070055template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056void SpaceBitmap::VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
Mathieu Chartier184e3222013-08-03 14:02:57 -070057 const Visitor& visitor) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058 DCHECK_LT(visit_begin, visit_end);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 const size_t bit_index_start = (visit_begin - heap_begin_) / kAlignment;
60 const size_t bit_index_end = (visit_end - heap_begin_ - 1) / kAlignment;
61
62 size_t word_start = bit_index_start / kBitsPerWord;
63 size_t word_end = bit_index_end / kBitsPerWord;
64 DCHECK_LT(word_end * kWordSize, Size());
65
66 // Trim off left_bits of left bits.
67 size_t edge_word = bitmap_begin_[word_start];
68
69 // Handle bits on the left first as a special case
70 size_t left_bits = bit_index_start & (kBitsPerWord - 1);
71 if (left_bits != 0) {
72 edge_word &= (1 << (kBitsPerWord - left_bits)) - 1;
73 }
74
75 // If word_start == word_end then handle this case at the same place we handle the right edge.
76 if (edge_word != 0 && word_start < word_end) {
77 uintptr_t ptr_base = IndexToOffset(word_start) + heap_begin_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078 do {
79 const size_t shift = CLZ(edge_word);
80 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
81 visitor(obj);
82 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
83 } while (edge_word != 0);
84 }
85 word_start++;
86
87 for (size_t i = word_start; i < word_end; i++) {
88 size_t w = bitmap_begin_[i];
89 if (w != 0) {
90 uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 do {
92 const size_t shift = CLZ(w);
93 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
94 visitor(obj);
95 w ^= static_cast<size_t>(kWordHighBitMask) >> shift;
96 } while (w != 0);
97 }
98 }
99
100 // Handle the right edge, and also the left edge if both edges are on the same word.
101 size_t right_bits = bit_index_end & (kBitsPerWord - 1);
102
103 // If word_start == word_end then we need to use the word which we removed the left bits.
104 if (word_start <= word_end) {
105 edge_word = bitmap_begin_[word_end];
106 }
107
108 // Bits that we trim off the right.
109 edge_word &= ~((static_cast<size_t>(kWordHighBitMask) >> right_bits) - 1);
110 uintptr_t ptr_base = IndexToOffset(word_end) + heap_begin_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 while (edge_word != 0) {
112 const size_t shift = CLZ(edge_word);
113 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
114 visitor(obj);
115 edge_word ^= static_cast<size_t>(kWordHighBitMask) >> shift;
116 }
117}
118
119inline bool SpaceBitmap::Modify(const mirror::Object* obj, bool do_set) {
120 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
121 DCHECK_GE(addr, heap_begin_);
122 const uintptr_t offset = addr - heap_begin_;
123 const size_t index = OffsetToIndex(offset);
124 const word mask = OffsetToMask(offset);
125 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
126 word* address = &bitmap_begin_[index];
127 word old_word = *address;
128 if (do_set) {
129 *address = old_word | mask;
130 } else {
131 *address = old_word & ~mask;
132 }
133 return (old_word & mask) != 0;
134}
Ian Rogers1d54e732013-05-02 21:10:01 -0700135
136} // namespace accounting
137} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138} // namespace art
139
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700140#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_INL_H_