blob: 7345448ed718a50ab48363ae40302bc10bff7046 [file] [log] [blame]
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -07001/*
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 Shih4ee7a662014-07-11 09:59:27 -070020#include "class.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "gc_root.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070022#include "object.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070023#include "object_callbacks.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070024#include "read_barrier_option.h"
Fred Shih4ee7a662014-07-11 09:59:27 -070025#include "thread.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070026
27namespace art {
28
Mathieu Chartier308351a2014-06-15 12:39:02 -070029namespace gc {
30
31class ReferenceProcessor;
32class ReferenceQueue;
33
34} // namespace gc
35
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070036struct ReferenceOffsets;
37struct FinalizerReferenceOffsets;
38
39namespace mirror {
40
41// C++ mirror of java.lang.ref.Reference
42class MANAGED Reference : public Object {
43 public:
Fred Shih4ee7a662014-07-11 09:59:27 -070044 // Size of java.lang.ref.Reference.class.
45 static uint32_t ClassSize();
46
47 // Size of an instance of java.lang.ref.Reference.
48 static constexpr uint32_t InstanceSize() {
49 return sizeof(Reference);
50 }
51
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070052 static MemberOffset PendingNextOffset() {
53 return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_);
54 }
55 static MemberOffset QueueOffset() {
56 return OFFSET_OF_OBJECT_MEMBER(Reference, queue_);
57 }
58 static MemberOffset QueueNextOffset() {
59 return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_);
60 }
61 static MemberOffset ReferentOffset() {
62 return OFFSET_OF_OBJECT_MEMBER(Reference, referent_);
63 }
Hiroshi Yamauchibfff21a2014-05-09 12:21:15 -070064 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070065 Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchibfff21a2014-05-09 12:21:15 -070066 return GetFieldObjectVolatile<Object, kDefaultVerifyFlags, kReadBarrierOption>(
67 ReferentOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070068 }
69 template<bool kTransactionActive>
70 void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070071 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), referent);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070072 }
73 template<bool kTransactionActive>
74 void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070075 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070076 }
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070077 // Volatile read/write is not necessary since the java pending next is only accessed from
78 // the java threads for cleared references. Once these cleared references have a null referent,
79 // we never end up reading their pending next from the GC again.
80 Reference* GetPendingNext() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070081 return GetFieldObject<Reference>(PendingNextOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070082 }
83 template<bool kTransactionActive>
84 void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070085 SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070086 }
87
88 bool IsEnqueued() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
89 // Since the references are stored as cyclic lists it means that once enqueued, the pending
90 // next is always non-null.
91 return GetPendingNext() != nullptr;
92 }
93
94 bool IsEnqueuable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
95
Fred Shih4ee7a662014-07-11 09:59:27 -070096 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
97 static Class* GetJavaLangRefReference() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070098 DCHECK(!java_lang_ref_Reference_.IsNull());
99 return java_lang_ref_Reference_.Read<kReadBarrierOption>();
Fred Shih4ee7a662014-07-11 09:59:27 -0700100 }
101 static void SetClass(Class* klass);
102 static void ResetClass(void);
103 static void VisitRoots(RootCallback* callback, void* arg);
104
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700105 private:
Mathieu Chartier308351a2014-06-15 12:39:02 -0700106 // Note: This avoids a read barrier, it should only be used by the GC.
107 HeapReference<Object>* GetReferentReferenceAddr() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
108 return GetFieldObjectReferenceAddr<kDefaultVerifyFlags>(ReferentOffset());
109 }
110
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700111 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
112 HeapReference<Reference> pending_next_; // Note this is Java volatile:
113 HeapReference<Object> queue_; // Note this is Java volatile:
114 HeapReference<Reference> queue_next_; // Note this is Java volatile:
115 HeapReference<Object> referent_; // Note this is Java volatile:
116
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700117 static GcRoot<Class> java_lang_ref_Reference_;
Fred Shih4ee7a662014-07-11 09:59:27 -0700118
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700119 friend struct art::ReferenceOffsets; // for verifying offset information
Mathieu Chartier308351a2014-06-15 12:39:02 -0700120 friend class gc::ReferenceProcessor;
121 friend class gc::ReferenceQueue;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700122 DISALLOW_IMPLICIT_CONSTRUCTORS(Reference);
123};
124
125// C++ mirror of java.lang.ref.FinalizerReference
126class MANAGED FinalizerReference : public Reference {
127 public:
128 static MemberOffset ZombieOffset() {
129 return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_);
130 }
131
132 template<bool kTransactionActive>
133 void SetZombie(Object* zombie) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700134 return SetFieldObjectVolatile<kTransactionActive>(ZombieOffset(), zombie);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700135 }
136 Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700137 return GetFieldObjectVolatile<Object>(ZombieOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700138 }
139
140 private:
141 HeapReference<FinalizerReference> next_;
142 HeapReference<FinalizerReference> prev_;
143 HeapReference<Object> zombie_;
144
145 friend struct art::FinalizerReferenceOffsets; // for verifying offset information
146 DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference);
147};
148
149} // namespace mirror
150} // namespace art
151
152#endif // ART_RUNTIME_MIRROR_REFERENCE_H_