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) { |
| 27 | jfieldID fid = env->FromReflectedField(javaField); |
| 28 | Field* field = DecodeField(fid); |
| 29 | return field->GetOffset().Int32Value(); |
| 30 | } |
| 31 | |
| 32 | jint Unsafe_arrayBaseOffset0(JNIEnv*, jclass, jclass) { |
| 33 | return Array::DataOffset().Int32Value(); |
| 34 | } |
| 35 | |
| 36 | jint Unsafe_arrayIndexScale0(JNIEnv* env, jclass, jclass javaClass) { |
| 37 | Class* c = Decode<Class*>(env, javaClass); |
| 38 | return c->GetComponentSize(); |
| 39 | } |
| 40 | |
| 41 | jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint expectedValue, jint newValue) { |
| 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 | |
| 50 | jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong expectedValue, jlong newValue) { |
| 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. |
| 55 | int result = QuasiAtomicCas64(expectedValue, newValue, address); |
| 56 | return (result == 0); |
| 57 | } |
| 58 | |
| 59 | jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaExpectedValue, jobject javaNewValue) { |
| 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); |
| 68 | Heap::WriteBarrier(obj); |
| 69 | return (result == 0); |
| 70 | } |
| 71 | |
| 72 | jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 73 | Object* obj = Decode<Object*>(env, javaObj); |
| 74 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 75 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 76 | return android_atomic_acquire_load(address); |
| 77 | } |
| 78 | |
| 79 | void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
| 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 | android_atomic_release_store(newValue, address); |
| 84 | } |
| 85 | |
| 86 | jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 87 | Object* obj = Decode<Object*>(env, javaObj); |
| 88 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 89 | volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr); |
| 90 | DCHECK_EQ(offset & 7, 0); |
| 91 | return QuasiAtomicRead64(address); |
| 92 | } |
| 93 | |
| 94 | void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
| 95 | Object* obj = Decode<Object*>(env, javaObj); |
| 96 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 97 | volatile int64_t* address = reinterpret_cast<volatile int64_t*>(raw_addr); |
| 98 | DCHECK_EQ(offset & 7, 0); |
| 99 | QuasiAtomicSwap64(newValue, address); |
| 100 | } |
| 101 | |
| 102 | jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 103 | Object* obj = Decode<Object*>(env, javaObj); |
| 104 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 105 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 106 | Object* value = reinterpret_cast<Object*>(android_atomic_acquire_load(address)); |
| 107 | return AddLocalReference<jobject>(env, value); |
| 108 | } |
| 109 | |
| 110 | void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
| 111 | Object* obj = Decode<Object*>(env, javaObj); |
| 112 | Object* newValue = Decode<Object*>(env, javaNewValue); |
| 113 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 114 | volatile int32_t* address = reinterpret_cast<volatile int32_t*>(raw_addr); |
| 115 | android_atomic_release_store(reinterpret_cast<int32_t>(newValue), address); |
| 116 | Heap::WriteBarrier(obj); |
| 117 | } |
| 118 | |
| 119 | jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 120 | Object* obj = Decode<Object*>(env, javaObj); |
| 121 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 122 | int32_t* address = reinterpret_cast<int32_t*>(raw_addr); |
| 123 | return *address; |
| 124 | } |
| 125 | |
| 126 | void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
| 127 | Object* obj = Decode<Object*>(env, javaObj); |
| 128 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 129 | int32_t* address = reinterpret_cast<int32_t*>(raw_addr); |
| 130 | *address = newValue; |
| 131 | } |
| 132 | |
| 133 | void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
| 134 | Object* obj = Decode<Object*>(env, javaObj); |
| 135 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 136 | int32_t* address = reinterpret_cast<int32_t*>(raw_addr); |
| 137 | ANDROID_MEMBAR_STORE(); |
| 138 | *address = newValue; |
| 139 | } |
| 140 | |
| 141 | jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 142 | Object* obj = Decode<Object*>(env, javaObj); |
| 143 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 144 | int64_t* address = reinterpret_cast<int64_t*>(raw_addr); |
| 145 | return *address; |
| 146 | } |
| 147 | |
| 148 | void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
| 149 | Object* obj = Decode<Object*>(env, javaObj); |
| 150 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 151 | int64_t* address = reinterpret_cast<int64_t*>(raw_addr); |
| 152 | *address = newValue; |
| 153 | } |
| 154 | |
| 155 | void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
| 156 | Object* obj = Decode<Object*>(env, javaObj); |
| 157 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 158 | int64_t* address = reinterpret_cast<int64_t*>(raw_addr); |
| 159 | ANDROID_MEMBAR_STORE(); |
| 160 | *address = newValue; |
| 161 | } |
| 162 | |
| 163 | jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 164 | Object* obj = Decode<Object*>(env, javaObj); |
| 165 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 166 | Object** address = reinterpret_cast<Object**>(raw_addr); |
| 167 | return AddLocalReference<jobject>(env, *address); |
| 168 | } |
| 169 | |
| 170 | void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
| 171 | Object* obj = Decode<Object*>(env, javaObj); |
| 172 | Object* newValue = Decode<Object*>(env, javaNewValue); |
| 173 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 174 | Object** address = reinterpret_cast<Object**>(raw_addr); |
| 175 | *address = newValue; |
| 176 | Heap::WriteBarrier(obj); |
| 177 | } |
| 178 | |
| 179 | void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, jobject javaNewValue) { |
| 180 | Object* obj = Decode<Object*>(env, javaObj); |
| 181 | Object* newValue = Decode<Object*>(env, javaNewValue); |
| 182 | byte* raw_addr = reinterpret_cast<byte*>(obj) + offset; |
| 183 | Object** address = reinterpret_cast<Object**>(raw_addr); |
| 184 | ANDROID_MEMBAR_STORE(); |
| 185 | *address = newValue; |
| 186 | Heap::WriteBarrier(obj); |
| 187 | } |
| 188 | |
| 189 | JNINativeMethod gMethods[] = { |
| 190 | NATIVE_METHOD(Unsafe, objectFieldOffset0, "(Ljava/lang/reflect/Field;)J"), |
| 191 | NATIVE_METHOD(Unsafe, arrayBaseOffset0, "(Ljava/lang/Class;)I"), |
| 192 | NATIVE_METHOD(Unsafe, arrayIndexScale0, "(Ljava/lang/Class;)I"), |
| 193 | NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"), |
| 194 | NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"), |
| 195 | NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"), |
| 196 | NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"), |
| 197 | NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"), |
| 198 | NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"), |
| 199 | NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"), |
| 200 | NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 201 | NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 202 | NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"), |
| 203 | NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"), |
| 204 | NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"), |
| 205 | NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"), |
| 206 | NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"), |
| 207 | NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"), |
| 208 | NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 209 | NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 210 | NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 211 | }; |
| 212 | |
| 213 | } // namespace |
| 214 | |
| 215 | void register_sun_misc_Unsafe(JNIEnv* env) { |
| 216 | jniRegisterNativeMethods(env, "sun/misc/Unsafe", gMethods, NELEM(gMethods)); |
| 217 | } |
| 218 | |
| 219 | } // namespace art |