blob: b30890f99ab6790ee6939f9ba61ff03186d89360 [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
20#include "locks.h"
21
22namespace art {
23namespace mirror {
24
25class Object;
26
27// Classes shared with the managed side of the world need to be packed so that they don't have
28// extra platform specific padding.
29#define MANAGED PACKED(4)
30
31// Value type representing a reference to a mirror::Object of type MirrorType.
32template<bool kPoisonReferences, class MirrorType>
33class MANAGED ObjectReference {
34 public:
35 MirrorType* AsMirrorPtr() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
36 return UnCompress();
37 }
38
39 void Assign(MirrorType* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
40 reference_ = Compress(other);
41 }
42
43 void Clear() {
44 reference_ = 0;
45 }
46
47 uint32_t AsVRegValue() const {
48 return reference_;
49 }
50
51 protected:
52 ObjectReference<kPoisonReferences, MirrorType>(MirrorType* mirror_ptr)
53 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
54 : reference_(Compress(mirror_ptr)) {
55 }
56
57 // Compress reference to its bit representation.
58 static uint32_t Compress(MirrorType* mirror_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
59 uintptr_t as_bits = reinterpret_cast<uintptr_t>(mirror_ptr);
60 return static_cast<uint32_t>(kPoisonReferences ? -as_bits : as_bits);
61 }
62
63 // Uncompress an encoded reference from its bit representation.
64 MirrorType* UnCompress() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 uintptr_t as_bits = kPoisonReferences ? -reference_ : reference_;
66 return reinterpret_cast<MirrorType*>(as_bits);
67 }
68
69 friend class Object;
70
71 // The encoded reference to a mirror::Object.
72 uint32_t reference_;
73};
74
75// References between objects within the managed heap.
76template<class MirrorType>
77class MANAGED HeapReference : public ObjectReference<false, MirrorType> {
78 public:
79 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
80 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81 return HeapReference<MirrorType>(mirror_ptr);
82 }
83 private:
84 HeapReference<MirrorType>(MirrorType* mirror_ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
85 : ObjectReference<false, MirrorType>(mirror_ptr) {}
86};
87
88} // namespace mirror
89} // namespace art
90
91#endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_