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