blob: 764db5e7b64b15f5f9b511921ad244c0ca654618 [file] [log] [blame]
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -07001/*
2 * Copyright (C) 2008 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
Ian Rogers1d54e732013-05-02 21:10:01 -070017#include "gc/accounting/card_table-inl.h"
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070018#include "jni_internal.h"
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -080019#include "mirror/array.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/object.h"
21#include "mirror/object-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070022#include "scoped_fast_native_object_access.h"
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070023
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070024namespace art {
25
Ian Rogersef7d42f2014-01-06 12:55:46 -080026static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset,
27 jint expectedValue, jint newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070028 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010030 // JNI must use non transactional mode.
31 bool success = obj->CasField32<false>(MemberOffset(offset), expectedValue, newValue);
Ian Rogers9adbff52013-01-23 18:19:03 -080032 return success ? JNI_TRUE : JNI_FALSE;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070033}
34
Ian Rogersef7d42f2014-01-06 12:55:46 -080035static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset,
36 jlong expectedValue, jlong newValue) {
37 ScopedFastNativeObjectAccess soa(env);
38 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010039 // JNI must use non transactional mode.
40 bool success = obj->CasField64<false>(MemberOffset(offset), expectedValue, newValue);
Ian Rogersef7d42f2014-01-06 12:55:46 -080041 return success ? JNI_TRUE : JNI_FALSE;
42}
43
44static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
45 jobject javaExpectedValue, jobject javaNewValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070046 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
48 mirror::Object* expectedValue = soa.Decode<mirror::Object*>(javaExpectedValue);
49 mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010050 // JNI must use non transactional mode.
51 bool success = obj->CasFieldObject<false>(MemberOffset(offset), expectedValue, newValue);
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 return success ? JNI_TRUE : JNI_FALSE;
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070053}
54
Elliott Hughes0512f022012-03-15 22:10:52 -070055static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070056 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070058 return obj->GetField32(MemberOffset(offset));
Ian Rogers5d76c432011-10-31 21:42:49 -070059}
60
Elliott Hughes0512f022012-03-15 22:10:52 -070061static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070062 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064 return obj->GetField32Volatile(MemberOffset(offset));
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070065}
66
Elliott Hughes0512f022012-03-15 22:10:52 -070067static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070068 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010070 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070071 obj->SetField32<false>(MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -070072}
73
Ian Rogersef7d42f2014-01-06 12:55:46 -080074static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
75 jint newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070076 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010078 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070079 obj->SetField32Volatile<false>(MemberOffset(offset), newValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070080}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset,
83 jint newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070084 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb122a4b2013-11-19 18:00:50 -080086 QuasiAtomic::MembarStoreStore();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010087 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070088 obj->SetField32<false>(MemberOffset(offset), newValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070089}
90
Elliott Hughes0512f022012-03-15 22:10:52 -070091static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070092 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 return obj->GetField64(MemberOffset(offset));
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -070095}
96
Elliott Hughes0512f022012-03-15 22:10:52 -070097static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070098 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700100 return obj->GetField64Volatile(MemberOffset(offset));
Ian Rogers5d76c432011-10-31 21:42:49 -0700101}
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700104 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100106 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700107 obj->SetField64<false>(MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -0700108}
109
Ian Rogersef7d42f2014-01-06 12:55:46 -0800110static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
111 jlong newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700112 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700115 obj->SetField64Volatile<false>(MemberOffset(offset), newValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700116}
117
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset,
119 jlong newValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700120 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800122 QuasiAtomic::MembarStoreStore();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700124 obj->SetField64<false>(MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -0700125}
126
Elliott Hughes0512f022012-03-15 22:10:52 -0700127static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700128 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 return soa.AddLocalReference<jobject>(value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700132}
133
Elliott Hughes0512f022012-03-15 22:10:52 -0700134static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700135 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800136 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700137 mirror::Object* value = obj->GetFieldObject<mirror::Object>(MemberOffset(offset));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 return soa.AddLocalReference<jobject>(value);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700139}
140
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
142 jobject javaNewValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700143 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
145 mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100146 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700147 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700148}
149
Ian Rogersef7d42f2014-01-06 12:55:46 -0800150static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
151 jobject javaNewValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700152 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
154 mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700156 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), newValue);
Ian Rogers5d76c432011-10-31 21:42:49 -0700157}
158
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
160 jobject javaNewValue) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700161 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj);
163 mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue);
Ian Rogersb122a4b2013-11-19 18:00:50 -0800164 QuasiAtomic::MembarStoreStore();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100165 // JNI must use non transactional mode.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700166 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700167}
168
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800169static jint Unsafe_getArrayBaseOffsetForComponentType(JNIEnv* env, jclass, jobject component_class) {
170 ScopedFastNativeObjectAccess soa(env);
171 mirror::Class* component = soa.Decode<mirror::Class*>(component_class);
172 Primitive::Type primitive_type = component->GetPrimitiveType();
173 return mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value();
174}
175
176static jint Unsafe_getArrayIndexScaleForComponentType(JNIEnv* env, jclass, jobject component_class) {
177 ScopedFastNativeObjectAccess soa(env);
178 mirror::Class* component = soa.Decode<mirror::Class*>(component_class);
179 Primitive::Type primitive_type = component->GetPrimitiveType();
180 return Primitive::ComponentSize(primitive_type);
181}
182
Elliott Hughes0512f022012-03-15 22:10:52 -0700183static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700184 NATIVE_METHOD(Unsafe, compareAndSwapInt, "!(Ljava/lang/Object;JII)Z"),
185 NATIVE_METHOD(Unsafe, compareAndSwapLong, "!(Ljava/lang/Object;JJJ)Z"),
186 NATIVE_METHOD(Unsafe, compareAndSwapObject, "!(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"),
187 NATIVE_METHOD(Unsafe, getIntVolatile, "!(Ljava/lang/Object;J)I"),
188 NATIVE_METHOD(Unsafe, putIntVolatile, "!(Ljava/lang/Object;JI)V"),
189 NATIVE_METHOD(Unsafe, getLongVolatile, "!(Ljava/lang/Object;J)J"),
190 NATIVE_METHOD(Unsafe, putLongVolatile, "!(Ljava/lang/Object;JJ)V"),
191 NATIVE_METHOD(Unsafe, getObjectVolatile, "!(Ljava/lang/Object;J)Ljava/lang/Object;"),
192 NATIVE_METHOD(Unsafe, putObjectVolatile, "!(Ljava/lang/Object;JLjava/lang/Object;)V"),
193 NATIVE_METHOD(Unsafe, getInt, "!(Ljava/lang/Object;J)I"),
194 NATIVE_METHOD(Unsafe, putInt, "!(Ljava/lang/Object;JI)V"),
195 NATIVE_METHOD(Unsafe, putOrderedInt, "!(Ljava/lang/Object;JI)V"),
196 NATIVE_METHOD(Unsafe, getLong, "!(Ljava/lang/Object;J)J"),
197 NATIVE_METHOD(Unsafe, putLong, "!(Ljava/lang/Object;JJ)V"),
198 NATIVE_METHOD(Unsafe, putOrderedLong, "!(Ljava/lang/Object;JJ)V"),
199 NATIVE_METHOD(Unsafe, getObject, "!(Ljava/lang/Object;J)Ljava/lang/Object;"),
200 NATIVE_METHOD(Unsafe, putObject, "!(Ljava/lang/Object;JLjava/lang/Object;)V"),
201 NATIVE_METHOD(Unsafe, putOrderedObject, "!(Ljava/lang/Object;JLjava/lang/Object;)V"),
Hiroshi Yamauchi4d2efce2014-02-10 16:19:09 -0800202 NATIVE_METHOD(Unsafe, getArrayBaseOffsetForComponentType, "!(Ljava/lang/Class;)I"),
203 NATIVE_METHOD(Unsafe, getArrayIndexScaleForComponentType, "!(Ljava/lang/Class;)I"),
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700204};
205
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700206void register_sun_misc_Unsafe(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700207 REGISTER_NATIVE_METHODS("sun/misc/Unsafe");
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700208}
209
210} // namespace art