Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [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_GC_ROOT_H_ |
| 18 | #define ART_RUNTIME_GC_ROOT_H_ |
| 19 | |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 20 | #include "base/macros.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 21 | #include "base/mutex.h" // For Locks::mutator_lock_. |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 25 | namespace mirror { |
| 26 | class Object; |
| 27 | } // namespace mirror |
| 28 | |
| 29 | enum 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 | }; |
| 44 | std::ostream& operator<<(std::ostream& os, const RootType& root_type); |
| 45 | |
| 46 | class 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. |
| 71 | typedef void (RootCallback)(mirror::Object** root, void* arg, const RootInfo& root_info); |
| 72 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 73 | template<class MirrorType> |
Mathieu Chartier | bad0267 | 2014-08-25 13:08:22 -0700 | [diff] [blame] | 74 | class PACKED(4) GcRoot { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 75 | public: |
| 76 | template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier> |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 77 | ALWAYS_INLINE MirrorType* Read() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 78 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 79 | void VisitRoot(RootCallback* callback, void* arg, const RootInfo& info) const { |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 80 | DCHECK(!IsNull()); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 81 | callback(reinterpret_cast<mirror::Object**>(&root_), arg, info); |
Mathieu Chartier | eb175f7 | 2014-10-31 11:49:27 -0700 | [diff] [blame] | 82 | DCHECK(!IsNull()); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 85 | void VisitRootIfNonNull(RootCallback* callback, void* arg, const RootInfo& info) const { |
| 86 | if (!IsNull()) { |
| 87 | VisitRoot(callback, arg, info); |
| 88 | } |
| 89 | } |
| 90 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 91 | // 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 Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 109 | mutable MirrorType* root_; |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | } // namespace art |
| 113 | |
| 114 | #endif // ART_RUNTIME_GC_ROOT_H_ |