Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2014 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_READ_BARRIER_H_
     18 #define ART_RUNTIME_READ_BARRIER_H_
     19 
     20 #include "base/mutex.h"
     21 #include "base/macros.h"
     22 #include "gc_root.h"
     23 #include "jni.h"
     24 #include "mirror/object_reference.h"
     25 #include "offsets.h"
     26 #include "read_barrier_c.h"
     27 
     28 // This is a C++ (not C) header file, separate from read_barrier_c.h
     29 // which needs to be a C header file for asm_support.h.
     30 
     31 namespace art {
     32 namespace mirror {
     33   class Object;
     34   template<typename MirrorType> class HeapReference;
     35 }  // namespace mirror
     36 class ArtMethod;
     37 
     38 class ReadBarrier {
     39  public:
     40   // TODO: disable thse flags for production use.
     41   // Enable the to-space invariant checks.
     42   static constexpr bool kEnableToSpaceInvariantChecks = true;
     43   // Enable the read barrier checks.
     44   static constexpr bool kEnableReadBarrierInvariantChecks = true;
     45 
     46   // It's up to the implementation whether the given field gets updated whereas the return value
     47   // must be an updated reference unless kAlwaysUpdateField is true.
     48   template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
     49             bool kAlwaysUpdateField = false>
     50   ALWAYS_INLINE static MirrorType* Barrier(
     51       mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
     52       SHARED_REQUIRES(Locks::mutator_lock_);
     53 
     54   // It's up to the implementation whether the given root gets updated
     55   // whereas the return value must be an updated reference.
     56   template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
     57   ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root,
     58                                                   GcRootSource* gc_root_source = nullptr)
     59       SHARED_REQUIRES(Locks::mutator_lock_);
     60 
     61   // It's up to the implementation whether the given root gets updated
     62   // whereas the return value must be an updated reference.
     63   template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
     64   ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
     65                                                   GcRootSource* gc_root_source = nullptr)
     66       SHARED_REQUIRES(Locks::mutator_lock_);
     67 
     68   static bool IsDuringStartup();
     69 
     70   // Without the holder object.
     71   static void AssertToSpaceInvariant(mirror::Object* ref)
     72       SHARED_REQUIRES(Locks::mutator_lock_) {
     73     AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
     74   }
     75   // With the holder object.
     76   static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
     77                                      mirror::Object* ref)
     78       SHARED_REQUIRES(Locks::mutator_lock_);
     79   // With GcRootSource.
     80   static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
     81       SHARED_REQUIRES(Locks::mutator_lock_);
     82 
     83   // ALWAYS_INLINE on this caused a performance regression b/26744236.
     84   static mirror::Object* Mark(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
     85 
     86   static mirror::Object* WhitePtr() {
     87     return reinterpret_cast<mirror::Object*>(white_ptr_);
     88   }
     89   static mirror::Object* GrayPtr() {
     90     return reinterpret_cast<mirror::Object*>(gray_ptr_);
     91   }
     92   static mirror::Object* BlackPtr() {
     93     return reinterpret_cast<mirror::Object*>(black_ptr_);
     94   }
     95 
     96   ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj,
     97                                                       uintptr_t* out_rb_ptr_high_bits)
     98       SHARED_REQUIRES(Locks::mutator_lock_);
     99 
    100   // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them.
    101   static constexpr uintptr_t white_ptr_ = 0x0;    // Not marked.
    102   static constexpr uintptr_t gray_ptr_ = 0x1;     // Marked, but not marked through. On mark stack.
    103   static constexpr uintptr_t black_ptr_ = 0x2;    // Marked through. Used for non-moving objects.
    104   static constexpr uintptr_t rb_ptr_mask_ = 0x3;  // The low 2 bits for white|gray|black.
    105 };
    106 
    107 }  // namespace art
    108 
    109 #endif  // ART_RUNTIME_READ_BARRIER_H_
    110