blob: 63c5ae5ff884ed4d1da9c57d2d2d3a8da337f3c3 [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
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070021#include "base/macros.h"
22#include "base/mutex.h"
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070023#include "obj_ptr.h"
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070024#include "object.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070025#include "read_barrier_option.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.
Andreas Gampe542451c2016-07-26 09:02:02 -070045 static uint32_t ClassSize(PointerSize pointer_size);
Fred Shih4ee7a662014-07-11 09:59:27 -070046
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>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 Object* GetReferent() REQUIRES_SHARED(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>
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070070 void SetReferent(ObjPtr<Object> referent) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070071 template<bool kTransactionActive>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 void ClearReferent() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070073 SetFieldObjectVolatile<kTransactionActive>(ReferentOffset(), nullptr);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070074 }
Richard Uhlerc4695df2016-01-15 14:08:05 -080075
Mathieu Chartier5ffa0782016-07-27 10:45:47 -070076 template <ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070077 Reference* GetPendingNext() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier5ffa0782016-07-27 10:45:47 -070078 return GetFieldObject<Reference, kDefaultVerifyFlags, kReadBarrierOption>(PendingNextOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070079 }
Richard Uhler522d51b2016-01-22 14:18:57 -080080
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -070081 void SetPendingNext(ObjPtr<Reference> pending_next) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070082
Richard Uhlerc4695df2016-01-15 14:08:05 -080083 // Returns true if the reference's pendingNext is null, indicating it is
84 // okay to process this reference.
85 //
86 // If pendingNext is not null, then one of the following cases holds:
87 // 1. The reference has already been enqueued to a java ReferenceQueue. In
88 // this case the referent should not be considered for reference processing
89 // ever again.
90 // 2. The reference is currently part of a list of references that may
91 // shortly be enqueued on a java ReferenceQueue. In this case the reference
92 // should not be processed again until and unless the reference has been
93 // removed from the list after having determined the reference is not ready
94 // to be enqueued on a java ReferenceQueue.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070095 bool IsUnprocessed() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier5ffa0782016-07-27 10:45:47 -070096 return GetPendingNext<kWithoutReadBarrier>() == nullptr;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070097 }
98
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -070099 private:
Mathieu Chartier308351a2014-06-15 12:39:02 -0700100 // Note: This avoids a read barrier, it should only be used by the GC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700101 HeapReference<Object>* GetReferentReferenceAddr() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier308351a2014-06-15 12:39:02 -0700102 return GetFieldObjectReferenceAddr<kDefaultVerifyFlags>(ReferentOffset());
103 }
104
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700105 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Richard Uhlerc4695df2016-01-15 14:08:05 -0800106 HeapReference<Reference> pending_next_;
107 HeapReference<Object> queue_;
108 HeapReference<Reference> queue_next_;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700109 HeapReference<Object> referent_; // Note this is Java volatile:
110
111 friend struct art::ReferenceOffsets; // for verifying offset information
Mathieu Chartier308351a2014-06-15 12:39:02 -0700112 friend class gc::ReferenceProcessor;
113 friend class gc::ReferenceQueue;
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700114 DISALLOW_IMPLICIT_CONSTRUCTORS(Reference);
115};
116
117// C++ mirror of java.lang.ref.FinalizerReference
118class MANAGED FinalizerReference : public Reference {
119 public:
120 static MemberOffset ZombieOffset() {
121 return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_);
122 }
123
124 template<bool kTransactionActive>
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -0700125 void SetZombie(ObjPtr<Object> zombie) REQUIRES_SHARED(Locks::mutator_lock_);
126
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700127 Object* GetZombie() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700128 return GetFieldObjectVolatile<Object>(ZombieOffset());
Mathieu Chartier8fa2dad2014-03-13 12:22:56 -0700129 }
130
131 private:
132 HeapReference<FinalizerReference> next_;
133 HeapReference<FinalizerReference> prev_;
134 HeapReference<Object> zombie_;
135
136 friend struct art::FinalizerReferenceOffsets; // for verifying offset information
137 DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference);
138};
139
140} // namespace mirror
141} // namespace art
142
143#endif // ART_RUNTIME_MIRROR_REFERENCE_H_