Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 20 | #include "base/mutex.h" // For Locks::mutator_lock_. |
Hiroshi Yamauchi | e63a745 | 2014-02-27 14:44:36 -0800 | [diff] [blame] | 21 | #include "globals.h" |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 22 | #include "obj_ptr.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | namespace mirror { |
| 26 | |
| 27 | class 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. |
| 34 | template<bool kPoisonReferences, class MirrorType> |
| 35 | class MANAGED ObjectReference { |
| 36 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 37 | MirrorType* AsMirrorPtr() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 38 | return UnCompress(); |
| 39 | } |
| 40 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 41 | void Assign(MirrorType* other) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 42 | reference_ = Compress(other); |
| 43 | } |
| 44 | |
Mathieu Chartier | 1a5337f | 2016-10-13 13:48:23 -0700 | [diff] [blame] | 45 | void Assign(ObjPtr<MirrorType> ptr) |
| 46 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 47 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 48 | void Clear() { |
| 49 | reference_ = 0; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 50 | DCHECK(IsNull()); |
| 51 | } |
| 52 | |
| 53 | bool IsNull() const { |
| 54 | return reference_ == 0; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | uint32_t AsVRegValue() const { |
| 58 | return reference_; |
| 59 | } |
| 60 | |
| 61 | protected: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 62 | explicit ObjectReference(MirrorType* mirror_ptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 63 | REQUIRES_SHARED(Locks::mutator_lock_) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 64 | : reference_(Compress(mirror_ptr)) { |
| 65 | } |
| 66 | |
| 67 | // Compress reference to its bit representation. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 68 | static uint32_t Compress(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 69 | uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr); |
| 70 | return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits); |
| 71 | } |
| 72 | |
| 73 | // Uncompress an encoded reference from its bit representation. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 74 | MirrorType* UnCompress() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 75 | uintptr_t as_bits = kPoisonReferences ? -reference_ : reference_; |
| 76 | return reinterpret_cast<MirrorType*>(as_bits); |
| 77 | } |
| 78 | |
| 79 | friend class Object; |
| 80 | |
| 81 | // The encoded reference to a mirror::Object. |
| 82 | uint32_t reference_; |
| 83 | }; |
| 84 | |
| 85 | // References between objects within the managed heap. |
| 86 | template<class MirrorType> |
Hiroshi Yamauchi | e63a745 | 2014-02-27 14:44:36 -0800 | [diff] [blame] | 87 | class MANAGED HeapReference : public ObjectReference<kPoisonHeapReferences, MirrorType> { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 88 | public: |
| 89 | static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 91 | return HeapReference<MirrorType>(mirror_ptr); |
| 92 | } |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 93 | |
| 94 | static HeapReference<MirrorType> FromObjPtr(ObjPtr<MirrorType> ptr) |
| 95 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 96 | |
Hiroshi Yamauchi | 65f5f24 | 2016-12-19 11:44:47 -0800 | [diff] [blame] | 97 | bool CasWeakRelaxed(MirrorType* old_ptr, MirrorType* new_ptr) |
| 98 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 99 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 100 | private: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 101 | explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) |
Hiroshi Yamauchi | e63a745 | 2014-02-27 14:44:36 -0800 | [diff] [blame] | 102 | : ObjectReference<kPoisonHeapReferences, MirrorType>(mirror_ptr) {} |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
Mathieu Chartier | a058fdf | 2016-10-06 15:13:58 -0700 | [diff] [blame] | 105 | static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize, |
| 106 | "heap reference size does not match"); |
| 107 | |
Mathieu Chartier | d3ed9a3 | 2015-04-10 14:23:35 -0700 | [diff] [blame] | 108 | // Standard compressed reference used in the runtime. Used for StackReference and GC roots. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 109 | template<class MirrorType> |
| 110 | class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> { |
| 111 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 112 | CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 113 | : mirror::ObjectReference<false, MirrorType>(nullptr) {} |
| 114 | |
| 115 | static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 116 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 117 | return CompressedReference<MirrorType>(p); |
| 118 | } |
| 119 | |
| 120 | private: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 121 | explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 122 | : mirror::ObjectReference<false, MirrorType>(p) {} |
| 123 | }; |
| 124 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 125 | } // namespace mirror |
| 126 | } // namespace art |
| 127 | |
| 128 | #endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_ |