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 | |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 17 | #include "common_throws.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 18 | #include "gc/accounting/card_table-inl.h" |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 19 | #include "jni_internal.h" |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 20 | #include "mirror/array.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/object.h" |
| 22 | #include "mirror/object-inl.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 23 | #include "scoped_fast_native_object_access.h" |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 24 | |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 31 | static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 32 | jint expectedValue, jint newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 33 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 35 | // JNI must use non transactional mode. |
Hans Boehm | d843443 | 2014-07-11 09:56:07 -0700 | [diff] [blame] | 36 | bool success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset), |
| 37 | expectedValue, newValue); |
Ian Rogers | 9adbff5 | 2013-01-23 18:19:03 -0800 | [diff] [blame] | 38 | return success ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 41 | static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 42 | jlong expectedValue, jlong newValue) { |
| 43 | ScopedFastNativeObjectAccess soa(env); |
| 44 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 45 | // JNI must use non transactional mode. |
Hans Boehm | d843443 | 2014-07-11 09:56:07 -0700 | [diff] [blame] | 46 | bool success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset), |
| 47 | expectedValue, newValue); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 48 | return success ? JNI_TRUE : JNI_FALSE; |
| 49 | } |
| 50 | |
| 51 | static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 52 | jobject javaExpectedValue, jobject javaNewValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 53 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 55 | mirror::Object* expectedValue = soa.Decode<mirror::Object*>(javaExpectedValue); |
| 56 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 57 | // JNI must use non transactional mode. |
Hans Boehm | d843443 | 2014-07-11 09:56:07 -0700 | [diff] [blame] | 58 | bool success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset), |
| 59 | expectedValue, newValue); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 60 | return success ? JNI_TRUE : JNI_FALSE; |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 63 | static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 64 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 65 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 66 | return obj->GetField32(MemberOffset(offset)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 69 | static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 70 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 72 | return obj->GetField32Volatile(MemberOffset(offset)); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 75 | static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 76 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 77 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 78 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 79 | obj->SetField32<false>(MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 82 | static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 83 | jint newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 84 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 86 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 87 | obj->SetField32Volatile<false>(MemberOffset(offset), newValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 90 | static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 91 | jint newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 92 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 94 | QuasiAtomic::ThreadFenceRelease(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 95 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 96 | obj->SetField32<false>(MemberOffset(offset), newValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 99 | static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 100 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 102 | return obj->GetField64(MemberOffset(offset)); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 105 | static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 106 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 108 | return obj->GetField64Volatile(MemberOffset(offset)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 111 | static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 112 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 113 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 114 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 115 | obj->SetField64<false>(MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 118 | static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 119 | jlong newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 120 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 122 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 123 | obj->SetField64Volatile<false>(MemberOffset(offset), newValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 126 | static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 127 | jlong newValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 128 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 130 | QuasiAtomic::ThreadFenceRelease(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 131 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 132 | obj->SetField64<false>(MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 135 | static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 136 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 137 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 138 | mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 139 | return soa.AddLocalReference<jobject>(value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 142 | static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 143 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 144 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 145 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(MemberOffset(offset)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 146 | return soa.AddLocalReference<jobject>(value); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 149 | static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 150 | jobject javaNewValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 151 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 152 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 153 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 154 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 155 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 158 | static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 159 | jobject javaNewValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 160 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 161 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 162 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 163 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 164 | obj->SetFieldObjectVolatile<false>(MemberOffset(offset), newValue); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset, |
| 168 | jobject javaNewValue) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 169 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 170 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 171 | mirror::Object* newValue = soa.Decode<mirror::Object*>(javaNewValue); |
Hans Boehm | 3035961 | 2014-05-21 17:46:23 -0700 | [diff] [blame] | 172 | QuasiAtomic::ThreadFenceRelease(); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 173 | // JNI must use non transactional mode. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 174 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 177 | static jint Unsafe_getArrayBaseOffsetForComponentType(JNIEnv* env, jclass, jobject component_class) { |
| 178 | ScopedFastNativeObjectAccess soa(env); |
| 179 | mirror::Class* component = soa.Decode<mirror::Class*>(component_class); |
| 180 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 181 | return mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value(); |
| 182 | } |
| 183 | |
| 184 | static jint Unsafe_getArrayIndexScaleForComponentType(JNIEnv* env, jclass, jobject component_class) { |
| 185 | ScopedFastNativeObjectAccess soa(env); |
| 186 | mirror::Class* component = soa.Decode<mirror::Class*>(component_class); |
| 187 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 188 | return Primitive::ComponentSize(primitive_type); |
| 189 | } |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 190 | |
Piotr Jastrzebski | d7fcf6e | 2015-05-05 12:54:00 +0100 | [diff] [blame] | 191 | static jint Unsafe_addressSize(JNIEnv* env, jobject) { |
| 192 | return sizeof(void*); |
| 193 | } |
| 194 | |
| 195 | static jint Unsafe_pageSize(JNIEnv* env, jobject) { |
| 196 | return sysconf(_SC_PAGESIZE); |
| 197 | } |
| 198 | |
| 199 | static jlong Unsafe_allocateMemory(JNIEnv* env, jobject, jlong bytes) { |
| 200 | ScopedFastNativeObjectAccess soa(env); |
| 201 | // bytes is nonnegative and fits into size_t |
| 202 | if (bytes < 0 || bytes != (jlong)(size_t) bytes) { |
| 203 | ThrowIllegalAccessException(nullptr, "wrong number of bytes"); |
| 204 | return 0; |
| 205 | } |
| 206 | void* mem = malloc(bytes); |
| 207 | if (mem == nullptr) { |
| 208 | soa.Self()->ThrowOutOfMemoryError("native alloc"); |
| 209 | return 0; |
| 210 | } |
| 211 | return (uintptr_t) mem; |
| 212 | } |
| 213 | |
| 214 | static void Unsafe_freeMemory(JNIEnv* env, jobject, jlong address) { |
| 215 | free((void*)(uintptr_t)address); |
| 216 | } |
| 217 | |
| 218 | static void Unsafe_setMemory(JNIEnv* env, jobject, jlong address, jlong bytes, jbyte value) { |
| 219 | memset((void*)(uintptr_t)address, value, bytes); |
| 220 | } |
| 221 | |
| 222 | static jbyte Unsafe_getByte$(JNIEnv* env, jobject, jlong address) { |
| 223 | return *reinterpret_cast<jbyte*>(address); |
| 224 | } |
| 225 | |
| 226 | static void Unsafe_putByte$(JNIEnv* env, jobject, jlong address, jbyte value) { |
| 227 | *reinterpret_cast<jbyte*>(address) = value; |
| 228 | } |
| 229 | |
| 230 | static jshort Unsafe_getShort$(JNIEnv* env, jobject, jlong address) { |
| 231 | return *reinterpret_cast<jshort*>(address); |
| 232 | } |
| 233 | |
| 234 | static void Unsafe_putShort$(JNIEnv* env, jobject, jlong address, jshort value) { |
| 235 | *reinterpret_cast<jshort*>(address) = value; |
| 236 | } |
| 237 | |
| 238 | static jchar Unsafe_getChar$(JNIEnv* env, jobject, jlong address) { |
| 239 | return *reinterpret_cast<jchar*>(address); |
| 240 | } |
| 241 | |
| 242 | static void Unsafe_putChar$(JNIEnv* env, jobject, jlong address, jchar value) { |
| 243 | *reinterpret_cast<jchar*>(address) = value; |
| 244 | } |
| 245 | |
| 246 | static jint Unsafe_getInt$(JNIEnv* env, jobject, jlong address) { |
| 247 | return *reinterpret_cast<jint*>(address); |
| 248 | } |
| 249 | |
| 250 | static void Unsafe_putInt$(JNIEnv* env, jobject, jlong address, jint value) { |
| 251 | *reinterpret_cast<jint*>(address) = value; |
| 252 | } |
| 253 | |
| 254 | static jlong Unsafe_getLong$(JNIEnv* env, jobject, jlong address) { |
| 255 | return *reinterpret_cast<jlong*>(address); |
| 256 | } |
| 257 | |
| 258 | static void Unsafe_putLong$(JNIEnv* env, jobject, jlong address, jlong value) { |
| 259 | *reinterpret_cast<jlong*>(address) = value; |
| 260 | } |
| 261 | |
| 262 | static jfloat Unsafe_getFloat$(JNIEnv* env, jobject, jlong address) { |
| 263 | return *reinterpret_cast<jfloat*>(address); |
| 264 | } |
| 265 | |
| 266 | static void Unsafe_putFloat$(JNIEnv* env, jobject, jlong address, jfloat value) { |
| 267 | *reinterpret_cast<jfloat*>(address) = value; |
| 268 | } |
| 269 | static jdouble Unsafe_getDouble$(JNIEnv* env, jobject, jlong address) { |
| 270 | return *reinterpret_cast<jdouble*>(address); |
| 271 | } |
| 272 | |
| 273 | static void Unsafe_putDouble$(JNIEnv* env, jobject, jlong address, jdouble value) { |
| 274 | *reinterpret_cast<jdouble*>(address) = value; |
| 275 | } |
| 276 | |
| 277 | static jlong Unsafe_getAddress(JNIEnv* env, jobject, jlong address) { |
| 278 | void* p = (void*)(uintptr_t)address; |
| 279 | return (uintptr_t)(*(void**)p); |
| 280 | } |
| 281 | |
| 282 | static void Unsafe_copyMemory(JNIEnv *env, jobject unsafe, jlong src, jlong dst, jlong size) { |
| 283 | if (size == 0) { |
| 284 | return; |
| 285 | } |
| 286 | // size is nonnegative and fits into size_t |
| 287 | if (size < 0 || size != (jlong)(size_t) size) { |
| 288 | ScopedFastNativeObjectAccess soa(env); |
| 289 | ThrowIllegalAccessException(nullptr, "wrong number of bytes"); |
| 290 | } |
| 291 | size_t sz = (size_t)size; |
| 292 | memcpy(reinterpret_cast<void *>(dst), reinterpret_cast<void *>(src), sz); |
| 293 | } |
| 294 | |
| 295 | template<typename T> |
| 296 | static void copyToArray(jlong srcAddr, mirror::PrimitiveArray<T>* array, size_t size) |
| 297 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 298 | const T* src = reinterpret_cast<T*>(srcAddr); |
| 299 | size_t sz = size / sizeof(T); |
| 300 | for (size_t i = 0; i < sz; ++i) { |
| 301 | array->Set(i, *(src + i)); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | template<typename T> |
| 306 | static void copyFromArray(jlong dstAddr, mirror::PrimitiveArray<T>* array, size_t size) |
| 307 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){ |
| 308 | T* dst = reinterpret_cast<T*>(dstAddr); |
| 309 | size_t sz = size / sizeof(T); |
| 310 | for (size_t i = 0; i < sz; ++i) { |
| 311 | *(dst + i) = array->Get(i); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void Unsafe_copyMemoryToPrimitiveArray(JNIEnv *env, |
| 316 | jobject unsafe, |
| 317 | jlong srcAddr, |
| 318 | jobject dstObj, |
| 319 | jlong dstOffset, |
| 320 | jlong size) { |
| 321 | ScopedObjectAccess soa(env); |
| 322 | if (size == 0) { |
| 323 | return; |
| 324 | } |
| 325 | // size is nonnegative and fits into size_t |
| 326 | if (size < 0 || size != (jlong)(size_t) size) { |
| 327 | ThrowIllegalAccessException(nullptr, "wrong number of bytes"); |
| 328 | } |
| 329 | size_t sz = (size_t)size; |
| 330 | mirror::Object* dst = soa.Decode<mirror::Object*>(dstObj); |
| 331 | mirror::Class* component_type = dst->GetClass()->GetComponentType(); |
| 332 | if (component_type->IsPrimitiveByte() || component_type->IsPrimitiveBoolean()) { |
| 333 | copyToArray(srcAddr, dst->AsByteSizedArray(), sz); |
| 334 | } else if (component_type->IsPrimitiveShort() || component_type->IsPrimitiveChar()) { |
| 335 | copyToArray(srcAddr, dst->AsShortSizedArray(), sz); |
| 336 | } else if (component_type->IsPrimitiveInt() || component_type->IsPrimitiveFloat()) { |
| 337 | copyToArray(srcAddr, dst->AsIntArray(), sz); |
| 338 | } else if (component_type->IsPrimitiveLong() || component_type->IsPrimitiveDouble()) { |
| 339 | copyToArray(srcAddr, dst->AsLongArray(), sz); |
| 340 | } else { |
| 341 | ThrowIllegalAccessException(nullptr, "not a primitive array"); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void Unsafe_copyMemoryFromPrimitiveArray(JNIEnv *env, |
| 346 | jobject unsafe, |
| 347 | jobject srcObj, |
| 348 | jlong srcOffset, |
| 349 | jlong dstAddr, |
| 350 | jlong size) { |
| 351 | ScopedObjectAccess soa(env); |
| 352 | if (size == 0) { |
| 353 | return; |
| 354 | } |
| 355 | // size is nonnegative and fits into size_t |
| 356 | if (size < 0 || size != (jlong)(size_t) size) { |
| 357 | ThrowIllegalAccessException(nullptr, "wrong number of bytes"); |
| 358 | } |
| 359 | size_t sz = (size_t)size; |
| 360 | mirror::Object* src = soa.Decode<mirror::Object*>(srcObj); |
| 361 | mirror::Class* component_type = src->GetClass()->GetComponentType(); |
| 362 | if (component_type->IsPrimitiveByte() || component_type->IsPrimitiveBoolean()) { |
| 363 | copyFromArray(dstAddr, src->AsByteSizedArray(), sz); |
| 364 | } else if (component_type->IsPrimitiveShort() || component_type->IsPrimitiveChar()) { |
| 365 | copyFromArray(dstAddr, src->AsShortSizedArray(), sz); |
| 366 | } else if (component_type->IsPrimitiveInt() || component_type->IsPrimitiveFloat()) { |
| 367 | copyFromArray(dstAddr, src->AsIntArray(), sz); |
| 368 | } else if (component_type->IsPrimitiveLong() || component_type->IsPrimitiveDouble()) { |
| 369 | copyFromArray(dstAddr, src->AsLongArray(), sz); |
| 370 | } else { |
| 371 | ThrowIllegalAccessException(nullptr, "not a primitive array"); |
| 372 | } |
| 373 | } |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 374 | static jboolean Unsafe_getBoolean(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 375 | ScopedFastNativeObjectAccess soa(env); |
| 376 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 377 | return obj->GetField8(MemberOffset(offset)); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void Unsafe_putBoolean(JNIEnv* env, jobject, jobject javaObj, jlong offset, jboolean newValue) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 381 | ScopedFastNativeObjectAccess soa(env); |
| 382 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 383 | // JNI must use non transactional mode (SetField8 is non-transactional). |
| 384 | obj->SetField8(MemberOffset(offset), newValue); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static jbyte Unsafe_getByte(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 388 | ScopedFastNativeObjectAccess soa(env); |
| 389 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 390 | return obj->GetField8(MemberOffset(offset)); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void Unsafe_putByte(JNIEnv* env, jobject, jobject javaObj, jlong offset, jbyte newValue) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 394 | ScopedFastNativeObjectAccess soa(env); |
| 395 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 396 | // JNI must use non transactional mode. |
| 397 | obj->SetField8(MemberOffset(offset), newValue); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static jchar Unsafe_getChar(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 401 | ScopedFastNativeObjectAccess soa(env); |
| 402 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 403 | return obj->GetField16(MemberOffset(offset)); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static void Unsafe_putChar(JNIEnv* env, jobject, jobject javaObj, jlong offset, jchar newValue) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 407 | ScopedFastNativeObjectAccess soa(env); |
| 408 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 409 | // JNI must use non transactional mode. |
| 410 | obj->SetField16(MemberOffset(offset), newValue); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static jshort Unsafe_getShort(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 414 | ScopedFastNativeObjectAccess soa(env); |
| 415 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 416 | return obj->GetField16(MemberOffset(offset)); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static void Unsafe_putShort(JNIEnv* env, jobject, jobject javaObj, jlong offset, jshort newValue) { |
Przemyslaw Szczepaniak | 5abc6f9 | 2015-06-30 11:05:37 +0100 | [diff] [blame^] | 420 | ScopedFastNativeObjectAccess soa(env); |
| 421 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 422 | // JNI must use non transactional mode. |
| 423 | obj->SetField16<false>(MemberOffset(offset), newValue); |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static jfloat Unsafe_getFloat(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 427 | ScopedFastNativeObjectAccess soa(env); |
| 428 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 429 | return obj->GetField32(MemberOffset(offset)); |
| 430 | } |
| 431 | |
| 432 | static void Unsafe_putFloat(JNIEnv* env, jobject, jobject javaObj, jlong offset, jfloat newValue) { |
| 433 | ScopedFastNativeObjectAccess soa(env); |
| 434 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 435 | // JNI must use non transactional mode. |
| 436 | obj->SetField32<false>(MemberOffset(offset), newValue); |
| 437 | } |
| 438 | |
| 439 | static jdouble Unsafe_getDouble(JNIEnv* env, jobject, jobject javaObj, jlong offset) { |
| 440 | ScopedFastNativeObjectAccess soa(env); |
| 441 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 442 | return obj->GetField64(MemberOffset(offset)); |
| 443 | } |
| 444 | |
| 445 | static void Unsafe_putDouble(JNIEnv* env, jobject, jobject javaObj, jlong offset, jdouble newValue) { |
| 446 | ScopedFastNativeObjectAccess soa(env); |
| 447 | mirror::Object* obj = soa.Decode<mirror::Object*>(javaObj); |
| 448 | // JNI must use non transactional mode. |
| 449 | obj->SetField64<false>(MemberOffset(offset), newValue); |
| 450 | } |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 451 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 452 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 453 | NATIVE_METHOD(Unsafe, compareAndSwapInt, "!(Ljava/lang/Object;JII)Z"), |
| 454 | NATIVE_METHOD(Unsafe, compareAndSwapLong, "!(Ljava/lang/Object;JJJ)Z"), |
| 455 | NATIVE_METHOD(Unsafe, compareAndSwapObject, "!(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"), |
| 456 | NATIVE_METHOD(Unsafe, getIntVolatile, "!(Ljava/lang/Object;J)I"), |
| 457 | NATIVE_METHOD(Unsafe, putIntVolatile, "!(Ljava/lang/Object;JI)V"), |
| 458 | NATIVE_METHOD(Unsafe, getLongVolatile, "!(Ljava/lang/Object;J)J"), |
| 459 | NATIVE_METHOD(Unsafe, putLongVolatile, "!(Ljava/lang/Object;JJ)V"), |
| 460 | NATIVE_METHOD(Unsafe, getObjectVolatile, "!(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 461 | NATIVE_METHOD(Unsafe, putObjectVolatile, "!(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 462 | NATIVE_METHOD(Unsafe, getInt, "!(Ljava/lang/Object;J)I"), |
| 463 | NATIVE_METHOD(Unsafe, putInt, "!(Ljava/lang/Object;JI)V"), |
| 464 | NATIVE_METHOD(Unsafe, putOrderedInt, "!(Ljava/lang/Object;JI)V"), |
| 465 | NATIVE_METHOD(Unsafe, getLong, "!(Ljava/lang/Object;J)J"), |
| 466 | NATIVE_METHOD(Unsafe, putLong, "!(Ljava/lang/Object;JJ)V"), |
| 467 | NATIVE_METHOD(Unsafe, putOrderedLong, "!(Ljava/lang/Object;JJ)V"), |
| 468 | NATIVE_METHOD(Unsafe, getObject, "!(Ljava/lang/Object;J)Ljava/lang/Object;"), |
| 469 | NATIVE_METHOD(Unsafe, putObject, "!(Ljava/lang/Object;JLjava/lang/Object;)V"), |
| 470 | NATIVE_METHOD(Unsafe, putOrderedObject, "!(Ljava/lang/Object;JLjava/lang/Object;)V"), |
Hiroshi Yamauchi | 4d2efce | 2014-02-10 16:19:09 -0800 | [diff] [blame] | 471 | NATIVE_METHOD(Unsafe, getArrayBaseOffsetForComponentType, "!(Ljava/lang/Class;)I"), |
| 472 | NATIVE_METHOD(Unsafe, getArrayIndexScaleForComponentType, "!(Ljava/lang/Class;)I"), |
Piotr Jastrzebski | d7fcf6e | 2015-05-05 12:54:00 +0100 | [diff] [blame] | 473 | NATIVE_METHOD(Unsafe, addressSize, "!()I"), |
| 474 | NATIVE_METHOD(Unsafe, pageSize, "!()I"), |
| 475 | NATIVE_METHOD(Unsafe, allocateMemory, "!(J)J"), |
| 476 | NATIVE_METHOD(Unsafe, freeMemory, "!(J)V"), |
| 477 | NATIVE_METHOD(Unsafe, setMemory, "!(JJB)V"), |
| 478 | NATIVE_METHOD(Unsafe, getByte$, "!(J)B"), |
| 479 | NATIVE_METHOD(Unsafe, putByte$, "!(JB)V"), |
| 480 | NATIVE_METHOD(Unsafe, getShort$, "!(J)S"), |
| 481 | NATIVE_METHOD(Unsafe, putShort$, "!(JS)V"), |
| 482 | NATIVE_METHOD(Unsafe, getChar$, "!(J)C"), |
| 483 | NATIVE_METHOD(Unsafe, putChar$, "!(JC)V"), |
| 484 | NATIVE_METHOD(Unsafe, getInt$, "!(J)I"), |
| 485 | NATIVE_METHOD(Unsafe, putInt$, "!(JI)V"), |
| 486 | NATIVE_METHOD(Unsafe, getLong$, "!(J)J"), |
| 487 | NATIVE_METHOD(Unsafe, putLong$, "!(JJ)V"), |
| 488 | NATIVE_METHOD(Unsafe, getFloat$, "!(J)F"), |
| 489 | NATIVE_METHOD(Unsafe, putFloat$, "!(JF)V"), |
| 490 | NATIVE_METHOD(Unsafe, getDouble$, "!(J)D"), |
| 491 | NATIVE_METHOD(Unsafe, putDouble$, "!(JD)V"), |
| 492 | NATIVE_METHOD(Unsafe, getAddress, "!(J)J"), |
| 493 | NATIVE_METHOD(Unsafe, copyMemory, "!(JJJ)V"), |
| 494 | NATIVE_METHOD(Unsafe, copyMemoryToPrimitiveArray, "!(JLjava/lang/Object;JJ)V"), |
| 495 | NATIVE_METHOD(Unsafe, copyMemoryFromPrimitiveArray, "!(Ljava/lang/Object;JJJ)V"), |
Piotr Jastrzebski | 8e73ea4 | 2015-05-07 09:41:00 +0100 | [diff] [blame] | 496 | NATIVE_METHOD(Unsafe, getBoolean, "!(Ljava/lang/Object;J)Z"), |
| 497 | NATIVE_METHOD(Unsafe, getByte, "!(Ljava/lang/Object;J)B"), |
| 498 | NATIVE_METHOD(Unsafe, getChar, "!(Ljava/lang/Object;J)C"), |
| 499 | NATIVE_METHOD(Unsafe, getShort, "!(Ljava/lang/Object;J)S"), |
| 500 | NATIVE_METHOD(Unsafe, getFloat, "!(Ljava/lang/Object;J)F"), |
| 501 | NATIVE_METHOD(Unsafe, getDouble, "!(Ljava/lang/Object;J)D"), |
| 502 | NATIVE_METHOD(Unsafe, putBoolean, "!(Ljava/lang/Object;JZ)V"), |
| 503 | NATIVE_METHOD(Unsafe, putByte, "!(Ljava/lang/Object;JB)V"), |
| 504 | NATIVE_METHOD(Unsafe, putChar, "!(Ljava/lang/Object;JC)V"), |
| 505 | NATIVE_METHOD(Unsafe, putShort, "!(Ljava/lang/Object;JS)V"), |
| 506 | NATIVE_METHOD(Unsafe, putFloat, "!(Ljava/lang/Object;JF)V"), |
| 507 | NATIVE_METHOD(Unsafe, putDouble, "!(Ljava/lang/Object;JD)V"), |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 508 | }; |
| 509 | |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 510 | void register_sun_misc_Unsafe(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 511 | REGISTER_NATIVE_METHODS("sun/misc/Unsafe"); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | } // namespace art |