blob: c2a83ff85510c8159e34c34352a45abef995108c [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
20#include "object.h"
21
22namespace art {
23
24struct ReferenceOffsets;
25struct FinalizerReferenceOffsets;
26
27namespace mirror {
28
29// C++ mirror of java.lang.ref.Reference
30class MANAGED Reference : public Object {
31 public:
32 static MemberOffset PendingNextOffset() {
33 return OFFSET_OF_OBJECT_MEMBER(Reference, pending_next_);
34 }
35 static MemberOffset QueueOffset() {
36 return OFFSET_OF_OBJECT_MEMBER(Reference, queue_);
37 }
38 static MemberOffset QueueNextOffset() {
39 return OFFSET_OF_OBJECT_MEMBER(Reference, queue_next_);
40 }
41 static MemberOffset ReferentOffset() {
42 return OFFSET_OF_OBJECT_MEMBER(Reference, referent_);
43 }
44
45 Object* GetReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
46 return GetFieldObject<Object>(ReferentOffset(), true);
47 }
48 template<bool kTransactionActive>
49 void SetReferent(Object* referent) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
50 SetFieldObject<kTransactionActive>(ReferentOffset(), referent, true);
51 }
52 template<bool kTransactionActive>
53 void ClearReferent() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
54 SetFieldObject<kTransactionActive>(ReferentOffset(), nullptr, true);
55 }
56
57 // Volatile read/write is not necessary since the java pending next is only accessed from
58 // the java threads for cleared references. Once these cleared references have a null referent,
59 // we never end up reading their pending next from the GC again.
60 Reference* GetPendingNext() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
61 return GetFieldObject<Reference>(PendingNextOffset(), false);
62 }
63 template<bool kTransactionActive>
64 void SetPendingNext(Reference* pending_next) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 SetFieldObject<kTransactionActive>(PendingNextOffset(), pending_next, false);
66 }
67
68 bool IsEnqueued() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
69 // Since the references are stored as cyclic lists it means that once enqueued, the pending
70 // next is always non-null.
71 return GetPendingNext() != nullptr;
72 }
73
74 bool IsEnqueuable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
75
76 private:
77 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
78 HeapReference<Reference> pending_next_; // Note this is Java volatile:
79 HeapReference<Object> queue_; // Note this is Java volatile:
80 HeapReference<Reference> queue_next_; // Note this is Java volatile:
81 HeapReference<Object> referent_; // Note this is Java volatile:
82
83 friend struct art::ReferenceOffsets; // for verifying offset information
84 DISALLOW_IMPLICIT_CONSTRUCTORS(Reference);
85};
86
87// C++ mirror of java.lang.ref.FinalizerReference
88class MANAGED FinalizerReference : public Reference {
89 public:
90 static MemberOffset ZombieOffset() {
91 return OFFSET_OF_OBJECT_MEMBER(FinalizerReference, zombie_);
92 }
93
94 template<bool kTransactionActive>
95 void SetZombie(Object* zombie) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
96 return SetFieldObject<kTransactionActive>(ZombieOffset(), zombie, true);
97 }
98 Object* GetZombie() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 return GetFieldObject<Object>(ZombieOffset(), true);
100 }
101
102 private:
103 HeapReference<FinalizerReference> next_;
104 HeapReference<FinalizerReference> prev_;
105 HeapReference<Object> zombie_;
106
107 friend struct art::FinalizerReferenceOffsets; // for verifying offset information
108 DISALLOW_IMPLICIT_CONSTRUCTORS(FinalizerReference);
109};
110
111} // namespace mirror
112} // namespace art
113
114#endif // ART_RUNTIME_MIRROR_REFERENCE_H_