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 20 #include "base/mutex.h" // For Locks::mutator_lock_. 21 #include "globals.h" 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: AsMirrorPtr()36 MirrorType* AsMirrorPtr() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 37 return UnCompress(); 38 } 39 Assign(MirrorType * other)40 void Assign(MirrorType* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 41 reference_ = Compress(other); 42 } 43 Clear()44 void Clear() { 45 reference_ = 0; 46 } 47 AsVRegValue()48 uint32_t AsVRegValue() const { 49 return reference_; 50 } 51 52 protected: 53 ObjectReference<kPoisonReferences, MirrorType>(MirrorType* mirror_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)54 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 55 : reference_(Compress(mirror_ptr)) { 56 } 57 58 // Compress reference to its bit representation. Compress(MirrorType * mirror_ptr)59 static uint32_t Compress(MirrorType* mirror_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 60 uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr); 61 return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits); 62 } 63 64 // Uncompress an encoded reference from its bit representation. UnCompress()65 MirrorType* UnCompress() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 66 uintptr_t as_bits = kPoisonReferences ? -reference_ : reference_; 67 return reinterpret_cast<MirrorType*>(as_bits); 68 } 69 70 friend class Object; 71 72 // The encoded reference to a mirror::Object. 73 uint32_t reference_; 74 }; 75 76 // References between objects within the managed heap. 77 template<class MirrorType> 78 class MANAGED HeapReference : public ObjectReference<kPoisonHeapReferences, MirrorType> { 79 public: FromMirrorPtr(MirrorType * mirror_ptr)80 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr) 81 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 82 return HeapReference<MirrorType>(mirror_ptr); 83 } 84 private: SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)85 HeapReference<MirrorType>(MirrorType* mirror_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) 86 : ObjectReference<kPoisonHeapReferences, MirrorType>(mirror_ptr) {} 87 }; 88 89 } // namespace mirror 90 } // namespace art 91 92 #endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_ 93