blob: b7bd99b6f5ce2276772fa6079acc170628ea22f0 [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 Yamauchi3f64f252015-06-12 18:35:06 -070022#include "gc_root.h"
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080023#include "jni.h"
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070024#include "mirror/object_reference.h"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070025#include "offsets.h"
26#include "read_barrier_c.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070027
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070028// This is a C++ (not C) header file, separate from read_barrier_c.h
29// which needs to be a C header file for asm_support.h.
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070030
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070031namespace art {
32namespace mirror {
33 class Object;
34 template<typename MirrorType> class HeapReference;
35} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070036class ArtMethod;
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070037
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070038class ReadBarrier {
39 public:
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080040 // TODO: disable thse flags for production use.
41 // Enable the to-space invariant checks.
42 static constexpr bool kEnableToSpaceInvariantChecks = true;
43 // Enable the read barrier checks.
44 static constexpr bool kEnableReadBarrierInvariantChecks = true;
45
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080046 // It's up to the implementation whether the given field gets updated whereas the return value
47 // must be an updated reference unless kAlwaysUpdateField is true.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080048 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080049 bool kAlwaysUpdateField = false>
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070050 ALWAYS_INLINE static MirrorType* Barrier(
51 mirror::Object* obj, MemberOffset offset, mirror::HeapReference<MirrorType>* ref_addr)
Mathieu Chartier90443472015-07-16 20:32:27 -070052 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070053
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -070054 // It's up to the implementation whether the given root gets updated
55 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080056 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070057 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root,
58 GcRootSource* gc_root_source = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -070059 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080060
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070061 // It's up to the implementation whether the given root gets updated
62 // whereas the return value must be an updated reference.
Hiroshi Yamauchicc78f3f2015-12-11 15:51:04 -080063 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070064 ALWAYS_INLINE static MirrorType* BarrierForRoot(mirror::CompressedReference<MirrorType>* root,
65 GcRootSource* gc_root_source = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -070066 SHARED_REQUIRES(Locks::mutator_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070067
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080068 static bool IsDuringStartup();
69
70 // Without the holder object.
71 static void AssertToSpaceInvariant(mirror::Object* ref)
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080073 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
74 }
75 // With the holder object.
76 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
77 mirror::Object* ref)
Mathieu Chartier90443472015-07-16 20:32:27 -070078 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070079 // With GcRootSource.
80 static void AssertToSpaceInvariant(GcRootSource* gc_root_source, mirror::Object* ref)
Mathieu Chartier90443472015-07-16 20:32:27 -070081 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080082
Hiroshi Yamauchi2e5de782016-01-29 12:06:36 -080083 // ALWAYS_INLINE on this caused a performance regression b/26744236.
Hiroshi Yamauchi49c93332016-01-28 13:39:41 -080084 static mirror::Object* Mark(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080085
86 static mirror::Object* WhitePtr() {
87 return reinterpret_cast<mirror::Object*>(white_ptr_);
88 }
89 static mirror::Object* GrayPtr() {
90 return reinterpret_cast<mirror::Object*>(gray_ptr_);
91 }
92 static mirror::Object* BlackPtr() {
93 return reinterpret_cast<mirror::Object*>(black_ptr_);
94 }
95
96 ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj,
97 uintptr_t* out_rb_ptr_high_bits)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080099
100 // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them.
101 static constexpr uintptr_t white_ptr_ = 0x0; // Not marked.
102 static constexpr uintptr_t gray_ptr_ = 0x1; // Marked, but not marked through. On mark stack.
103 static constexpr uintptr_t black_ptr_ = 0x2; // Marked through. Used for non-moving objects.
104 static constexpr uintptr_t rb_ptr_mask_ = 0x3; // The low 2 bits for white|gray|black.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700105};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700106
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -0700107} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700108
109#endif // ART_RUNTIME_READ_BARRIER_H_