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" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | namespace mirror { |
| 25 | |
| 26 | class Object; |
| 27 | |
| 28 | // Classes shared with the managed side of the world need to be packed so that they don't have |
| 29 | // extra platform specific padding. |
| 30 | #define MANAGED PACKED(4) |
| 31 | |
| 32 | // Value type representing a reference to a mirror::Object of type MirrorType. |
| 33 | template<bool kPoisonReferences, class MirrorType> |
| 34 | class MANAGED ObjectReference { |
| 35 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 36 | MirrorType* AsMirrorPtr() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 37 | return UnCompress(); |
| 38 | } |
| 39 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 40 | void Assign(MirrorType* other) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 41 | reference_ = Compress(other); |
| 42 | } |
| 43 | |
| 44 | void Clear() { |
| 45 | reference_ = 0; |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 46 | DCHECK(IsNull()); |
| 47 | } |
| 48 | |
| 49 | bool IsNull() const { |
| 50 | return reference_ == 0; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | uint32_t AsVRegValue() const { |
| 54 | return reference_; |
| 55 | } |
| 56 | |
| 57 | protected: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame^] | 58 | explicit ObjectReference(MirrorType* mirror_ptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 59 | REQUIRES_SHARED(Locks::mutator_lock_) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 60 | : reference_(Compress(mirror_ptr)) { |
| 61 | } |
| 62 | |
| 63 | // Compress reference to its bit representation. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 64 | static uint32_t Compress(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 65 | uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr); |
| 66 | return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits); |
| 67 | } |
| 68 | |
| 69 | // Uncompress an encoded reference from its bit representation. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | MirrorType* UnCompress() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 71 | uintptr_t as_bits = kPoisonReferences ? -reference_ : reference_; |
| 72 | return reinterpret_cast<MirrorType*>(as_bits); |
| 73 | } |
| 74 | |
| 75 | friend class Object; |
| 76 | |
| 77 | // The encoded reference to a mirror::Object. |
| 78 | uint32_t reference_; |
| 79 | }; |
| 80 | |
| 81 | // References between objects within the managed heap. |
| 82 | template<class MirrorType> |
Hiroshi Yamauchi | e63a745 | 2014-02-27 14:44:36 -0800 | [diff] [blame] | 83 | class MANAGED HeapReference : public ObjectReference<kPoisonHeapReferences, MirrorType> { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 84 | public: |
| 85 | static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 86 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 87 | return HeapReference<MirrorType>(mirror_ptr); |
| 88 | } |
| 89 | private: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame^] | 90 | explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) |
Hiroshi Yamauchi | e63a745 | 2014-02-27 14:44:36 -0800 | [diff] [blame] | 91 | : ObjectReference<kPoisonHeapReferences, MirrorType>(mirror_ptr) {} |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Mathieu Chartier | d3ed9a3 | 2015-04-10 14:23:35 -0700 | [diff] [blame] | 94 | // 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] | 95 | template<class MirrorType> |
| 96 | class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> { |
| 97 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 98 | CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 99 | : mirror::ObjectReference<false, MirrorType>(nullptr) {} |
| 100 | |
| 101 | static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 102 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 103 | return CompressedReference<MirrorType>(p); |
| 104 | } |
| 105 | |
| 106 | private: |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame^] | 107 | explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_) |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 108 | : mirror::ObjectReference<false, MirrorType>(p) {} |
| 109 | }; |
| 110 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 111 | } // namespace mirror |
| 112 | } // namespace art |
| 113 | |
| 114 | #endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_ |