blob: 474b46f99c36e3ca8cf646cd0d4569d71925bbb1 [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"
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070023#include "offsets.h"
24#include "read_barrier_c.h"
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070025
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070026// This is a C++ (not C) header file, separate from read_barrier_c.h
27// which needs to be a C header file for asm_support.h.
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070028
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070029namespace art {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080030
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070031namespace mirror {
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080032 class ArtField;
33 class ArtMethod;
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070034 class Object;
35 template<typename MirrorType> class HeapReference;
36} // namespace mirror
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 Yamauchiea2e1bd2014-06-18 13:47:35 -070046 // It's up to the implementation whether the given field gets
47 // updated whereas the return value must be an updated reference.
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080048 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
49 bool kMaybeDuringStartup = 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)
52 SHARED_LOCKS_REQUIRED(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 Yamauchi2cd334a2015-01-09 14:03:35 -080056 template <typename MirrorType, ReadBarrierOption kReadBarrierOption = kWithReadBarrier,
57 bool kMaybeDuringStartup = false>
Hiroshi Yamauchia91a4bc2014-06-13 16:44:55 -070058 ALWAYS_INLINE static MirrorType* BarrierForRoot(MirrorType** root)
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -070059 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -080060
61 static bool IsDuringStartup();
62
63 // Without the holder object.
64 static void AssertToSpaceInvariant(mirror::Object* ref)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
66 AssertToSpaceInvariant(nullptr, MemberOffset(0), ref);
67 }
68 // With the holder object.
69 static void AssertToSpaceInvariant(mirror::Object* obj, MemberOffset offset,
70 mirror::Object* ref)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
72
73 static mirror::Object* Mark(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
74
75 static mirror::Object* WhitePtr() {
76 return reinterpret_cast<mirror::Object*>(white_ptr_);
77 }
78 static mirror::Object* GrayPtr() {
79 return reinterpret_cast<mirror::Object*>(gray_ptr_);
80 }
81 static mirror::Object* BlackPtr() {
82 return reinterpret_cast<mirror::Object*>(black_ptr_);
83 }
84
85 ALWAYS_INLINE static bool HasGrayReadBarrierPointer(mirror::Object* obj,
86 uintptr_t* out_rb_ptr_high_bits)
87 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88
89 // Note: These couldn't be constexpr pointers as reinterpret_cast isn't compatible with them.
90 static constexpr uintptr_t white_ptr_ = 0x0; // Not marked.
91 static constexpr uintptr_t gray_ptr_ = 0x1; // Marked, but not marked through. On mark stack.
92 static constexpr uintptr_t black_ptr_ = 0x2; // Marked through. Used for non-moving objects.
93 static constexpr uintptr_t rb_ptr_mask_ = 0x3; // The low 2 bits for white|gray|black.
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070094};
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070095
Hiroshi Yamauchi800ac2d2014-04-02 17:32:54 -070096} // namespace art
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -070097
98#endif // ART_RUNTIME_READ_BARRIER_H_