1 /*
2 * Copyright (C) 2013 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
17 #ifndef ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
18 #define ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
19
20 #include "bump_pointer_space-inl.h"
21
22 #include "base/bit_utils.h"
23 #include "mirror/object-inl.h"
24 #include "thread-current-inl.h"
25
26 #include <memory>
27
28 namespace art HIDDEN {
29 namespace gc {
30 namespace space {
31
32 template <typename Visitor>
Walk(Visitor && visitor)33 inline void BumpPointerSpace::Walk(Visitor&& visitor) {
34 uint8_t* pos = Begin();
35 uint8_t* end = End();
36 uint8_t* main_end = pos;
37 std::unique_ptr<std::vector<size_t>> block_sizes_copy;
38 // Internal indirection w/ NO_THREAD_SAFETY_ANALYSIS. Optimally, we'd like to have an annotation
39 // like
40 // REQUIRES_AS(visitor.operator(mirror::Object*))
41 // on Walk to expose the interprocedural nature of locks here without having to duplicate the
42 // function.
43 //
44 // NO_THREAD_SAFETY_ANALYSIS is a workaround. The problem with the workaround of course is that
45 // it doesn't complain at the callsite. However, that is strictly not worse than the
46 // ObjectCallback version it replaces.
47 auto no_thread_safety_analysis_visit = [&](mirror::Object* obj) NO_THREAD_SAFETY_ANALYSIS {
48 visitor(obj);
49 };
50
51 {
52 MutexLock mu(Thread::Current(), lock_);
53 // If we have 0 blocks then we need to update the main header since we have bump pointer style
54 // allocation into an unbounded region (actually bounded by Capacity()).
55 if (block_sizes_.empty()) {
56 UpdateMainBlock();
57 }
58 main_end = Begin() + main_block_size_;
59 if (block_sizes_.empty()) {
60 // We don't have any other blocks, this means someone else may be allocating into the main
61 // block. In this case, we don't want to try and visit the other blocks after the main block
62 // since these could actually be part of the main block.
63 end = main_end;
64 } else {
65 block_sizes_copy.reset(new std::vector<size_t>(block_sizes_.begin(), block_sizes_.end()));
66 }
67 }
68 // Walk all of the objects in the main block first.
69 while (pos < main_end) {
70 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
71 // No read barrier because obj may not be a valid object.
72 if (obj->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>() == nullptr) {
73 // There is a race condition where a thread has just allocated an object but not set the
74 // class. We can't know the size of this object, so we don't visit it and break the loop
75 pos = main_end;
76 break;
77 } else {
78 no_thread_safety_analysis_visit(obj);
79 pos = reinterpret_cast<uint8_t*>(GetNextObject(obj));
80 }
81 }
82 // Walk the other blocks (currently only TLABs).
83 if (block_sizes_copy != nullptr) {
84 for (size_t block_size : *block_sizes_copy) {
85 mirror::Object* obj = reinterpret_cast<mirror::Object*>(pos);
86 const mirror::Object* end_obj = reinterpret_cast<const mirror::Object*>(pos + block_size);
87 CHECK_LE(reinterpret_cast<const uint8_t*>(end_obj), End());
88 // We don't know how many objects are allocated in the current block. When we hit a null class
89 // assume it's the end. TODO: Have a thread update the header when it flushes the block?
90 // No read barrier because obj may not be a valid object.
91 while (obj < end_obj && obj->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) {
92 no_thread_safety_analysis_visit(obj);
93 obj = GetNextObject(obj);
94 }
95 pos += block_size;
96 }
97 } else {
98 CHECK_EQ(end, main_end);
99 }
100 CHECK_EQ(pos, end);
101 }
102
103 } // namespace space
104 } // namespace gc
105 } // namespace art
106
107 #endif // ART_RUNTIME_GC_SPACE_BUMP_POINTER_SPACE_WALK_INL_H_
108