blob: 7e0be64441c02d0e0a82f74444c0957463380001 [file] [log] [blame]
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001/*
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_GC_ROOT_H_
18#define ART_RUNTIME_GC_ROOT_H_
19
Mathieu Chartierbad02672014-08-25 13:08:22 -070020#include "base/macros.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070021#include "base/mutex.h" // For Locks::mutator_lock_.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070022
23namespace art {
24
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080025namespace mirror {
26class Object;
27} // namespace mirror
28
29enum RootType {
30 kRootUnknown = 0,
31 kRootJNIGlobal,
32 kRootJNILocal,
33 kRootJavaFrame,
34 kRootNativeStack,
35 kRootStickyClass,
36 kRootThreadBlock,
37 kRootMonitorUsed,
38 kRootThreadObject,
39 kRootInternedString,
40 kRootDebugger,
41 kRootVMInternal,
42 kRootJNIMonitor,
43};
44std::ostream& operator<<(std::ostream& os, const RootType& root_type);
45
46class RootInfo {
47 public:
48 // Thread id 0 is for non thread roots.
49 explicit RootInfo(RootType type, uint32_t thread_id = 0)
50 : type_(type), thread_id_(thread_id) {
51 }
52 virtual ~RootInfo() {
53 }
54 RootType GetType() const {
55 return type_;
56 }
57 uint32_t GetThreadId() const {
58 return thread_id_;
59 }
60 virtual void Describe(std::ostream& os) const {
61 os << "Type=" << type_ << " thread_id=" << thread_id_;
62 }
63
64 private:
65 const RootType type_;
66 const uint32_t thread_id_;
67};
68
69// Returns the new address of the object, returns root if it has not moved. tid and root_type are
70// only used by hprof.
71typedef void (RootCallback)(mirror::Object** root, void* arg, const RootInfo& root_info);
72
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070073template<class MirrorType>
Mathieu Chartierbad02672014-08-25 13:08:22 -070074class PACKED(4) GcRoot {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070075 public:
76 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Mathieu Chartierc2e20622014-11-03 11:41:47 -080077 ALWAYS_INLINE MirrorType* Read() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070078
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080079 void VisitRoot(RootCallback* callback, void* arg, const RootInfo& info) const {
Mathieu Chartiereb175f72014-10-31 11:49:27 -070080 DCHECK(!IsNull());
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080081 callback(reinterpret_cast<mirror::Object**>(&root_), arg, info);
Mathieu Chartiereb175f72014-10-31 11:49:27 -070082 DCHECK(!IsNull());
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070083 }
84
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080085 void VisitRootIfNonNull(RootCallback* callback, void* arg, const RootInfo& info) const {
86 if (!IsNull()) {
87 VisitRoot(callback, arg, info);
88 }
89 }
90
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070091 // This is only used by IrtIterator.
92 ALWAYS_INLINE MirrorType** AddressWithoutBarrier() {
93 return &root_;
94 }
95
96 bool IsNull() const {
97 // It's safe to null-check it without a read barrier.
98 return root_ == nullptr;
99 }
100
101 ALWAYS_INLINE explicit GcRoot<MirrorType>() : root_(nullptr) {
102 }
103
104 ALWAYS_INLINE explicit GcRoot<MirrorType>(MirrorType* ref)
105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : root_(ref) {
106 }
107
108 private:
Mathieu Chartierc2e20622014-11-03 11:41:47 -0800109 mutable MirrorType* root_;
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700110};
111
112} // namespace art
113
114#endif // ART_RUNTIME_GC_ROOT_H_