blob: 573cb308bdc0213506c51dc5a0792f1c1e3ab90a [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
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070020#include "base/mutex.h" // For Locks::mutator_lock_.
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080021#include "globals.h"
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070022#include "obj_ptr.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080023
24namespace art {
25namespace mirror {
26
27class Object;
28
29// Classes shared with the managed side of the world need to be packed so that they don't have
30// extra platform specific padding.
31#define MANAGED PACKED(4)
32
33// Value type representing a reference to a mirror::Object of type MirrorType.
34template<bool kPoisonReferences, class MirrorType>
35class MANAGED ObjectReference {
36 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070037 MirrorType* AsMirrorPtr() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080038 return UnCompress();
39 }
40
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070041 void Assign(MirrorType* other) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080042 reference_ = Compress(other);
43 }
44
45 void Clear() {
46 reference_ = 0;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070047 DCHECK(IsNull());
48 }
49
50 bool IsNull() const {
51 return reference_ == 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 }
53
54 uint32_t AsVRegValue() const {
55 return reference_;
56 }
57
58 protected:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070059 explicit ObjectReference(MirrorType* mirror_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 : reference_(Compress(mirror_ptr)) {
62 }
63
64 // Compress reference to its bit representation.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 static uint32_t Compress(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080066 uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr);
67 return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits);
68 }
69
70 // Uncompress an encoded reference from its bit representation.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070071 MirrorType* UnCompress() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 uintptr_t as_bits = kPoisonReferences ? -reference_ : reference_;
73 return reinterpret_cast<MirrorType*>(as_bits);
74 }
75
76 friend class Object;
77
78 // The encoded reference to a mirror::Object.
79 uint32_t reference_;
80};
81
82// References between objects within the managed heap.
83template<class MirrorType>
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080084class MANAGED HeapReference : public ObjectReference<kPoisonHeapReferences, MirrorType> {
Ian Rogersef7d42f2014-01-06 12:55:46 -080085 public:
86 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070087 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 return HeapReference<MirrorType>(mirror_ptr);
89 }
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070090
91 static HeapReference<MirrorType> FromObjPtr(ObjPtr<MirrorType> ptr)
92 REQUIRES_SHARED(Locks::mutator_lock_);
93
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070095 explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080096 : ObjectReference<kPoisonHeapReferences, MirrorType>(mirror_ptr) {}
Ian Rogersef7d42f2014-01-06 12:55:46 -080097};
98
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070099static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
100 "heap reference size does not match");
101
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700102// Standard compressed reference used in the runtime. Used for StackReference and GC roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700103template<class MirrorType>
104class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> {
105 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700107 : mirror::ObjectReference<false, MirrorType>(nullptr) {}
108
109 static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700111 return CompressedReference<MirrorType>(p);
112 }
113
114 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700115 explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700116 : mirror::ObjectReference<false, MirrorType>(p) {}
117};
118
Ian Rogersef7d42f2014-01-06 12:55:46 -0800119} // namespace mirror
120} // namespace art
121
122#endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_