blob: 71f34c66e25debf1cec0e2b21cf137fc14b568b3 [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
Mathieu Chartier1a5337f2016-10-13 13:48:23 -070045 void Assign(ObjPtr<MirrorType> ptr)
46 REQUIRES_SHARED(Locks::mutator_lock_);
47
Ian Rogersef7d42f2014-01-06 12:55:46 -080048 void Clear() {
49 reference_ = 0;
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070050 DCHECK(IsNull());
51 }
52
53 bool IsNull() const {
54 return reference_ == 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -080055 }
56
57 uint32_t AsVRegValue() const {
58 return reference_;
59 }
60
61 protected:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070062 explicit ObjectReference(MirrorType* mirror_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070063 REQUIRES_SHARED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -080064 : reference_(Compress(mirror_ptr)) {
65 }
66
67 // Compress reference to its bit representation.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 static uint32_t Compress(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080069 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 Gampebdf7f1c2016-08-30 16:38:47 -070074 MirrorType* UnCompress() const REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080075 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.
86template<class MirrorType>
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080087class MANAGED HeapReference : public ObjectReference<kPoisonHeapReferences, MirrorType> {
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 public:
89 static HeapReference<MirrorType> FromMirrorPtr(MirrorType* mirror_ptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 return HeapReference<MirrorType>(mirror_ptr);
92 }
Mathieu Chartiera058fdf2016-10-06 15:13:58 -070093
94 static HeapReference<MirrorType> FromObjPtr(ObjPtr<MirrorType> ptr)
95 REQUIRES_SHARED(Locks::mutator_lock_);
96
Ian Rogersef7d42f2014-01-06 12:55:46 -080097 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -070098 explicit HeapReference(MirrorType* mirror_ptr) REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchie63a7452014-02-27 14:44:36 -080099 : ObjectReference<kPoisonHeapReferences, MirrorType>(mirror_ptr) {}
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100};
101
Mathieu Chartiera058fdf2016-10-06 15:13:58 -0700102static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize,
103 "heap reference size does not match");
104
Mathieu Chartierd3ed9a32015-04-10 14:23:35 -0700105// Standard compressed reference used in the runtime. Used for StackReference and GC roots.
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700106template<class MirrorType>
107class MANAGED CompressedReference : public mirror::ObjectReference<false, MirrorType> {
108 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 CompressedReference<MirrorType>() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700110 : mirror::ObjectReference<false, MirrorType>(nullptr) {}
111
112 static CompressedReference<MirrorType> FromMirrorPtr(MirrorType* p)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700113 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700114 return CompressedReference<MirrorType>(p);
115 }
116
117 private:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700118 explicit CompressedReference(MirrorType* p) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -0700119 : mirror::ObjectReference<false, MirrorType>(p) {}
120};
121
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122} // namespace mirror
123} // namespace art
124
125#endif // ART_RUNTIME_MIRROR_OBJECT_REFERENCE_H_