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