Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "object.h" |
| 19 | |
| 20 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 21 | |
| 22 | namespace art { |
| 23 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 24 | static jlong Unsafe_objectFieldOffset0(JNIEnv* env, jclass, jobject javaField) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 25 | // TODO: move to Java code |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 26 | jfieldID fid = env->FromReflectedField(javaField); |
| 27 | Field* field = DecodeField(fid); |
| 28 | return field->GetOffset().Int32Value(); |
| 29 | } |
| 30 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 31 | static jint Unsafe_arrayBaseOffset0(JNIEnv* env, jclass, jclass javaArrayClass) { |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 32 | // TODO: move to Java code |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame^] | 33 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 34 | Class* array_class = Decode<Class*>(env, javaArrayClass); |
| 35 | return Array::DataOffset(array_class->GetComponentSize()).Int32Value(); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 38 | static jint Unsafe_arrayIndexScale0(JNIEnv* env, jclass, jclass javaClass) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 39 | Class* c = Decode<Class*>(env, javaClass); |
| 40 | return c->GetComponentSize(); |
| 41 | } |
| 42 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 43 | static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint expectedValue, jint newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 44 | Object* obj = Decode<Object*>(env, javaObj); |
| 45 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 46 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 47 | // Note: android_atomic_release_cas() returns 0 on success, not failure. |
| 48 | int result = android_atomic_release_cas(expectedValue, newValue, address); |
| 49 | return (result == 0); |
| 50 | } |
| 51 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 52 | static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong expectedValue, jlong newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 53 | Object* obj = Decode<Object*>(env, javaObj); |
| 54 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 55 | volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr); |
| 56 | // Note: android_atomic_cmpxchg() returns 0 on success, not failure. |
| 57 | int result = QuasiAtomicCas64(expectedValue, newValue, address); |
| 58 | return (result == 0); |
| 59 | } |
| 60 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 61 | static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaExpectedValue, jobject javaNewValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 62 | Object* obj = Decode<Object*>(env, javaObj); |
| 63 | Object* expectedValue = Decode<Object*>(env, javaExpectedValue); |
| 64 | Object* newValue = Decode<Object*>(env, javaNewValue); |
| 65 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 66 | int32_t* address = reinterpret_cast<int32_t*>(raw_addr); |
| 67 | // Note: android_atomic_cmpxchg() returns 0 on success, not failure. |
| 68 | int result = android_atomic_release_cas(reinterpret_cast<int32_t>(expectedValue), |
| 69 | reinterpret_cast<int32_t>(newValue), address); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 70 | if (result == 0) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 71 | Runtime::Current()->GetHeap()->WriteBarrierField(obj, MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 72 | } |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 73 | return (result == 0); |
| 74 | } |
| 75 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 76 | static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 77 | Object* obj = Decode<Object*>(env, javaObj); |
| 78 | return obj->GetField32(MemberOffset(offset), false); |
| 79 | } |
| 80 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 81 | static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 82 | Object* obj = Decode<Object*>(env, javaObj); |
| 83 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 84 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 85 | return android_atomic_acquire_load(address); |
| 86 | } |
| 87 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 88 | static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 89 | Object* obj = Decode<Object*>(env, javaObj); |
| 90 | obj->SetField32(MemberOffset(offset), newValue, false); |
| 91 | } |
| 92 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 93 | static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 94 | Object* obj = Decode<Object*>(env, javaObj); |
| 95 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 96 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 97 | android_atomic_release_store(newValue, address); |
| 98 | } |
| 99 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 100 | static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 101 | Object* obj = Decode<Object*>(env, javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 102 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 103 | obj->SetField32(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 106 | static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 107 | Object* obj = Decode<Object*>(env, javaObj); |
| 108 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 109 | int64_t* address = reinterpret_cast<int64_t*>(raw_addr); |
| 110 | return *address; |
| 111 | } |
| 112 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 113 | static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 114 | Object* obj = Decode<Object*>(env, javaObj); |
| 115 | return obj->GetField64(MemberOffset(offset), true); |
| 116 | } |
| 117 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 118 | static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 119 | Object* obj = Decode<Object*>(env, javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 120 | obj->SetField64(MemberOffset(offset), newValue, false); |
| 121 | } |
| 122 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 123 | static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 124 | Object* obj = Decode<Object*>(env, javaObj); |
| 125 | obj->SetField64(MemberOffset(offset), newValue, true); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 128 | static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 129 | Object* obj = Decode<Object*>(env, javaObj); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 130 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 131 | obj->SetField64(MemberOffset(offset), newValue, false); |
| 132 | } |
| 133 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 134 | static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 135 | Object* obj = Decode<Object*>(env, javaObj); |
| 136 | Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), true); |
| 137 | return AddLocalReference<jobject>(env, value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 140 | static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 141 | Object* obj = Decode<Object*>(env, javaObj); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 142 | Object* value = obj->GetFieldObject<Object*>(MemberOffset(offset), false); |
| 143 | return AddLocalReference<jobject>(env, value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 146 | static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 147 | Object* obj = Decode<Object*>(env, javaObj); |
| 148 | Object* newValue = Decode<Object*>(env, javaNewValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 149 | obj->SetFieldObject(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 152 | static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 153 | Object* obj = Decode<Object*>(env, javaObj); |
| 154 | Object* newValue = Decode<Object*>(env, javaNewValue); |
| 155 | obj->SetFieldObject(MemberOffset(offset), newValue, true); |
| 156 | } |
| 157 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 158 | static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 159 | Object* obj = Decode<Object*>(env, javaObj); |
| 160 | Object* newValue = Decode<Object*>(env, javaNewValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 161 | ANDROID_MEMBAR_STORE(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 162 | obj->SetFieldObject(MemberOffset(offset), newValue, false); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 165 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 166 | NATIVE_METHOD(Unsafe, objectFieldOffset0, "(Ljava/lang/reflect/Field;)J"), |
| 167 | NATIVE_METHOD(Unsafe, arrayBaseOffset0, "(Ljava/lang/Class;)I"), |
| 168 | NATIVE_METHOD(Unsafe, arrayIndexScale0, "(Ljava/lang/Class;)I"), |
| 169 | NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"), |
| 170 | NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"), |
| 171 | NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"), |
| 172 | NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"), |
| 173 | NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"), |
| 174 | NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"), |
| 175 | NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"), |
| 176 | NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 177 | NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 178 | NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"), |
| 179 | NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"), |
| 180 | NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"), |
| 181 | NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"), |
| 182 | NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"), |
| 183 | NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"), |
| 184 | NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 185 | NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 186 | NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 187 | }; |
| 188 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 189 | void register_sun_misc_Unsafe(JNIEnv* env) { |
| 190 | jniRegisterNativeMethods(env, "sun/misc/Unsafe", gMethods, NELEM(gMethods)); |
| 191 | } |
| 192 | |
| 193 | } // namespace art |