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 | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 22 | #include "gc_root.h" |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 23 | #include "jni.h" |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 24 | #include "mirror/object_reference.h" |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 25 | #include "offsets.h" |
| 26 | #include "read_barrier_c.h" |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 27 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 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. |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 30 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | namespace mirror { |
| 33 | class Object; |
| 34 | template<typename MirrorType> class HeapReference; |
| 35 | } // namespace mirror |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | class ArtMethod; |
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 | // Enable the to-space invariant checks. |
Hiroshi Yamauchi | daf61a1 | 2016-06-10 14:27:38 -0700 | [diff] [blame] | 41 | static constexpr bool kEnableToSpaceInvariantChecks = kIsDebugBuild; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 42 | // Enable the read barrier checks. |
Hiroshi Yamauchi | daf61a1 | 2016-06-10 14:27:38 -0700 | [diff] [blame] | 43 | static constexpr bool kEnableReadBarrierInvariantChecks = kIsDebugBuild; |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 44 | |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 45 | // It's up to the implementation whether the given field gets updated whereas the return value |
| 46 | // must be an updated reference unless kAlwaysUpdateField is true. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 47 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier, |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 48 | bool kAlwaysUpdateField = false> |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 49 | ALWAYS_INLINE static MirrorType* Barrier( |
| 50 | mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 51 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 4cba0d9 | 2014-05-21 21:10:23 -0700 | [diff] [blame] | 52 | |
Hiroshi Yamauchi | ea2e1bd | 2014-06-18 13:47:35 -0700 | [diff] [blame] | 53 | // It's up to the implementation whether the given root gets updated |
| 54 | // whereas the return value must be an updated reference. |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 55 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 56 | ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root, |
| 57 | GcRootSource* gc_root_source = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 58 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 59 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 60 | // It's up to the implementation whether the given root gets updated |
| 61 | // whereas the return value must be an updated reference. |
Hiroshi Yamauchi | cc78f3f | 2015-12-11 15:51:04 -0800 | [diff] [blame] | 62 | template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 63 | ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root, |
| 64 | GcRootSource* gc_root_source = nullptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 65 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 66 | |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 67 | static bool IsDuringStartup(); |
| 68 | |
| 69 | // Without the holder object. |
| 70 | static void AssertToSpaceInvariant(mirror::Object* ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 71 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 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) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 77 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 3f64f25 | 2015-06-12 18:35:06 -0700 | [diff] [blame] | 78 | // With GcRootSource. |
| 79 | static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 80 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 81 | |
Hiroshi Yamauchi | 2e5de78 | 2016-01-29 12:06:36 -0800 | [diff] [blame] | 82 | // ALWAYS_INLINE on this caused a performance regression b/26744236. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 83 | static mirror::Object* Mark(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 84 | |
| 85 | static mirror::Object* WhitePtr() { |
| 86 | return reinterpret_cast<mirror::Object*>(white_ptr_); |
| 87 | } |
| 88 | static mirror::Object* GrayPtr() { |
| 89 | return reinterpret_cast<mirror::Object*>(gray_ptr_); |
| 90 | } |
| 91 | static mirror::Object* BlackPtr() { |
| 92 | return reinterpret_cast<mirror::Object*>(black_ptr_); |
| 93 | } |
| 94 | |
| 95 | ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj, |
| 96 | uintptr_t* out_rb_ptr_high_bits) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame^] | 97 | REQUIRES_SHARED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 98 | |
| 99 | // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them. |
| 100 | static constexpr uintptr_t white_ptr_ = 0x0; // Not marked. |
| 101 | static constexpr uintptr_t gray_ptr_ = 0x1; // Marked, but not marked through. On mark stack. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 102 | // TODO: black_ptr_ is unused, we should remove it. |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 103 | static constexpr uintptr_t black_ptr_ = 0x2; // Marked through. Used for non-moving objects. |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 104 | static constexpr uintptr_t rb_ptr_mask_ = 0x1; // The low bits for white|gray. |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 105 | }; |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 106 | |
Hiroshi Yamauchi | 800ac2d | 2014-04-02 17:32:54 -0700 | [diff] [blame] | 107 | } // namespace art |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 108 | |
| 109 | #endif // ART_RUNTIME_READ_BARRIER_H_ |