Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -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_MIRROR_REFERENCE_H_ |
| 18 | #define ART_RUNTIME_MIRROR_REFERENCE_H_ |
| 19 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 20 | #include "class.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 21 | #include "gc_root.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 22 | #include "object.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 23 | #include "object_callbacks.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 24 | #include "read_barrier_option.h" |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 25 | #include "runtime.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 26 | #include "thread.h" |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 30 | namespace gc { |
| 31 | |
| 32 | class ReferenceProcessor; |
| 33 | class ReferenceQueue; |
| 34 | |
| 35 | } // namespace gc |
| 36 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 37 | struct ReferenceOffsets; |
| 38 | struct FinalizerReferenceOffsets; |
| 39 | |
| 40 | namespace mirror { |
| 41 | |
| 42 | // C++ mirror of java.lang.ref.Reference |
| 43 | class MANAGED Reference : public Object { |
| 44 | public: |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 45 | // Size of java.lang.ref.Reference.class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 46 | static uint32_t ClassSize(size_t pointer_size); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 47 | |
| 48 | // Size of an instance of java.lang.ref.Reference. |
| 49 | static constexpr uint32_t InstanceSize() { |
| 50 | return sizeof(Reference); |
| 51 | } |
| 52 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 53 | static MemberOffset PendingNextOffset() { |
| 54 | return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_); |
| 55 | } |
| 56 | static MemberOffset QueueOffset() { |
| 57 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_); |
| 58 | } |
| 59 | static MemberOffset QueueNextOffset() { |
| 60 | return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_); |
| 61 | } |
| 62 | static MemberOffset ReferentOffset() { |
| 63 | return OFFSET_OF_OBJECT_MEMBER(Reference, referent_); |
| 64 | } |
Hiroshi Yamauchi | bfff21a | 2014-05-09 12:21:15 -0700 | [diff] [blame] | 65 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 66 | Object* GetReferent() SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | bfff21a | 2014-05-09 12:21:15 -0700 | [diff] [blame] | 67 | return GetFieldObjectVolatile<Object, kDefaultVerifyFlags, kReadBarrierOption>( |
| 68 | ReferentOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 69 | } |
| 70 | template<bool kTransactionActive> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 71 | void SetReferent(Object* referent) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 72 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 73 | } |
| 74 | template<bool kTransactionActive> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 75 | void ClearReferent() SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 76 | SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 77 | } |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 78 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 79 | Reference* GetPendingNext() SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 80 | return GetFieldObject<Reference>(PendingNextOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 81 | } |
Richard Uhler | 522d51b | 2016-01-22 14:18:57 -0800 | [diff] [blame] | 82 | |
| 83 | void SetPendingNext(Reference* pending_next) |
| 84 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 85 | if (Runtime::Current()->IsActiveTransaction()) { |
| 86 | SetFieldObject<true>(PendingNextOffset(), pending_next); |
| 87 | } else { |
| 88 | SetFieldObject<false>(PendingNextOffset(), pending_next); |
| 89 | } |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 92 | // Returns true if the reference's pendingNext is null, indicating it is |
| 93 | // okay to process this reference. |
| 94 | // |
| 95 | // If pendingNext is not null, then one of the following cases holds: |
| 96 | // 1. The reference has already been enqueued to a java ReferenceQueue. In |
| 97 | // this case the referent should not be considered for reference processing |
| 98 | // ever again. |
| 99 | // 2. The reference is currently part of a list of references that may |
| 100 | // shortly be enqueued on a java ReferenceQueue. In this case the reference |
| 101 | // should not be processed again until and unless the reference has been |
| 102 | // removed from the list after having determined the reference is not ready |
| 103 | // to be enqueued on a java ReferenceQueue. |
| 104 | bool IsUnprocessed() SHARED_REQUIRES(Locks::mutator_lock_) { |
| 105 | return GetPendingNext() == nullptr; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 108 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 109 | static Class* GetJavaLangRefReference() SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 110 | DCHECK(!java_lang_ref_Reference_.IsNull()); |
| 111 | return java_lang_ref_Reference_.Read<kReadBarrierOption>(); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 112 | } |
| 113 | static void SetClass(Class* klass); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 114 | static void ResetClass(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 115 | static void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 116 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 117 | private: |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 118 | // Note: This avoids a read barrier, it should only be used by the GC. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | HeapReference<Object>* GetReferentReferenceAddr() SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 120 | return GetFieldObjectReferenceAddr<kDefaultVerifyFlags>(ReferentOffset()); |
| 121 | } |
| 122 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 123 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Richard Uhler | c4695df | 2016-01-15 14:08:05 -0800 | [diff] [blame] | 124 | HeapReference<Reference> pending_next_; |
| 125 | HeapReference<Object> queue_; |
| 126 | HeapReference<Reference> queue_next_; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 127 | HeapReference<Object> referent_; // Note this is Java volatile: |
| 128 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 129 | static GcRoot<Class> java_lang_ref_Reference_; |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 130 | |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 131 | friend struct art::ReferenceOffsets; // for verifying offset information |
Mathieu Chartier | 308351a | 2014-06-15 12:39:02 -0700 | [diff] [blame] | 132 | friend class gc::ReferenceProcessor; |
| 133 | friend class gc::ReferenceQueue; |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 134 | DISALLOW_IMPLICIT_CONSTRUCTORS(Reference); |
| 135 | }; |
| 136 | |
| 137 | // C++ mirror of java.lang.ref.FinalizerReference |
| 138 | class MANAGED FinalizerReference : public Reference { |
| 139 | public: |
| 140 | static MemberOffset ZombieOffset() { |
| 141 | return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_); |
| 142 | } |
| 143 | |
| 144 | template<bool kTransactionActive> |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 145 | void SetZombie(Object* zombie) SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 146 | return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 147 | } |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 148 | Object* GetZombie() SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 149 | return GetFieldObjectVolatile<Object>(ZombieOffset()); |
Mathieu Chartier | 8fa2dad | 2014-03-13 12:22:56 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | private: |
| 153 | HeapReference<FinalizerReference> next_; |
| 154 | HeapReference<FinalizerReference> prev_; |
| 155 | HeapReference<Object> zombie_; |
| 156 | |
| 157 | friend struct art::FinalizerReferenceOffsets; // for verifying offset information |
| 158 | DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference); |
| 159 | }; |
| 160 | |
| 161 | } // namespace mirror |
| 162 | } // namespace art |
| 163 | |
| 164 | #endif // ART_RUNTIME_MIRROR_REFERENCE_H_ |