1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2013 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include "bump_pointer_space-inl.h"
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
23*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
24*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker #include <memory>
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
29*795d594fSAndroid Build Coastguard Worker namespace gc {
30*795d594fSAndroid Build Coastguard Worker namespace space {
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker template <typename Visitor>
Walk(Visitor && visitor)33*795d594fSAndroid Build Coastguard Worker inline void BumpPointerSpace::Walk(Visitor&& visitor) {
34*795d594fSAndroid Build Coastguard Worker uint8_t* pos = Begin();
35*795d594fSAndroid Build Coastguard Worker uint8_t* end = End();
36*795d594fSAndroid Build Coastguard Worker uint8_t* main_end = pos;
37*795d594fSAndroid Build Coastguard Worker size_t black_dense_size;
38*795d594fSAndroid Build Coastguard Worker std::unique_ptr<std::vector<size_t>> block_sizes_copy;
39*795d594fSAndroid Build Coastguard Worker // Internal indirection w/ NO_THREAD_SAFETY_ANALYSIS. Optimally, we'd like to have an annotation
40*795d594fSAndroid Build Coastguard Worker // like
41*795d594fSAndroid Build Coastguard Worker // REQUIRES_AS(visitor.operator(mirror::Object*))
42*795d594fSAndroid Build Coastguard Worker // on Walk to expose the interprocedural nature of locks here without having to duplicate the
43*795d594fSAndroid Build Coastguard Worker // function.
44*795d594fSAndroid Build Coastguard Worker //
45*795d594fSAndroid Build Coastguard Worker // NO_THREAD_SAFETY_ANALYSIS is a workaround. The problem with the workaround of course is that
46*795d594fSAndroid Build Coastguard Worker // it doesn't complain at the callsite. However, that is strictly not worse than the
47*795d594fSAndroid Build Coastguard Worker // ObjectCallback version it replaces.
48*795d594fSAndroid Build Coastguard Worker auto no_thread_safety_analysis_visit = [&](mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
49*795d594fSAndroid Build Coastguard Worker visitor(obj);
50*795d594fSAndroid Build Coastguard Worker };
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker {
53*795d594fSAndroid Build Coastguard Worker MutexLock mu(Thread::Current(), lock_);
54*795d594fSAndroid Build Coastguard Worker // If we have 0 blocks then we need to update the main header since we have bump pointer style
55*795d594fSAndroid Build Coastguard Worker // allocation into an unbounded region (actually bounded by Capacity()).
56*795d594fSAndroid Build Coastguard Worker if (block_sizes_.empty()) {
57*795d594fSAndroid Build Coastguard Worker UpdateMainBlock();
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker main_end = Begin() + main_block_size_;
60*795d594fSAndroid Build Coastguard Worker if (block_sizes_.empty()) {
61*795d594fSAndroid Build Coastguard Worker // We don't have any other blocks, this means someone else may be allocating into the main
62*795d594fSAndroid Build Coastguard Worker // block. In this case, we don't want to try and visit the other blocks after the main block
63*795d594fSAndroid Build Coastguard Worker // since these could actually be part of the main block.
64*795d594fSAndroid Build Coastguard Worker end = main_end;
65*795d594fSAndroid Build Coastguard Worker } else {
66*795d594fSAndroid Build Coastguard Worker block_sizes_copy.reset(new std::vector<size_t>(block_sizes_.begin(), block_sizes_.end()));
67*795d594fSAndroid Build Coastguard Worker }
68*795d594fSAndroid Build Coastguard Worker
69*795d594fSAndroid Build Coastguard Worker black_dense_size = black_dense_region_size_;
70*795d594fSAndroid Build Coastguard Worker }
71*795d594fSAndroid Build Coastguard Worker
72*795d594fSAndroid Build Coastguard Worker // black_dense_region_size_ will be non-zero only in case of moving-space of CMC GC.
73*795d594fSAndroid Build Coastguard Worker if (black_dense_size > 0) {
74*795d594fSAndroid Build Coastguard Worker // Objects are not packed in this case, and therefore the bitmap is needed
75*795d594fSAndroid Build Coastguard Worker // to walk this part of the space.
76*795d594fSAndroid Build Coastguard Worker // Remember the last object visited using bitmap to be able to fetch its size.
77*795d594fSAndroid Build Coastguard Worker mirror::Object* last_obj = nullptr;
78*795d594fSAndroid Build Coastguard Worker auto return_obj_visit = [&](mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
79*795d594fSAndroid Build Coastguard Worker visitor(obj);
80*795d594fSAndroid Build Coastguard Worker last_obj = obj;
81*795d594fSAndroid Build Coastguard Worker };
82*795d594fSAndroid Build Coastguard Worker GetMarkBitmap()->VisitMarkedRange(reinterpret_cast<uintptr_t>(pos),
83*795d594fSAndroid Build Coastguard Worker reinterpret_cast<uintptr_t>(pos + black_dense_size),
84*795d594fSAndroid Build Coastguard Worker return_obj_visit);
85*795d594fSAndroid Build Coastguard Worker pos += black_dense_size;
86*795d594fSAndroid Build Coastguard Worker if (last_obj != nullptr) {
87*795d594fSAndroid Build Coastguard Worker // If the last object visited using bitmap was large enough to go past the
88*795d594fSAndroid Build Coastguard Worker // black-dense region, then we need to adjust for that to be able to visit
89*795d594fSAndroid Build Coastguard Worker // objects one after the other below.
90*795d594fSAndroid Build Coastguard Worker pos = std::max(pos, reinterpret_cast<uint8_t*>(GetNextObject(last_obj)));
91*795d594fSAndroid Build Coastguard Worker }
92*795d594fSAndroid Build Coastguard Worker }
93*795d594fSAndroid Build Coastguard Worker // Walk all of the objects in the main block first.
94*795d594fSAndroid Build Coastguard Worker while (pos < main_end) {
95*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
96*795d594fSAndroid Build Coastguard Worker // No read barrier because obj may not be a valid object.
97*795d594fSAndroid Build Coastguard Worker if (obj->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>() == nullptr) {
98*795d594fSAndroid Build Coastguard Worker // There is a race condition where a thread has just allocated an object but not set the
99*795d594fSAndroid Build Coastguard Worker // class. We can't know the size of this object, so we don't visit it and break the loop
100*795d594fSAndroid Build Coastguard Worker pos = main_end;
101*795d594fSAndroid Build Coastguard Worker break;
102*795d594fSAndroid Build Coastguard Worker } else {
103*795d594fSAndroid Build Coastguard Worker no_thread_safety_analysis_visit(obj);
104*795d594fSAndroid Build Coastguard Worker pos = reinterpret_cast<uint8_t*>(GetNextObject(obj));
105*795d594fSAndroid Build Coastguard Worker }
106*795d594fSAndroid Build Coastguard Worker }
107*795d594fSAndroid Build Coastguard Worker // Walk the other blocks (currently only TLABs).
108*795d594fSAndroid Build Coastguard Worker if (block_sizes_copy != nullptr) {
109*795d594fSAndroid Build Coastguard Worker size_t iter = 0;
110*795d594fSAndroid Build Coastguard Worker size_t num_blks = block_sizes_copy->size();
111*795d594fSAndroid Build Coastguard Worker // Skip blocks which are already visited above as part of black-dense region.
112*795d594fSAndroid Build Coastguard Worker for (uint8_t* ptr = main_end; iter < num_blks; iter++) {
113*795d594fSAndroid Build Coastguard Worker size_t block_size = (*block_sizes_copy)[iter];
114*795d594fSAndroid Build Coastguard Worker ptr += block_size;
115*795d594fSAndroid Build Coastguard Worker if (ptr > pos) {
116*795d594fSAndroid Build Coastguard Worker // Adjust block-size in case 'pos' is in the middle of the block.
117*795d594fSAndroid Build Coastguard Worker if (static_cast<ssize_t>(block_size) > ptr - pos) {
118*795d594fSAndroid Build Coastguard Worker (*block_sizes_copy)[iter] = ptr - pos;
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker break;
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker }
123*795d594fSAndroid Build Coastguard Worker
124*795d594fSAndroid Build Coastguard Worker for (; iter < num_blks; iter++) {
125*795d594fSAndroid Build Coastguard Worker size_t block_size = (*block_sizes_copy)[iter];
126*795d594fSAndroid Build Coastguard Worker mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
127*795d594fSAndroid Build Coastguard Worker const mirror::Object* end_obj = reinterpret_cast<const mirror::Object*>(pos + block_size);
128*795d594fSAndroid Build Coastguard Worker CHECK_LE(reinterpret_cast<const uint8_t*>(end_obj), End());
129*795d594fSAndroid Build Coastguard Worker // We don't know how many objects are allocated in the current block. When we hit a null class
130*795d594fSAndroid Build Coastguard Worker // assume it's the end. TODO: Have a thread update the header when it flushes the block?
131*795d594fSAndroid Build Coastguard Worker // No read barrier because obj may not be a valid object.
132*795d594fSAndroid Build Coastguard Worker while (obj < end_obj && obj->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) {
133*795d594fSAndroid Build Coastguard Worker no_thread_safety_analysis_visit(obj);
134*795d594fSAndroid Build Coastguard Worker obj = GetNextObject(obj);
135*795d594fSAndroid Build Coastguard Worker }
136*795d594fSAndroid Build Coastguard Worker pos += block_size;
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker } else {
139*795d594fSAndroid Build Coastguard Worker CHECK_EQ(end, main_end);
140*795d594fSAndroid Build Coastguard Worker }
141*795d594fSAndroid Build Coastguard Worker CHECK_EQ(pos, end);
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker
144*795d594fSAndroid Build Coastguard Worker } // namespace space
145*795d594fSAndroid Build Coastguard Worker } // namespace gc
146*795d594fSAndroid Build Coastguard Worker } // namespace art
147*795d594fSAndroid Build Coastguard Worker
148*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
149