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