blob: cf1f85d23692916533f36200fffc6312ccfc59d7 [file] [log] [blame]
Ian Rogersef7d42f2014-01-06 12:55:46 -08001/*
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_OBJECT_REFERENCE_H_
18#define ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_
19
David Sehrc431b9d2018-03-02 12:01:51 -080020#include "base/atomic.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070021#include "base/mutex.h" // For Locks::mutator_lock_.
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080022#include "globals.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070023#include "heap_poisoning.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070024#include "obj_ptr.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080025
26namespace art {
27namespace mirror {
28
29class Object;
30
31// Classes shared with the managed side of the world need to be packed so that they don't have
32// extra platform specific padding.
33#define MANAGED PACKED(4)
34
Hans Boehmcc55e1d2017-07-27 15:28:07 -070035template<bool kPoisonReferences, class MirrorType>
36class PtrCompression {
37 public:
38 // Compress reference to its bit representation.
39 static uint32_t Compress(MirrorType* mirror_ptr) {
40 uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr);
41 return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits);
42 }
43
44 // Uncompress an encoded reference from its bit representation.
45 static MirrorType* Decompress(uint32_t ref) {
46 uintptr_t as_bits = kPoisonReferences ? -ref : ref;
47 return reinterpret_cast<MirrorType*>(as_bits);
48 }
49
50 // Convert an ObjPtr to a compressed reference.
51 static uint32_t Compress(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
52 return Compress(ptr.Ptr());
53 }
54};
55
Ian Rogersef7d42f2014-01-06 12:55:46 -080056// Value type representing a reference to a mirror::Object of type MirrorType.
57template<bool kPoisonReferences, class MirrorType>
58class MANAGED ObjectReference {
Hans Boehmcc55e1d2017-07-27 15:28:07 -070059 private:
60 using Compression = PtrCompression<kPoisonReferences, MirrorType>;
61
Ian Rogersef7d42f2014-01-06 12:55:46 -080062 public:
Hans Boehmcc55e1d2017-07-27 15:28:07 -070063 MirrorType* AsMirrorPtr() const {
64 return Compression::Decompress(reference_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080065 }
66
Hans Boehmcc55e1d2017-07-27 15:28:07 -070067 void Assign(MirrorType* other) {
68 reference_ = Compression::Compress(other);
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 }
70
Hans Boehmcc55e1d2017-07-27 15:28:07 -070071 void Assign(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070072
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 void Clear() {
74 reference_ = 0;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070075 DCHECK(IsNull());
76 }
77
78 bool IsNull() const {
79 return reference_ == 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -080080 }
81
82 uint32_t AsVRegValue() const {
83 return reference_;
84 }
85
Hans Boehmcc55e1d2017-07-27 15:28:07 -070086 static ObjectReference<kPoisonReferences, MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
87 REQUIRES_SHARED(Locks::mutator_lock_) {
88 return ObjectReference<kPoisonReferences, MirrorType>(mirror_ptr);
89 }
90
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 protected:
Hans Boehmcc55e1d2017-07-27 15:28:07 -070092 explicit ObjectReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
93 : reference_(Compression::Compress(mirror_ptr)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 }
95
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 // The encoded reference to a mirror::Object.
97 uint32_t reference_;
98};
99
100// References between objects within the managed heap.
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700101// Similar API to ObjectReference, but not a value type. Supports atomic access.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102template<class MirrorType>
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700103class MANAGED HeapReference {
104 private:
105 using Compression = PtrCompression<kPoisonHeapReferences, MirrorType>;
106
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 public:
Mathieu Chartierad8ebb32017-08-09 20:13:18 -0700108 HeapReference() REQUIRES_SHARED(Locks::mutator_lock_) : HeapReference(nullptr) {}
109
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700110 template <bool kIsVolatile = false>
111 MirrorType* AsMirrorPtr() const REQUIRES_SHARED(Locks::mutator_lock_) {
112 return Compression::Decompress(
113 kIsVolatile ? reference_.LoadSequentiallyConsistent() : reference_.LoadJavaData());
114 }
115
116 template <bool kIsVolatile = false>
117 void Assign(MirrorType* other) REQUIRES_SHARED(Locks::mutator_lock_) {
118 if (kIsVolatile) {
119 reference_.StoreSequentiallyConsistent(Compression::Compress(other));
120 } else {
121 reference_.StoreJavaData(Compression::Compress(other));
122 }
123 }
124
125 template <bool kIsVolatile = false>
126 void Assign(ObjPtr<MirrorType> ptr) REQUIRES_SHARED(Locks::mutator_lock_);
127
128 void Clear() {
129 reference_.StoreJavaData(0);
130 DCHECK(IsNull());
131 }
132
133 bool IsNull() const {
134 return reference_.LoadJavaData() == 0;
135 }
136
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 return HeapReference<MirrorType>(mirror_ptr);
140 }
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700141
Hiroshi Yamauchi65f5f242016-12-19 11:44:47 -0800142 bool CasWeakRelaxed(MirrorType* old_ptr, MirrorType* new_ptr)
143 REQUIRES_SHARED(Locks::mutator_lock_);
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700146 explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierad8ebb32017-08-09 20:13:18 -0700147 : reference_(Compression::Compress(mirror_ptr)) {}
Hans Boehmcc55e1d2017-07-27 15:28:07 -0700148
149 // The encoded reference to a mirror::Object. Atomically updateable.
150 Atomic<uint32_t> reference_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151};
152
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700153static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
154 "heap reference size does not match");
155
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700156// Standard compressed reference used in the runtime. Used for StackReference and GC roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700157template<class MirrorType>
158class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> {
159 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700161 : mirror::ObjectReference<false, MirrorType>(nullptr) {}
162
163 static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700164 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700165 return CompressedReference<MirrorType>(p);
166 }
167
168 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700169 explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700170 : mirror::ObjectReference<false, MirrorType>(p) {}
171};
172
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173} // namespace mirror
174} // namespace art
175
176#endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_