• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include "base/enums.h"
21 #include "base/locks.h"
22 #include "base/macros.h"
23 #include "obj_ptr.h"
24 #include "object.h"
25 #include "read_barrier_option.h"
26 
27 namespace art {
28 
29 namespace gc {
30 
31 class ReferenceProcessor;
32 class ReferenceQueue;
33 
34 }  // namespace gc
35 
36 struct ReferenceOffsets;
37 struct FinalizerReferenceOffsets;
38 
39 namespace mirror {
40 
41 // C++ mirror of java.lang.ref.Reference
42 class MANAGED Reference : public Object {
43  public:
44   MIRROR_CLASS("Ljava/lang/ref/Reference;");
45 
46   // Size of java.lang.ref.Reference.class.
47   static uint32_t ClassSize(PointerSize pointer_size);
48 
49   // Size of an instance of java.lang.ref.Reference.
InstanceSize()50   static constexpr uint32_t InstanceSize() {
51     return sizeof(Reference);
52   }
53 
PendingNextOffset()54   static MemberOffset PendingNextOffset() {
55     return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_);
56   }
QueueOffset()57   static MemberOffset QueueOffset() {
58     return OFFSET_OF_OBJECT_MEMBER(Reference, queue_);
59   }
QueueNextOffset()60   static MemberOffset QueueNextOffset() {
61     return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_);
62   }
ReferentOffset()63   static MemberOffset ReferentOffset() {
64     return OFFSET_OF_OBJECT_MEMBER(Reference, referent_);
65   }
66   template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
GetReferent()67   Object* GetReferent() REQUIRES_SHARED(Locks::mutator_lock_) {
68     return GetFieldObjectVolatile<Object, kDefaultVerifyFlags, kReadBarrierOption>(
69         ReferentOffset());
70   }
71   template<bool kTransactionActive>
72   void SetReferent(ObjPtr<Object> referent) REQUIRES_SHARED(Locks::mutator_lock_);
73   template<bool kTransactionActive>
ClearReferent()74   void ClearReferent() REQUIRES_SHARED(Locks::mutator_lock_) {
75     SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr);
76   }
77 
78   template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
GetPendingNext()79   Reference* GetPendingNext() REQUIRES_SHARED(Locks::mutator_lock_) {
80     return GetFieldObject<Reference, kDefaultVerifyFlags, kReadBarrierOption>(PendingNextOffset());
81   }
82 
83   void SetPendingNext(ObjPtr<Reference> pending_next) REQUIRES_SHARED(Locks::mutator_lock_);
84 
85   // Returns true if the reference's pendingNext is null, indicating it is
86   // okay to process this reference.
87   //
88   // If pendingNext is not null, then one of the following cases holds:
89   // 1. The reference has already been enqueued to a java ReferenceQueue. In
90   // this case the referent should not be considered for reference processing
91   // ever again.
92   // 2. The reference is currently part of a list of references that may
93   // shortly be enqueued on a java ReferenceQueue. In this case the reference
94   // should not be processed again until and unless the reference has been
95   // removed from the list after having determined the reference is not ready
96   // to be enqueued on a java ReferenceQueue.
IsUnprocessed()97   bool IsUnprocessed() REQUIRES_SHARED(Locks::mutator_lock_) {
98     return GetPendingNext<kWithoutReadBarrier>() == nullptr;
99   }
100 
101  private:
102   // Note: This avoids a read barrier, it should only be used by the GC.
GetReferentReferenceAddr()103   HeapReference<Object>* GetReferentReferenceAddr() REQUIRES_SHARED(Locks::mutator_lock_) {
104     return GetFieldObjectReferenceAddr<kDefaultVerifyFlags>(ReferentOffset());
105   }
106 
107   // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
108   HeapReference<Reference> pending_next_;
109   HeapReference<Object> queue_;
110   HeapReference<Reference> queue_next_;
111   HeapReference<Object> referent_;  // Note this is Java volatile:
112 
113   friend struct art::ReferenceOffsets;  // for verifying offset information
114   friend class gc::ReferenceProcessor;
115   friend class gc::ReferenceQueue;
116   DISALLOW_IMPLICIT_CONSTRUCTORS(Reference);
117 };
118 
119 // C++ mirror of java.lang.ref.FinalizerReference
120 class MANAGED FinalizerReference : public Reference {
121  public:
122   MIRROR_CLASS("Ljava/lang/ref/FinalizerReference;");
123 
ZombieOffset()124   static MemberOffset ZombieOffset() {
125     return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_);
126   }
127 
128   template<bool kTransactionActive>
129   void SetZombie(ObjPtr<Object> zombie) REQUIRES_SHARED(Locks::mutator_lock_);
130 
GetZombie()131   Object* GetZombie() REQUIRES_SHARED(Locks::mutator_lock_) {
132     return GetFieldObjectVolatile<Object>(ZombieOffset());
133   }
134 
135  private:
136   HeapReference<FinalizerReference> next_;
137   HeapReference<FinalizerReference> prev_;
138   HeapReference<Object> zombie_;
139 
140   friend struct art::FinalizerReferenceOffsets;  // for verifying offset information
141   DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference);
142 };
143 
144 }  // namespace mirror
145 }  // namespace art
146 
147 #endif  // ART_RUNTIME_MIRROR_REFERENCE_H_
148