blob: 471b37ceb1caa26a4548f3c91af994de2ca7ff6c [file] [log] [blame]
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -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_READ_BARRIER_H_
18#define ART_RUNTIME_READ_BARRIER_H_
19
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070020#include "base/mutex.h"
21#include "base/macros.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080022#include "jni.h"
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070023#include "mirror/object_reference.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070024#include "offsets.h"
25#include "read_barrier_c.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070026
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070027// This is a C++ (not C) header file, separate from read_barrier_c.h
28// which needs to be a C header file for asm_support.h.
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070029
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070030namespace art {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080031
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070032namespace mirror {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080033 class ArtField;
34 class ArtMethod;
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070035 class Object;
36 template<typename MirrorType> class HeapReference;
37} // namespace mirror
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070038
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070039class ReadBarrier {
40 public:
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080041 // TODO: disable thse flags for production use.
42 // Enable the to-space invariant checks.
43 static constexpr bool kEnableToSpaceInvariantChecks = true;
44 // Enable the read barrier checks.
45 static constexpr bool kEnableReadBarrierInvariantChecks = true;
46
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070047 // It's up to the implementation whether the given field gets
48 // updated whereas the return value must be an updated reference.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080049 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
50 bool kMaybeDuringStartup = false>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070051 ALWAYS_INLINE static MirrorType* Barrier(
52 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
53 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070054
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070055 // It's up to the implementation whether the given root gets updated
56 // whereas the return value must be an updated reference.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080057 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
58 bool kMaybeDuringStartup = false>
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070059 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root)
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080061
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070062 // It's up to the implementation whether the given root gets updated
63 // whereas the return value must be an updated reference.
64 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
65 bool kMaybeDuringStartup = false>
66 ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
68
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080069 static bool IsDuringStartup();
70
71 // Without the holder object.
72 static void AssertToSpaceInvariant(mirror::Object* ref)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
75 }
76 // With the holder object.
77 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
78 mirror::Object* ref)
79 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80
81 static mirror::Object* Mark(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
82
83 static mirror::Object* WhitePtr() {
84 return reinterpret_cast<mirror::Object*>(white_ptr_);
85 }
86 static mirror::Object* GrayPtr() {
87 return reinterpret_cast<mirror::Object*>(gray_ptr_);
88 }
89 static mirror::Object* BlackPtr() {
90 return reinterpret_cast<mirror::Object*>(black_ptr_);
91 }
92
93 ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj,
94 uintptr_t* out_rb_ptr_high_bits)
95 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
97 // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them.
98 static constexpr uintptr_t white_ptr_ = 0x0; // Not marked.
99 static constexpr uintptr_t gray_ptr_ = 0x1; // Marked, but not marked through. On mark stack.
100 static constexpr uintptr_t black_ptr_ = 0x2; // Marked through. Used for non-moving objects.
101 static constexpr uintptr_t rb_ptr_mask_ = 0x3; // The low 2 bits for white|gray|black.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700102};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700103
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700104} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700105
106#endif // ART_RUNTIME_READ_BARRIER_H_