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