blob: 880ff1f74a554f0291bb0039cb14f18286ba06bb [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);
Andreas Gampecb8aea42014-04-02 15:39:58 -070032 const uword mask = OffsetToMask(offset);
33 uword* const address = &bitmap_begin_[index];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
Andreas Gampecb8aea42014-04-02 15:39:58 -070035 uword old_word;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036 do {
37 old_word = *address;
38 // Fast path: The bit is already set.
39 if ((old_word & mask) != 0) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080040 DCHECK(Test(obj));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041 return true;
42 }
Ian Rogers55b27642014-01-23 16:37:07 -080043 } while (!__sync_bool_compare_and_swap(address, old_word, old_word | mask));
Ian Rogersef7d42f2014-01-06 12:55:46 -080044 DCHECK(Test(obj));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045 return false;
46}
47
48inline 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 Chartier184e3222013-08-03 14:02:57 -070057template <typename Visitor>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058void SpaceBitmap::VisitMarkedRange(uintptr_t visit_begin, uintptr_t visit_end,
Mathieu Chartier184e3222013-08-03 14:02:57 -070059 const Visitor& visitor) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080060 DCHECK_LT(visit_begin, visit_end);
Andreas Gampebe73e572014-04-03 10:46:42 -070061#if 0
62 for (uintptr_t i = visit_begin; i < visit_end; i += kAlignment) {
63 mirror::Object* obj = reinterpret_cast<mirror::Object*>(i);
64 if (Test(obj)) {
65 visitor(obj);
66 }
67 }
68#else
Andreas Gampecb8aea42014-04-02 15:39:58 -070069 DCHECK_LE(heap_begin_, visit_begin);
70 DCHECK_LE(visit_end, HeapLimit());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071
Andreas Gampecb8aea42014-04-02 15:39:58 -070072 const uintptr_t offset_start = visit_begin - heap_begin_;
73 const uintptr_t offset_end = visit_end - heap_begin_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074
Andreas Gampecb8aea42014-04-02 15:39:58 -070075 const uintptr_t index_start = OffsetToIndex(offset_start);
76 const uintptr_t index_end = OffsetToIndex(offset_end);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077
Andreas Gampecb8aea42014-04-02 15:39:58 -070078 const size_t bit_start = (offset_start / kAlignment) % kBitsPerWord;
79 const size_t bit_end = (offset_end / kAlignment) % kBitsPerWord;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Andreas Gampecb8aea42014-04-02 15:39:58 -070081 // Index(begin) ... Index(end)
82 // [xxxxx???][........][????yyyy]
83 // ^ ^
84 // | #---- Bit of visit_end
85 // #---- Bit of visit_begin
86 //
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
Andreas Gampecb8aea42014-04-02 15:39:58 -070088 // Left edge.
89 uword left_edge = bitmap_begin_[index_start];
90 // Mark of lower bits that are not in range.
91 left_edge &= ~((static_cast<uword>(1) << bit_start) - 1);
92
93 // Right edge. Either unique, or left_edge.
94 uword right_edge;
95
96 if (index_start < index_end) {
97 // Left edge != right edge.
98
99 // Traverse left edge.
100 if (left_edge != 0) {
101 const uintptr_t ptr_base = IndexToOffset(index_start) + heap_begin_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 do {
Andreas Gampecb8aea42014-04-02 15:39:58 -0700103 const size_t shift = CTZ(left_edge);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
105 visitor(obj);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700106 left_edge ^= (static_cast<uword>(1)) << shift;
107 } while (left_edge != 0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 }
Andreas Gampecb8aea42014-04-02 15:39:58 -0700109
110 // Traverse the middle, full part.
111 for (size_t i = index_start + 1; i < index_end; ++i) {
112 uword w = bitmap_begin_[i];
113 if (w != 0) {
114 const uintptr_t ptr_base = IndexToOffset(i) + heap_begin_;
115 do {
116 const size_t shift = CTZ(w);
117 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
118 visitor(obj);
119 w ^= (static_cast<uword>(1)) << shift;
120 } while (w != 0);
121 }
122 }
123
124 // Right edge is unique.
Andreas Gampebe73e572014-04-03 10:46:42 -0700125 // But maybe we don't have anything to do: visit_end starts in a new word...
126 if (bit_end == 0) {
127 // Do not read memory, as it could be after the end of the bitmap.
128 right_edge = 0;
129 } else {
130 right_edge = bitmap_begin_[index_end];
131 }
Andreas Gampecb8aea42014-04-02 15:39:58 -0700132 } else {
133 // Right edge = left edge.
134 right_edge = left_edge;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135 }
136
Andreas Gampecb8aea42014-04-02 15:39:58 -0700137 // Right edge handling.
Andreas Gampebe73e572014-04-03 10:46:42 -0700138 right_edge &= ((static_cast<uword>(1) << bit_end) - 1);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700139 if (right_edge != 0) {
140 const uintptr_t ptr_base = IndexToOffset(index_end) + heap_begin_;
141 do {
142 const size_t shift = CTZ(right_edge);
143 mirror::Object* obj = reinterpret_cast<mirror::Object*>(ptr_base + shift * kAlignment);
144 visitor(obj);
145 right_edge ^= (static_cast<uword>(1)) << shift;
146 } while (right_edge != 0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147 }
Andreas Gampebe73e572014-04-03 10:46:42 -0700148#endif
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149}
150
151inline bool SpaceBitmap::Modify(const mirror::Object* obj, bool do_set) {
152 uintptr_t addr = reinterpret_cast<uintptr_t>(obj);
153 DCHECK_GE(addr, heap_begin_);
154 const uintptr_t offset = addr - heap_begin_;
155 const size_t index = OffsetToIndex(offset);
Andreas Gampecb8aea42014-04-02 15:39:58 -0700156 const uword mask = OffsetToMask(offset);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800157 DCHECK_LT(index, bitmap_size_ / kWordSize) << " bitmap_size_ = " << bitmap_size_;
Andreas Gampecb8aea42014-04-02 15:39:58 -0700158 uword* address = &bitmap_begin_[index];
159 uword old_word = *address;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 if (do_set) {
161 *address = old_word | mask;
162 } else {
163 *address = old_word & ~mask;
164 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 DCHECK_EQ(Test(obj), do_set);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 return (old_word & mask) != 0;
167}
Ian Rogers1d54e732013-05-02 21:10:01 -0700168
169} // namespace accounting
170} // namespace gc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171} // namespace art
172
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700173#endif // ART_RUNTIME_GC_ACCOUNTING_SPACE_BITMAP_INL_H_