Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 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 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 20 | #include "base/mutex.h" |
| 21 | #include "base/macros.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 22 | #include "jni.h" |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 23 | #include "mirror/object_reference.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 24 | #include "offsets.h" |
| 25 | #include "read_barrier_c.h" |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 26 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 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. |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 29 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 30 | namespace art { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 31 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 32 | namespace mirror { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 33 | class ArtMethod; |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 34 | class Object; |
| 35 | template<typename MirrorType> class HeapReference; |
| 36 | } // namespace mirror |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 37 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 38 | class ReadBarrier { |
| 39 | public: |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 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 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 46 | // It's up to the implementation whether the given field gets |
| 47 | // updated whereas the return value must be an updated reference. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 48 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier, |
| 49 | bool kMaybeDuringStartup = false> |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 50 | ALWAYS_INLINE static MirrorType* Barrier( |
| 51 | mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) |
| 52 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 53 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 54 | // It's up to the implementation whether the given root gets updated |
| 55 | // whereas the return value must be an updated reference. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 56 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier, |
| 57 | bool kMaybeDuringStartup = false> |
Hiroshi Yamauchi | a91a4bc | 2014-06-13 16:44:55 -0700 | [diff] [blame] | 58 | ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root) |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 59 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 60 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 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 | bool kMaybeDuringStartup = false> |
| 65 | ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root) |
| 66 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 67 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 68 | static bool IsDuringStartup(); |
| 69 | |
| 70 | // Without the holder object. |
| 71 | static void AssertToSpaceInvariant(mirror::Object* ref) |
| 72 | SHARED_LOCKS_REQUIRED(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_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 79 | |
| 80 | static mirror::Object* Mark(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 81 | |
| 82 | static mirror::Object* WhitePtr() { |
| 83 | return reinterpret_cast<mirror::Object*>(white_ptr_); |
| 84 | } |
| 85 | static mirror::Object* GrayPtr() { |
| 86 | return reinterpret_cast<mirror::Object*>(gray_ptr_); |
| 87 | } |
| 88 | static mirror::Object* BlackPtr() { |
| 89 | return reinterpret_cast<mirror::Object*>(black_ptr_); |
| 90 | } |
| 91 | |
| 92 | ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj, |
| 93 | uintptr_t* out_rb_ptr_high_bits) |
| 94 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 95 | |
| 96 | // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them. |
| 97 | static constexpr uintptr_t white_ptr_ = 0x0; // Not marked. |
| 98 | static constexpr uintptr_t gray_ptr_ = 0x1; // Marked, but not marked through. On mark stack. |
| 99 | static constexpr uintptr_t black_ptr_ = 0x2; // Marked through. Used for non-moving objects. |
| 100 | static constexpr uintptr_t rb_ptr_mask_ = 0x3; // The low 2 bits for white|gray|black. |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 101 | }; |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 102 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 103 | } // namespace art |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 104 | |
| 105 | #endif // ART_RUNTIME_READ_BARRIER_H_ |