src/share/vm/prims/unsafe.cpp

Tue, 25 Mar 2014 17:07:36 -0700

author
kvn
date
Tue, 25 Mar 2014 17:07:36 -0700
changeset 6518
62c54fcc0a35
parent 6502
3514ee402842
parent 6346
56cd09c4a5c9
child 6680
78bbf4d43a14
permissions
-rw-r--r--

Merge

duke@435 1 /*
sla@5237 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/vmSymbols.hpp"
jprovino@4542 27 #include "utilities/macros.hpp"
jprovino@4542 28 #if INCLUDE_ALL_GCS
johnc@2781 29 #include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
jprovino@4542 30 #endif // INCLUDE_ALL_GCS
stefank@2314 31 #include "memory/allocation.inline.hpp"
stefank@2314 32 #include "prims/jni.h"
stefank@2314 33 #include "prims/jvm.h"
stefank@2314 34 #include "runtime/globals.hpp"
stefank@2314 35 #include "runtime/interfaceSupport.hpp"
stefank@2314 36 #include "runtime/reflection.hpp"
stefank@2314 37 #include "runtime/synchronizer.hpp"
stefank@2314 38 #include "services/threadService.hpp"
sla@5237 39 #include "trace/tracing.hpp"
stefank@2314 40 #include "utilities/copy.hpp"
stefank@2314 41 #include "utilities/dtrace.hpp"
stefank@2314 42
duke@435 43 /*
duke@435 44 * Implementation of class sun.misc.Unsafe
duke@435 45 */
duke@435 46
dcubed@3202 47 #ifndef USDT2
fparain@1759 48 HS_DTRACE_PROBE_DECL3(hotspot, thread__park__begin, uintptr_t, int, long long);
fparain@1759 49 HS_DTRACE_PROBE_DECL1(hotspot, thread__park__end, uintptr_t);
fparain@1759 50 HS_DTRACE_PROBE_DECL1(hotspot, thread__unpark, uintptr_t);
dcubed@3202 51 #endif /* !USDT2 */
fparain@1759 52
duke@435 53 #define MAX_OBJECT_SIZE \
duke@435 54 ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \
duke@435 55 + ((julong)max_jint * sizeof(double)) )
duke@435 56
duke@435 57
duke@435 58 #define UNSAFE_ENTRY(result_type, header) \
duke@435 59 JVM_ENTRY(result_type, header)
duke@435 60
duke@435 61 // Can't use UNSAFE_LEAF because it has the signature of a straight
duke@435 62 // call into the runtime (just like JVM_LEAF, funny that) but it's
duke@435 63 // called like a Java Native and thus the wrapper built for it passes
duke@435 64 // arguments like a JNI call. It expects those arguments to be popped
duke@435 65 // from the stack on Intel like all good JNI args are, and adjusts the
duke@435 66 // stack according. Since the JVM_LEAF call expects no extra
duke@435 67 // arguments the stack isn't popped in the C code, is pushed by the
duke@435 68 // wrapper and we get sick.
duke@435 69 //#define UNSAFE_LEAF(result_type, header) \
duke@435 70 // JVM_LEAF(result_type, header)
duke@435 71
duke@435 72 #define UNSAFE_END JVM_END
duke@435 73
duke@435 74 #define UnsafeWrapper(arg) /*nothing, for the present*/
duke@435 75
duke@435 76
duke@435 77 inline void* addr_from_java(jlong addr) {
duke@435 78 // This assert fails in a variety of ways on 32-bit systems.
duke@435 79 // It is impossible to predict whether native code that converts
duke@435 80 // pointers to longs will sign-extend or zero-extend the addresses.
duke@435 81 //assert(addr == (uintptr_t)addr, "must not be odd high bits");
duke@435 82 return (void*)(uintptr_t)addr;
duke@435 83 }
duke@435 84
duke@435 85 inline jlong addr_to_java(void* p) {
duke@435 86 assert(p == (void*)(uintptr_t)p, "must not be odd high bits");
duke@435 87 return (uintptr_t)p;
duke@435 88 }
duke@435 89
duke@435 90
duke@435 91 // Note: The VM's obj_field and related accessors use byte-scaled
duke@435 92 // ("unscaled") offsets, just as the unsafe methods do.
duke@435 93
duke@435 94 // However, the method Unsafe.fieldOffset explicitly declines to
duke@435 95 // guarantee this. The field offset values manipulated by the Java user
duke@435 96 // through the Unsafe API are opaque cookies that just happen to be byte
duke@435 97 // offsets. We represent this state of affairs by passing the cookies
duke@435 98 // through conversion functions when going between the VM and the Unsafe API.
duke@435 99 // The conversion functions just happen to be no-ops at present.
duke@435 100
duke@435 101 inline jlong field_offset_to_byte_offset(jlong field_offset) {
duke@435 102 return field_offset;
duke@435 103 }
duke@435 104
duke@435 105 inline jlong field_offset_from_byte_offset(jlong byte_offset) {
duke@435 106 return byte_offset;
duke@435 107 }
duke@435 108
duke@435 109 inline jint invocation_key_from_method_slot(jint slot) {
duke@435 110 return slot;
duke@435 111 }
duke@435 112
duke@435 113 inline jint invocation_key_to_method_slot(jint key) {
duke@435 114 return key;
duke@435 115 }
duke@435 116
duke@435 117 inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
duke@435 118 jlong byte_offset = field_offset_to_byte_offset(field_offset);
duke@435 119 #ifdef ASSERT
duke@435 120 if (p != NULL) {
duke@435 121 assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset");
duke@435 122 if (byte_offset == (jint)byte_offset) {
duke@435 123 void* ptr_plus_disp = (address)p + byte_offset;
coleenp@548 124 assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp,
duke@435 125 "raw [ptr+disp] must be consistent with oop::field_base");
duke@435 126 }
kvn@4200 127 jlong p_size = HeapWordSize * (jlong)(p->size());
kvn@4200 128 assert(byte_offset < p_size, err_msg("Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, byte_offset, p_size));
duke@435 129 }
duke@435 130 #endif
duke@435 131 if (sizeof(char*) == sizeof(jint)) // (this constant folds!)
duke@435 132 return (address)p + (jint) byte_offset;
duke@435 133 else
duke@435 134 return (address)p + byte_offset;
duke@435 135 }
duke@435 136
duke@435 137 // Externally callable versions:
duke@435 138 // (Use these in compiler intrinsics which emulate unsafe primitives.)
duke@435 139 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
duke@435 140 return field_offset;
duke@435 141 }
duke@435 142 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
duke@435 143 return byte_offset;
duke@435 144 }
duke@435 145 jint Unsafe_invocation_key_from_method_slot(jint slot) {
duke@435 146 return invocation_key_from_method_slot(slot);
duke@435 147 }
duke@435 148 jint Unsafe_invocation_key_to_method_slot(jint key) {
duke@435 149 return invocation_key_to_method_slot(key);
duke@435 150 }
duke@435 151
duke@435 152
duke@435 153 ///// Data in the Java heap.
duke@435 154
duke@435 155 #define GET_FIELD(obj, offset, type_name, v) \
duke@435 156 oop p = JNIHandles::resolve(obj); \
duke@435 157 type_name v = *(type_name*)index_oop_from_field_offset_long(p, offset)
duke@435 158
duke@435 159 #define SET_FIELD(obj, offset, type_name, x) \
duke@435 160 oop p = JNIHandles::resolve(obj); \
duke@435 161 *(type_name*)index_oop_from_field_offset_long(p, offset) = x
duke@435 162
duke@435 163 #define GET_FIELD_VOLATILE(obj, offset, type_name, v) \
duke@435 164 oop p = JNIHandles::resolve(obj); \
goetz@6502 165 if (support_IRIW_for_not_multiple_copy_atomic_cpu) { \
goetz@6502 166 OrderAccess::fence(); \
goetz@6502 167 } \
kvn@2434 168 volatile type_name v = OrderAccess::load_acquire((volatile type_name*)index_oop_from_field_offset_long(p, offset));
duke@435 169
duke@435 170 #define SET_FIELD_VOLATILE(obj, offset, type_name, x) \
duke@435 171 oop p = JNIHandles::resolve(obj); \
kvn@2434 172 OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), x);
duke@435 173
coleenp@548 174 // Macros for oops that check UseCompressedOops
coleenp@548 175
coleenp@548 176 #define GET_OOP_FIELD(obj, offset, v) \
coleenp@548 177 oop p = JNIHandles::resolve(obj); \
coleenp@548 178 oop v; \
coleenp@548 179 if (UseCompressedOops) { \
coleenp@548 180 narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); \
coleenp@548 181 v = oopDesc::decode_heap_oop(n); \
coleenp@548 182 } else { \
coleenp@548 183 v = *(oop*)index_oop_from_field_offset_long(p, offset); \
coleenp@548 184 }
coleenp@548 185
coleenp@548 186
duke@435 187 // Get/SetObject must be special-cased, since it works with handles.
duke@435 188
duke@435 189 // The xxx140 variants for backward compatibility do not allow a full-width offset.
duke@435 190 UNSAFE_ENTRY(jobject, Unsafe_GetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset))
duke@435 191 UnsafeWrapper("Unsafe_GetObject");
duke@435 192 if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException());
coleenp@548 193 GET_OOP_FIELD(obj, offset, v)
johnc@2781 194 jobject ret = JNIHandles::make_local(env, v);
jprovino@4542 195 #if INCLUDE_ALL_GCS
johnc@2781 196 // We could be accessing the referent field in a reference
johnc@2781 197 // object. If G1 is enabled then we need to register a non-null
johnc@2781 198 // referent with the SATB barrier.
johnc@2781 199 if (UseG1GC) {
johnc@2781 200 bool needs_barrier = false;
johnc@2781 201
johnc@2781 202 if (ret != NULL) {
johnc@2781 203 if (offset == java_lang_ref_Reference::referent_offset) {
johnc@2781 204 oop o = JNIHandles::resolve_non_null(obj);
coleenp@4037 205 Klass* k = o->klass();
coleenp@4037 206 if (InstanceKlass::cast(k)->reference_type() != REF_NONE) {
coleenp@4037 207 assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
johnc@2781 208 needs_barrier = true;
johnc@2781 209 }
johnc@2781 210 }
johnc@2781 211 }
johnc@2781 212
johnc@2781 213 if (needs_barrier) {
johnc@2781 214 oop referent = JNIHandles::resolve(ret);
johnc@2781 215 G1SATBCardTableModRefBS::enqueue(referent);
johnc@2781 216 }
johnc@2781 217 }
jprovino@4542 218 #endif // INCLUDE_ALL_GCS
johnc@2781 219 return ret;
duke@435 220 UNSAFE_END
duke@435 221
duke@435 222 UNSAFE_ENTRY(void, Unsafe_SetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jobject x_h))
duke@435 223 UnsafeWrapper("Unsafe_SetObject");
duke@435 224 if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException());
duke@435 225 oop x = JNIHandles::resolve(x_h);
duke@435 226 //SET_FIELD(obj, offset, oop, x);
duke@435 227 oop p = JNIHandles::resolve(obj);
coleenp@548 228 if (UseCompressedOops) {
coleenp@548 229 if (x != NULL) {
coleenp@548 230 // If there is a heap base pointer, we are obliged to emit a store barrier.
coleenp@548 231 oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x);
coleenp@548 232 } else {
coleenp@548 233 narrowOop n = oopDesc::encode_heap_oop_not_null(x);
coleenp@548 234 *(narrowOop*)index_oop_from_field_offset_long(p, offset) = n;
coleenp@548 235 }
duke@435 236 } else {
coleenp@548 237 if (x != NULL) {
coleenp@548 238 // If there is a heap base pointer, we are obliged to emit a store barrier.
coleenp@548 239 oop_store((oop*)index_oop_from_field_offset_long(p, offset), x);
coleenp@548 240 } else {
coleenp@548 241 *(oop*)index_oop_from_field_offset_long(p, offset) = x;
coleenp@548 242 }
duke@435 243 }
duke@435 244 UNSAFE_END
duke@435 245
duke@435 246 // The normal variants allow a null base pointer with an arbitrary address.
duke@435 247 // But if the base pointer is non-null, the offset should make some sense.
duke@435 248 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
duke@435 249 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
duke@435 250 UnsafeWrapper("Unsafe_GetObject");
coleenp@548 251 GET_OOP_FIELD(obj, offset, v)
johnc@2781 252 jobject ret = JNIHandles::make_local(env, v);
jprovino@4542 253 #if INCLUDE_ALL_GCS
johnc@2781 254 // We could be accessing the referent field in a reference
johnc@2781 255 // object. If G1 is enabled then we need to register non-null
johnc@2781 256 // referent with the SATB barrier.
johnc@2781 257 if (UseG1GC) {
johnc@2781 258 bool needs_barrier = false;
johnc@2781 259
johnc@2781 260 if (ret != NULL) {
johnc@2781 261 if (offset == java_lang_ref_Reference::referent_offset && obj != NULL) {
johnc@2781 262 oop o = JNIHandles::resolve(obj);
coleenp@4037 263 Klass* k = o->klass();
coleenp@4037 264 if (InstanceKlass::cast(k)->reference_type() != REF_NONE) {
coleenp@4037 265 assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity");
johnc@2781 266 needs_barrier = true;
johnc@2781 267 }
johnc@2781 268 }
johnc@2781 269 }
johnc@2781 270
johnc@2781 271 if (needs_barrier) {
johnc@2781 272 oop referent = JNIHandles::resolve(ret);
johnc@2781 273 G1SATBCardTableModRefBS::enqueue(referent);
johnc@2781 274 }
johnc@2781 275 }
jprovino@4542 276 #endif // INCLUDE_ALL_GCS
johnc@2781 277 return ret;
duke@435 278 UNSAFE_END
duke@435 279
duke@435 280 UNSAFE_ENTRY(void, Unsafe_SetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
duke@435 281 UnsafeWrapper("Unsafe_SetObject");
duke@435 282 oop x = JNIHandles::resolve(x_h);
duke@435 283 oop p = JNIHandles::resolve(obj);
coleenp@548 284 if (UseCompressedOops) {
coleenp@548 285 oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x);
coleenp@548 286 } else {
coleenp@548 287 oop_store((oop*)index_oop_from_field_offset_long(p, offset), x);
coleenp@548 288 }
duke@435 289 UNSAFE_END
duke@435 290
duke@435 291 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
duke@435 292 UnsafeWrapper("Unsafe_GetObjectVolatile");
twisti@3928 293 oop p = JNIHandles::resolve(obj);
twisti@3928 294 void* addr = index_oop_from_field_offset_long(p, offset);
twisti@3928 295 volatile oop v;
twisti@3928 296 if (UseCompressedOops) {
twisti@3928 297 volatile narrowOop n = *(volatile narrowOop*) addr;
hseigel@5784 298 (void)const_cast<oop&>(v = oopDesc::decode_heap_oop(n));
twisti@3928 299 } else {
hseigel@5784 300 (void)const_cast<oop&>(v = *(volatile oop*) addr);
twisti@3928 301 }
twisti@3928 302 OrderAccess::acquire();
duke@435 303 return JNIHandles::make_local(env, v);
duke@435 304 UNSAFE_END
duke@435 305
duke@435 306 UNSAFE_ENTRY(void, Unsafe_SetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
duke@435 307 UnsafeWrapper("Unsafe_SetObjectVolatile");
duke@435 308 oop x = JNIHandles::resolve(x_h);
duke@435 309 oop p = JNIHandles::resolve(obj);
kvn@2434 310 void* addr = index_oop_from_field_offset_long(p, offset);
kvn@2434 311 OrderAccess::release();
coleenp@548 312 if (UseCompressedOops) {
kvn@2434 313 oop_store((narrowOop*)addr, x);
coleenp@548 314 } else {
kvn@2434 315 oop_store((oop*)addr, x);
coleenp@548 316 }
duke@435 317 OrderAccess::fence();
duke@435 318 UNSAFE_END
duke@435 319
bpittore@5067 320 #ifndef SUPPORTS_NATIVE_CX8
kvn@2434 321 // Keep old code for platforms which may not have atomic jlong (8 bytes) instructions
kvn@2434 322
duke@435 323 // Volatile long versions must use locks if !VM_Version::supports_cx8().
duke@435 324 // support_cx8 is a surrogate for 'supports atomic long memory ops'.
duke@435 325
duke@435 326 UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
duke@435 327 UnsafeWrapper("Unsafe_GetLongVolatile");
duke@435 328 {
duke@435 329 if (VM_Version::supports_cx8()) {
duke@435 330 GET_FIELD_VOLATILE(obj, offset, jlong, v);
duke@435 331 return v;
duke@435 332 }
duke@435 333 else {
duke@435 334 Handle p (THREAD, JNIHandles::resolve(obj));
duke@435 335 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
duke@435 336 ObjectLocker ol(p, THREAD);
duke@435 337 jlong value = *addr;
duke@435 338 return value;
duke@435 339 }
duke@435 340 }
duke@435 341 UNSAFE_END
duke@435 342
duke@435 343 UNSAFE_ENTRY(void, Unsafe_SetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
duke@435 344 UnsafeWrapper("Unsafe_SetLongVolatile");
duke@435 345 {
duke@435 346 if (VM_Version::supports_cx8()) {
duke@435 347 SET_FIELD_VOLATILE(obj, offset, jlong, x);
duke@435 348 }
duke@435 349 else {
duke@435 350 Handle p (THREAD, JNIHandles::resolve(obj));
duke@435 351 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
duke@435 352 ObjectLocker ol(p, THREAD);
duke@435 353 *addr = x;
duke@435 354 }
duke@435 355 }
duke@435 356 UNSAFE_END
duke@435 357
bpittore@5067 358 #endif // not SUPPORTS_NATIVE_CX8
duke@435 359
duke@435 360 #define DEFINE_GETSETOOP(jboolean, Boolean) \
duke@435 361 \
duke@435 362 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) \
duke@435 363 UnsafeWrapper("Unsafe_Get"#Boolean); \
duke@435 364 if (obj == NULL) THROW_0(vmSymbols::java_lang_NullPointerException()); \
duke@435 365 GET_FIELD(obj, offset, jboolean, v); \
duke@435 366 return v; \
duke@435 367 UNSAFE_END \
duke@435 368 \
duke@435 369 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jboolean x)) \
duke@435 370 UnsafeWrapper("Unsafe_Set"#Boolean); \
duke@435 371 if (obj == NULL) THROW(vmSymbols::java_lang_NullPointerException()); \
duke@435 372 SET_FIELD(obj, offset, jboolean, x); \
duke@435 373 UNSAFE_END \
duke@435 374 \
duke@435 375 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
duke@435 376 UnsafeWrapper("Unsafe_Get"#Boolean); \
duke@435 377 GET_FIELD(obj, offset, jboolean, v); \
duke@435 378 return v; \
duke@435 379 UNSAFE_END \
duke@435 380 \
duke@435 381 UNSAFE_ENTRY(void, Unsafe_Set##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
duke@435 382 UnsafeWrapper("Unsafe_Set"#Boolean); \
duke@435 383 SET_FIELD(obj, offset, jboolean, x); \
duke@435 384 UNSAFE_END \
duke@435 385 \
duke@435 386 // END DEFINE_GETSETOOP.
duke@435 387
kvn@2434 388 DEFINE_GETSETOOP(jboolean, Boolean)
kvn@2434 389 DEFINE_GETSETOOP(jbyte, Byte)
kvn@2434 390 DEFINE_GETSETOOP(jshort, Short);
kvn@2434 391 DEFINE_GETSETOOP(jchar, Char);
kvn@2434 392 DEFINE_GETSETOOP(jint, Int);
kvn@2434 393 DEFINE_GETSETOOP(jlong, Long);
kvn@2434 394 DEFINE_GETSETOOP(jfloat, Float);
kvn@2434 395 DEFINE_GETSETOOP(jdouble, Double);
kvn@2434 396
kvn@2434 397 #undef DEFINE_GETSETOOP
duke@435 398
duke@435 399 #define DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) \
duke@435 400 \
duke@435 401 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
duke@435 402 UnsafeWrapper("Unsafe_Get"#Boolean); \
duke@435 403 GET_FIELD_VOLATILE(obj, offset, jboolean, v); \
duke@435 404 return v; \
duke@435 405 UNSAFE_END \
duke@435 406 \
duke@435 407 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
duke@435 408 UnsafeWrapper("Unsafe_Set"#Boolean); \
duke@435 409 SET_FIELD_VOLATILE(obj, offset, jboolean, x); \
duke@435 410 UNSAFE_END \
duke@435 411 \
duke@435 412 // END DEFINE_GETSETOOP_VOLATILE.
duke@435 413
duke@435 414 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean)
duke@435 415 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte)
duke@435 416 DEFINE_GETSETOOP_VOLATILE(jshort, Short);
duke@435 417 DEFINE_GETSETOOP_VOLATILE(jchar, Char);
duke@435 418 DEFINE_GETSETOOP_VOLATILE(jint, Int);
duke@435 419 DEFINE_GETSETOOP_VOLATILE(jfloat, Float);
duke@435 420 DEFINE_GETSETOOP_VOLATILE(jdouble, Double);
duke@435 421
bpittore@5067 422 #ifdef SUPPORTS_NATIVE_CX8
kvn@2434 423 DEFINE_GETSETOOP_VOLATILE(jlong, Long);
kvn@2434 424 #endif
kvn@2434 425
kvn@2434 426 #undef DEFINE_GETSETOOP_VOLATILE
duke@435 427
duke@435 428 // The non-intrinsified versions of setOrdered just use setVolatile
duke@435 429
kvn@2434 430 UNSAFE_ENTRY(void, Unsafe_SetOrderedInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint x))
kvn@2434 431 UnsafeWrapper("Unsafe_SetOrderedInt");
kvn@2434 432 SET_FIELD_VOLATILE(obj, offset, jint, x);
duke@435 433 UNSAFE_END
duke@435 434
duke@435 435 UNSAFE_ENTRY(void, Unsafe_SetOrderedObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
duke@435 436 UnsafeWrapper("Unsafe_SetOrderedObject");
duke@435 437 oop x = JNIHandles::resolve(x_h);
duke@435 438 oop p = JNIHandles::resolve(obj);
kvn@2434 439 void* addr = index_oop_from_field_offset_long(p, offset);
kvn@2434 440 OrderAccess::release();
coleenp@548 441 if (UseCompressedOops) {
kvn@2434 442 oop_store((narrowOop*)addr, x);
coleenp@548 443 } else {
kvn@2434 444 oop_store((oop*)addr, x);
coleenp@548 445 }
duke@435 446 OrderAccess::fence();
duke@435 447 UNSAFE_END
duke@435 448
duke@435 449 UNSAFE_ENTRY(void, Unsafe_SetOrderedLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
duke@435 450 UnsafeWrapper("Unsafe_SetOrderedLong");
bpittore@5067 451 #ifdef SUPPORTS_NATIVE_CX8
kvn@2434 452 SET_FIELD_VOLATILE(obj, offset, jlong, x);
kvn@2434 453 #else
kvn@2434 454 // Keep old code for platforms which may not have atomic long (8 bytes) instructions
duke@435 455 {
duke@435 456 if (VM_Version::supports_cx8()) {
duke@435 457 SET_FIELD_VOLATILE(obj, offset, jlong, x);
duke@435 458 }
duke@435 459 else {
duke@435 460 Handle p (THREAD, JNIHandles::resolve(obj));
duke@435 461 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
duke@435 462 ObjectLocker ol(p, THREAD);
duke@435 463 *addr = x;
duke@435 464 }
duke@435 465 }
kvn@2434 466 #endif
duke@435 467 UNSAFE_END
duke@435 468
kvn@4361 469 UNSAFE_ENTRY(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe))
kvn@4361 470 UnsafeWrapper("Unsafe_LoadFence");
kvn@4361 471 OrderAccess::acquire();
kvn@4361 472 UNSAFE_END
kvn@4361 473
kvn@4361 474 UNSAFE_ENTRY(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe))
kvn@4361 475 UnsafeWrapper("Unsafe_StoreFence");
kvn@4361 476 OrderAccess::release();
kvn@4361 477 UNSAFE_END
kvn@4361 478
kvn@4361 479 UNSAFE_ENTRY(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe))
kvn@4361 480 UnsafeWrapper("Unsafe_FullFence");
kvn@4361 481 OrderAccess::fence();
kvn@4361 482 UNSAFE_END
kvn@4361 483
duke@435 484 ////// Data in the C heap.
duke@435 485
duke@435 486 // Note: These do not throw NullPointerException for bad pointers.
duke@435 487 // They just crash. Only a oop base pointer can generate a NullPointerException.
duke@435 488 //
duke@435 489 #define DEFINE_GETSETNATIVE(java_type, Type, native_type) \
duke@435 490 \
duke@435 491 UNSAFE_ENTRY(java_type, Unsafe_GetNative##Type(JNIEnv *env, jobject unsafe, jlong addr)) \
duke@435 492 UnsafeWrapper("Unsafe_GetNative"#Type); \
duke@435 493 void* p = addr_from_java(addr); \
duke@435 494 JavaThread* t = JavaThread::current(); \
duke@435 495 t->set_doing_unsafe_access(true); \
duke@435 496 java_type x = *(volatile native_type*)p; \
duke@435 497 t->set_doing_unsafe_access(false); \
duke@435 498 return x; \
duke@435 499 UNSAFE_END \
duke@435 500 \
duke@435 501 UNSAFE_ENTRY(void, Unsafe_SetNative##Type(JNIEnv *env, jobject unsafe, jlong addr, java_type x)) \
duke@435 502 UnsafeWrapper("Unsafe_SetNative"#Type); \
duke@435 503 JavaThread* t = JavaThread::current(); \
duke@435 504 t->set_doing_unsafe_access(true); \
duke@435 505 void* p = addr_from_java(addr); \
duke@435 506 *(volatile native_type*)p = x; \
duke@435 507 t->set_doing_unsafe_access(false); \
duke@435 508 UNSAFE_END \
duke@435 509 \
duke@435 510 // END DEFINE_GETSETNATIVE.
duke@435 511
duke@435 512 DEFINE_GETSETNATIVE(jbyte, Byte, signed char)
duke@435 513 DEFINE_GETSETNATIVE(jshort, Short, signed short);
duke@435 514 DEFINE_GETSETNATIVE(jchar, Char, unsigned short);
duke@435 515 DEFINE_GETSETNATIVE(jint, Int, jint);
duke@435 516 // no long -- handled specially
duke@435 517 DEFINE_GETSETNATIVE(jfloat, Float, float);
duke@435 518 DEFINE_GETSETNATIVE(jdouble, Double, double);
duke@435 519
duke@435 520 #undef DEFINE_GETSETNATIVE
duke@435 521
duke@435 522 UNSAFE_ENTRY(jlong, Unsafe_GetNativeLong(JNIEnv *env, jobject unsafe, jlong addr))
duke@435 523 UnsafeWrapper("Unsafe_GetNativeLong");
duke@435 524 JavaThread* t = JavaThread::current();
duke@435 525 // We do it this way to avoid problems with access to heap using 64
duke@435 526 // bit loads, as jlong in heap could be not 64-bit aligned, and on
duke@435 527 // some CPUs (SPARC) it leads to SIGBUS.
duke@435 528 t->set_doing_unsafe_access(true);
duke@435 529 void* p = addr_from_java(addr);
duke@435 530 jlong x;
duke@435 531 if (((intptr_t)p & 7) == 0) {
duke@435 532 // jlong is aligned, do a volatile access
duke@435 533 x = *(volatile jlong*)p;
duke@435 534 } else {
duke@435 535 jlong_accessor acc;
duke@435 536 acc.words[0] = ((volatile jint*)p)[0];
duke@435 537 acc.words[1] = ((volatile jint*)p)[1];
duke@435 538 x = acc.long_value;
duke@435 539 }
duke@435 540 t->set_doing_unsafe_access(false);
duke@435 541 return x;
duke@435 542 UNSAFE_END
duke@435 543
duke@435 544 UNSAFE_ENTRY(void, Unsafe_SetNativeLong(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
duke@435 545 UnsafeWrapper("Unsafe_SetNativeLong");
duke@435 546 JavaThread* t = JavaThread::current();
duke@435 547 // see comment for Unsafe_GetNativeLong
duke@435 548 t->set_doing_unsafe_access(true);
duke@435 549 void* p = addr_from_java(addr);
duke@435 550 if (((intptr_t)p & 7) == 0) {
duke@435 551 // jlong is aligned, do a volatile access
duke@435 552 *(volatile jlong*)p = x;
duke@435 553 } else {
duke@435 554 jlong_accessor acc;
duke@435 555 acc.long_value = x;
duke@435 556 ((volatile jint*)p)[0] = acc.words[0];
duke@435 557 ((volatile jint*)p)[1] = acc.words[1];
duke@435 558 }
duke@435 559 t->set_doing_unsafe_access(false);
duke@435 560 UNSAFE_END
duke@435 561
duke@435 562
duke@435 563 UNSAFE_ENTRY(jlong, Unsafe_GetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr))
duke@435 564 UnsafeWrapper("Unsafe_GetNativeAddress");
duke@435 565 void* p = addr_from_java(addr);
duke@435 566 return addr_to_java(*(void**)p);
duke@435 567 UNSAFE_END
duke@435 568
duke@435 569 UNSAFE_ENTRY(void, Unsafe_SetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
duke@435 570 UnsafeWrapper("Unsafe_SetNativeAddress");
duke@435 571 void* p = addr_from_java(addr);
duke@435 572 *(void**)p = addr_from_java(x);
duke@435 573 UNSAFE_END
duke@435 574
duke@435 575
duke@435 576 ////// Allocation requests
duke@435 577
duke@435 578 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls))
duke@435 579 UnsafeWrapper("Unsafe_AllocateInstance");
duke@435 580 {
duke@435 581 ThreadToNativeFromVM ttnfv(thread);
duke@435 582 return env->AllocObject(cls);
duke@435 583 }
duke@435 584 UNSAFE_END
duke@435 585
duke@435 586 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong size))
duke@435 587 UnsafeWrapper("Unsafe_AllocateMemory");
duke@435 588 size_t sz = (size_t)size;
duke@435 589 if (sz != (julong)size || size < 0) {
duke@435 590 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
duke@435 591 }
duke@435 592 if (sz == 0) {
duke@435 593 return 0;
duke@435 594 }
duke@435 595 sz = round_to(sz, HeapWordSize);
zgu@3900 596 void* x = os::malloc(sz, mtInternal);
duke@435 597 if (x == NULL) {
duke@435 598 THROW_0(vmSymbols::java_lang_OutOfMemoryError());
duke@435 599 }
duke@435 600 //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize);
duke@435 601 return addr_to_java(x);
duke@435 602 UNSAFE_END
duke@435 603
duke@435 604 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size))
duke@435 605 UnsafeWrapper("Unsafe_ReallocateMemory");
duke@435 606 void* p = addr_from_java(addr);
duke@435 607 size_t sz = (size_t)size;
duke@435 608 if (sz != (julong)size || size < 0) {
duke@435 609 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
duke@435 610 }
duke@435 611 if (sz == 0) {
duke@435 612 os::free(p);
duke@435 613 return 0;
duke@435 614 }
duke@435 615 sz = round_to(sz, HeapWordSize);
zgu@3900 616 void* x = (p == NULL) ? os::malloc(sz, mtInternal) : os::realloc(p, sz, mtInternal);
duke@435 617 if (x == NULL) {
duke@435 618 THROW_0(vmSymbols::java_lang_OutOfMemoryError());
duke@435 619 }
duke@435 620 return addr_to_java(x);
duke@435 621 UNSAFE_END
duke@435 622
duke@435 623 UNSAFE_ENTRY(void, Unsafe_FreeMemory(JNIEnv *env, jobject unsafe, jlong addr))
duke@435 624 UnsafeWrapper("Unsafe_FreeMemory");
duke@435 625 void* p = addr_from_java(addr);
duke@435 626 if (p == NULL) {
duke@435 627 return;
duke@435 628 }
duke@435 629 os::free(p);
duke@435 630 UNSAFE_END
duke@435 631
duke@435 632 UNSAFE_ENTRY(void, Unsafe_SetMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size, jbyte value))
duke@435 633 UnsafeWrapper("Unsafe_SetMemory");
duke@435 634 size_t sz = (size_t)size;
duke@435 635 if (sz != (julong)size || size < 0) {
duke@435 636 THROW(vmSymbols::java_lang_IllegalArgumentException());
duke@435 637 }
duke@435 638 char* p = (char*) addr_from_java(addr);
duke@435 639 Copy::fill_to_memory_atomic(p, sz, value);
duke@435 640 UNSAFE_END
duke@435 641
duke@435 642 UNSAFE_ENTRY(void, Unsafe_SetMemory2(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value))
duke@435 643 UnsafeWrapper("Unsafe_SetMemory");
duke@435 644 size_t sz = (size_t)size;
duke@435 645 if (sz != (julong)size || size < 0) {
duke@435 646 THROW(vmSymbols::java_lang_IllegalArgumentException());
duke@435 647 }
duke@435 648 oop base = JNIHandles::resolve(obj);
duke@435 649 void* p = index_oop_from_field_offset_long(base, offset);
duke@435 650 Copy::fill_to_memory_atomic(p, sz, value);
duke@435 651 UNSAFE_END
duke@435 652
duke@435 653 UNSAFE_ENTRY(void, Unsafe_CopyMemory(JNIEnv *env, jobject unsafe, jlong srcAddr, jlong dstAddr, jlong size))
duke@435 654 UnsafeWrapper("Unsafe_CopyMemory");
duke@435 655 if (size == 0) {
duke@435 656 return;
duke@435 657 }
duke@435 658 size_t sz = (size_t)size;
duke@435 659 if (sz != (julong)size || size < 0) {
duke@435 660 THROW(vmSymbols::java_lang_IllegalArgumentException());
duke@435 661 }
duke@435 662 void* src = addr_from_java(srcAddr);
duke@435 663 void* dst = addr_from_java(dstAddr);
duke@435 664 Copy::conjoint_memory_atomic(src, dst, sz);
duke@435 665 UNSAFE_END
duke@435 666
duke@435 667 UNSAFE_ENTRY(void, Unsafe_CopyMemory2(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size))
duke@435 668 UnsafeWrapper("Unsafe_CopyMemory");
duke@435 669 if (size == 0) {
duke@435 670 return;
duke@435 671 }
duke@435 672 size_t sz = (size_t)size;
duke@435 673 if (sz != (julong)size || size < 0) {
duke@435 674 THROW(vmSymbols::java_lang_IllegalArgumentException());
duke@435 675 }
duke@435 676 oop srcp = JNIHandles::resolve(srcObj);
duke@435 677 oop dstp = JNIHandles::resolve(dstObj);
duke@435 678 if (dstp != NULL && !dstp->is_typeArray()) {
duke@435 679 // NYI: This works only for non-oop arrays at present.
duke@435 680 // Generalizing it would be reasonable, but requires card marking.
duke@435 681 // Also, autoboxing a Long from 0L in copyMemory(x,y, 0L,z, n) would be bad.
duke@435 682 THROW(vmSymbols::java_lang_IllegalArgumentException());
duke@435 683 }
duke@435 684 void* src = index_oop_from_field_offset_long(srcp, srcOffset);
duke@435 685 void* dst = index_oop_from_field_offset_long(dstp, dstOffset);
duke@435 686 Copy::conjoint_memory_atomic(src, dst, sz);
duke@435 687 UNSAFE_END
duke@435 688
duke@435 689
duke@435 690 ////// Random queries
duke@435 691
duke@435 692 // See comment at file start about UNSAFE_LEAF
duke@435 693 //UNSAFE_LEAF(jint, Unsafe_AddressSize())
duke@435 694 UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe))
duke@435 695 UnsafeWrapper("Unsafe_AddressSize");
duke@435 696 return sizeof(void*);
duke@435 697 UNSAFE_END
duke@435 698
duke@435 699 // See comment at file start about UNSAFE_LEAF
duke@435 700 //UNSAFE_LEAF(jint, Unsafe_PageSize())
duke@435 701 UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe))
duke@435 702 UnsafeWrapper("Unsafe_PageSize");
duke@435 703 return os::vm_page_size();
duke@435 704 UNSAFE_END
duke@435 705
duke@435 706 jint find_field_offset(jobject field, int must_be_static, TRAPS) {
duke@435 707 if (field == NULL) {
duke@435 708 THROW_0(vmSymbols::java_lang_NullPointerException());
duke@435 709 }
duke@435 710
duke@435 711 oop reflected = JNIHandles::resolve_non_null(field);
duke@435 712 oop mirror = java_lang_reflect_Field::clazz(reflected);
coleenp@4037 713 Klass* k = java_lang_Class::as_Klass(mirror);
duke@435 714 int slot = java_lang_reflect_Field::slot(reflected);
duke@435 715 int modifiers = java_lang_reflect_Field::modifiers(reflected);
duke@435 716
duke@435 717 if (must_be_static >= 0) {
duke@435 718 int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0);
duke@435 719 if (must_be_static != really_is_static) {
duke@435 720 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
duke@435 721 }
duke@435 722 }
duke@435 723
coleenp@4037 724 int offset = InstanceKlass::cast(k)->field_offset(slot);
duke@435 725 return field_offset_from_byte_offset(offset);
duke@435 726 }
duke@435 727
duke@435 728 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
duke@435 729 UnsafeWrapper("Unsafe_ObjectFieldOffset");
duke@435 730 return find_field_offset(field, 0, THREAD);
duke@435 731 UNSAFE_END
duke@435 732
duke@435 733 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
duke@435 734 UnsafeWrapper("Unsafe_StaticFieldOffset");
duke@435 735 return find_field_offset(field, 1, THREAD);
duke@435 736 UNSAFE_END
duke@435 737
duke@435 738 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field))
duke@435 739 UnsafeWrapper("Unsafe_StaticFieldBase");
duke@435 740 // Note: In this VM implementation, a field address is always a short
duke@435 741 // offset from the base of a a klass metaobject. Thus, the full dynamic
duke@435 742 // range of the return type is never used. However, some implementations
duke@435 743 // might put the static field inside an array shared by many classes,
duke@435 744 // or even at a fixed address, in which case the address could be quite
duke@435 745 // large. In that last case, this function would return NULL, since
duke@435 746 // the address would operate alone, without any base pointer.
duke@435 747
duke@435 748 if (field == NULL) THROW_0(vmSymbols::java_lang_NullPointerException());
duke@435 749
duke@435 750 oop reflected = JNIHandles::resolve_non_null(field);
duke@435 751 oop mirror = java_lang_reflect_Field::clazz(reflected);
duke@435 752 int modifiers = java_lang_reflect_Field::modifiers(reflected);
duke@435 753
duke@435 754 if ((modifiers & JVM_ACC_STATIC) == 0) {
duke@435 755 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
duke@435 756 }
duke@435 757
never@2658 758 return JNIHandles::make_local(env, mirror);
duke@435 759 UNSAFE_END
duke@435 760
duke@435 761 //@deprecated
duke@435 762 UNSAFE_ENTRY(jint, Unsafe_FieldOffset(JNIEnv *env, jobject unsafe, jobject field))
duke@435 763 UnsafeWrapper("Unsafe_FieldOffset");
duke@435 764 // tries (but fails) to be polymorphic between static and non-static:
duke@435 765 jlong offset = find_field_offset(field, -1, THREAD);
duke@435 766 guarantee(offset == (jint)offset, "offset fits in 32 bits");
duke@435 767 return (jint)offset;
duke@435 768 UNSAFE_END
duke@435 769
duke@435 770 //@deprecated
duke@435 771 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromClass(JNIEnv *env, jobject unsafe, jobject clazz))
duke@435 772 UnsafeWrapper("Unsafe_StaticFieldBase");
duke@435 773 if (clazz == NULL) {
duke@435 774 THROW_0(vmSymbols::java_lang_NullPointerException());
duke@435 775 }
never@2658 776 return JNIHandles::make_local(env, JNIHandles::resolve_non_null(clazz));
duke@435 777 UNSAFE_END
duke@435 778
twisti@3969 779 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
duke@435 780 UnsafeWrapper("Unsafe_EnsureClassInitialized");
duke@435 781 if (clazz == NULL) {
duke@435 782 THROW(vmSymbols::java_lang_NullPointerException());
duke@435 783 }
duke@435 784 oop mirror = JNIHandles::resolve_non_null(clazz);
twisti@3969 785
coleenp@4037 786 Klass* klass = java_lang_Class::as_Klass(mirror);
hseigel@4278 787 if (klass != NULL && klass->should_be_initialized()) {
coleenp@4037 788 InstanceKlass* k = InstanceKlass::cast(klass);
duke@435 789 k->initialize(CHECK);
duke@435 790 }
twisti@3969 791 }
twisti@3969 792 UNSAFE_END
twisti@3969 793
twisti@3969 794 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized(JNIEnv *env, jobject unsafe, jobject clazz)) {
twisti@3969 795 UnsafeWrapper("Unsafe_ShouldBeInitialized");
twisti@3969 796 if (clazz == NULL) {
twisti@3969 797 THROW_(vmSymbols::java_lang_NullPointerException(), false);
twisti@3969 798 }
twisti@3969 799 oop mirror = JNIHandles::resolve_non_null(clazz);
coleenp@4037 800 Klass* klass = java_lang_Class::as_Klass(mirror);
hseigel@4278 801 if (klass != NULL && klass->should_be_initialized()) {
twisti@3969 802 return true;
twisti@3969 803 }
twisti@3969 804 return false;
twisti@3969 805 }
duke@435 806 UNSAFE_END
duke@435 807
duke@435 808 static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) {
duke@435 809 if (acls == NULL) {
duke@435 810 THROW(vmSymbols::java_lang_NullPointerException());
duke@435 811 }
duke@435 812 oop mirror = JNIHandles::resolve_non_null(acls);
coleenp@4037 813 Klass* k = java_lang_Class::as_Klass(mirror);
coleenp@4037 814 if (k == NULL || !k->oop_is_array()) {
duke@435 815 THROW(vmSymbols::java_lang_InvalidClassException());
coleenp@4037 816 } else if (k->oop_is_objArray()) {
duke@435 817 base = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
coleenp@548 818 scale = heapOopSize;
coleenp@4037 819 } else if (k->oop_is_typeArray()) {
coleenp@4142 820 TypeArrayKlass* tak = TypeArrayKlass::cast(k);
duke@435 821 base = tak->array_header_in_bytes();
duke@435 822 assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
duke@435 823 scale = (1 << tak->log2_element_size());
duke@435 824 } else {
duke@435 825 ShouldNotReachHere();
duke@435 826 }
duke@435 827 }
duke@435 828
duke@435 829 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls))
duke@435 830 UnsafeWrapper("Unsafe_ArrayBaseOffset");
duke@435 831 int base, scale;
duke@435 832 getBaseAndScale(base, scale, acls, CHECK_0);
duke@435 833 return field_offset_from_byte_offset(base);
duke@435 834 UNSAFE_END
duke@435 835
duke@435 836
duke@435 837 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls))
duke@435 838 UnsafeWrapper("Unsafe_ArrayIndexScale");
duke@435 839 int base, scale;
duke@435 840 getBaseAndScale(base, scale, acls, CHECK_0);
duke@435 841 // This VM packs both fields and array elements down to the byte.
duke@435 842 // But watch out: If this changes, so that array references for
duke@435 843 // a given primitive type (say, T_BOOLEAN) use different memory units
duke@435 844 // than fields, this method MUST return zero for such arrays.
duke@435 845 // For example, the VM used to store sub-word sized fields in full
duke@435 846 // words in the object layout, so that accessors like getByte(Object,int)
duke@435 847 // did not really do what one might expect for arrays. Therefore,
duke@435 848 // this function used to report a zero scale factor, so that the user
duke@435 849 // would know not to attempt to access sub-word array elements.
duke@435 850 // // Code for unpacked fields:
duke@435 851 // if (scale < wordSize) return 0;
duke@435 852
duke@435 853 // The following allows for a pretty general fieldOffset cookie scheme,
duke@435 854 // but requires it to be linear in byte offset.
duke@435 855 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
duke@435 856 UNSAFE_END
duke@435 857
duke@435 858
duke@435 859 static inline void throw_new(JNIEnv *env, const char *ename) {
duke@435 860 char buf[100];
duke@435 861 strcpy(buf, "java/lang/");
duke@435 862 strcat(buf, ename);
duke@435 863 jclass cls = env->FindClass(buf);
ccheung@6346 864 if (env->ExceptionCheck()) {
ccheung@6346 865 env->ExceptionClear();
ccheung@6346 866 tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", buf);
ccheung@6346 867 return;
ccheung@6346 868 }
duke@435 869 char* msg = NULL;
duke@435 870 env->ThrowNew(cls, msg);
duke@435 871 }
duke@435 872
twisti@4866 873 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
duke@435 874 {
duke@435 875 // Code lifted from JDK 1.3 ClassLoader.c
duke@435 876
duke@435 877 jbyte *body;
duke@435 878 char *utfName;
duke@435 879 jclass result = 0;
duke@435 880 char buf[128];
duke@435 881
duke@435 882 if (UsePerfData) {
duke@435 883 ClassLoader::unsafe_defineClassCallCounter()->inc();
duke@435 884 }
duke@435 885
duke@435 886 if (data == NULL) {
duke@435 887 throw_new(env, "NullPointerException");
duke@435 888 return 0;
duke@435 889 }
duke@435 890
duke@435 891 /* Work around 4153825. malloc crashes on Solaris when passed a
duke@435 892 * negative size.
duke@435 893 */
duke@435 894 if (length < 0) {
duke@435 895 throw_new(env, "ArrayIndexOutOfBoundsException");
duke@435 896 return 0;
duke@435 897 }
duke@435 898
zgu@3900 899 body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal);
duke@435 900
duke@435 901 if (body == 0) {
duke@435 902 throw_new(env, "OutOfMemoryError");
duke@435 903 return 0;
duke@435 904 }
duke@435 905
duke@435 906 env->GetByteArrayRegion(data, offset, length, body);
duke@435 907
duke@435 908 if (env->ExceptionOccurred())
duke@435 909 goto free_body;
duke@435 910
duke@435 911 if (name != NULL) {
duke@435 912 uint len = env->GetStringUTFLength(name);
duke@435 913 int unicode_len = env->GetStringLength(name);
duke@435 914 if (len >= sizeof(buf)) {
zgu@3900 915 utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
duke@435 916 if (utfName == NULL) {
duke@435 917 throw_new(env, "OutOfMemoryError");
duke@435 918 goto free_body;
duke@435 919 }
duke@435 920 } else {
duke@435 921 utfName = buf;
duke@435 922 }
duke@435 923 env->GetStringUTFRegion(name, 0, unicode_len, utfName);
duke@435 924 //VerifyFixClassname(utfName);
duke@435 925 for (uint i = 0; i < len; i++) {
duke@435 926 if (utfName[i] == '.') utfName[i] = '/';
duke@435 927 }
duke@435 928 } else {
duke@435 929 utfName = NULL;
duke@435 930 }
duke@435 931
duke@435 932 result = JVM_DefineClass(env, utfName, loader, body, length, pd);
duke@435 933
duke@435 934 if (utfName && utfName != buf)
zgu@3900 935 FREE_C_HEAP_ARRAY(char, utfName, mtInternal);
duke@435 936
duke@435 937 free_body:
zgu@3900 938 FREE_C_HEAP_ARRAY(jbyte, body, mtInternal);
duke@435 939 return result;
duke@435 940 }
duke@435 941 }
duke@435 942
duke@435 943
twisti@4866 944 UNSAFE_ENTRY(jclass, Unsafe_DefineClass(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
twisti@4866 945 UnsafeWrapper("Unsafe_DefineClass");
twisti@4866 946 {
twisti@4866 947 ThreadToNativeFromVM ttnfv(thread);
twisti@4866 948 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
twisti@4866 949 }
twisti@4866 950 UNSAFE_END
twisti@4866 951
twisti@4866 952
duke@435 953 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length))
duke@435 954 UnsafeWrapper("Unsafe_DefineClass");
duke@435 955 {
duke@435 956 ThreadToNativeFromVM ttnfv(thread);
duke@435 957
duke@435 958 int depthFromDefineClass0 = 1;
duke@435 959 jclass caller = JVM_GetCallerClass(env, depthFromDefineClass0);
duke@435 960 jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller);
duke@435 961 jobject pd = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller);
duke@435 962
twisti@4866 963 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd);
duke@435 964 }
duke@435 965 UNSAFE_END
duke@435 966
duke@435 967
jrose@866 968 #define DAC_Args CLS"[B["OBJ
jrose@866 969 // define a class but do not make it known to the class loader or system dictionary
jrose@866 970 // - host_class: supplies context for linkage, access control, protection domain, and class loader
jrose@866 971 // - data: bytes of a class file, a raw memory address (length gives the number of bytes)
jrose@866 972 // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data
jrose@866 973
jrose@866 974 // When you load an anonymous class U, it works as if you changed its name just before loading,
jrose@866 975 // to a name that you will never use again. Since the name is lost, no other class can directly
jrose@866 976 // link to any member of U. Just after U is loaded, the only way to use it is reflectively,
jrose@866 977 // through java.lang.Class methods like Class.newInstance.
jrose@866 978
jrose@866 979 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
jrose@866 980 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
jrose@866 981 // An anonymous class also has special privileges to access any member of its host class.
jrose@866 982 // This is the main reason why this loading operation is unsafe. The purpose of this is to
jrose@866 983 // allow language implementations to simulate "open classes"; a host class in effect gets
jrose@866 984 // new code when an anonymous class is loaded alongside it. A less convenient but more
jrose@866 985 // standard way to do this is with reflection, which can also be set to ignore access
jrose@866 986 // restrictions.
jrose@866 987
jrose@866 988 // Access into an anonymous class is possible only through reflection. Therefore, there
jrose@866 989 // are no special access rules for calling into an anonymous class. The relaxed access
jrose@866 990 // rule for the host class is applied in the opposite direction: A host class reflectively
jrose@866 991 // access one of its anonymous classes.
jrose@866 992
jrose@866 993 // If you load the same bytecodes twice, you get two different classes. You can reload
jrose@866 994 // the same bytecodes with or without varying CP patches.
jrose@866 995
jrose@866 996 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1.
jrose@866 997 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
jrose@866 998 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
jrose@866 999
jrose@866 1000 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
jrose@866 1001 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
jrose@866 1002 // It is not possible for a named class, or an older anonymous class, to refer by
jrose@866 1003 // name (via its CP) to a newer anonymous class.
jrose@866 1004
jrose@866 1005 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
jrose@866 1006 // or type descriptors used in the loaded anonymous class.
jrose@866 1007
jrose@866 1008 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
jrose@866 1009 // instead of "dead" strings. A compiled statement like println((Object)"hello") can
jrose@866 1010 // be changed to println(greeting), where greeting is an arbitrary object created before
jrose@866 1011 // the anonymous class is loaded. This is useful in dynamic languages, in which
jrose@866 1012 // various kinds of metaobjects must be introduced as constants into bytecode.
jrose@866 1013 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
jrose@866 1014 // not just a literal string. For such ldc instructions, the verifier uses the
jrose@866 1015 // type Object instead of String, if the loaded constant is not in fact a String.
jrose@866 1016
coleenp@4304 1017 static instanceKlassHandle
jrose@866 1018 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
jrose@866 1019 jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
jrose@866 1020 HeapWord* *temp_alloc,
jrose@866 1021 TRAPS) {
jrose@866 1022
jrose@866 1023 if (UsePerfData) {
jrose@866 1024 ClassLoader::unsafe_defineClassCallCounter()->inc();
jrose@866 1025 }
jrose@866 1026
jrose@866 1027 if (data == NULL) {
jrose@866 1028 THROW_0(vmSymbols::java_lang_NullPointerException());
jrose@866 1029 }
jrose@866 1030
jrose@866 1031 jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
jrose@866 1032 jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord);
zgu@3900 1033 HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length, mtInternal);
jrose@866 1034 if (body == NULL) {
jrose@866 1035 THROW_0(vmSymbols::java_lang_OutOfMemoryError());
jrose@866 1036 }
jrose@866 1037
jrose@866 1038 // caller responsible to free it:
jrose@866 1039 (*temp_alloc) = body;
jrose@866 1040
jrose@866 1041 {
jrose@866 1042 jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0);
jrose@866 1043 Copy::conjoint_words((HeapWord*) array_base, body, word_length);
jrose@866 1044 }
jrose@866 1045
jrose@866 1046 u1* class_bytes = (u1*) body;
jrose@866 1047 int class_bytes_length = (int) length;
jrose@866 1048 if (class_bytes_length < 0) class_bytes_length = 0;
jrose@866 1049 if (class_bytes == NULL
jrose@866 1050 || host_class == NULL
jrose@866 1051 || length != class_bytes_length)
jrose@866 1052 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
jrose@866 1053
jrose@866 1054 objArrayHandle cp_patches_h;
jrose@866 1055 if (cp_patches_jh != NULL) {
jrose@866 1056 oop p = JNIHandles::resolve_non_null(cp_patches_jh);
jrose@866 1057 if (!p->is_objArray())
jrose@866 1058 THROW_0(vmSymbols::java_lang_IllegalArgumentException());
jrose@866 1059 cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p);
jrose@866 1060 }
jrose@866 1061
coleenp@4037 1062 KlassHandle host_klass(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class)));
jrose@866 1063 const char* host_source = host_klass->external_name();
jrose@866 1064 Handle host_loader(THREAD, host_klass->class_loader());
jrose@866 1065 Handle host_domain(THREAD, host_klass->protection_domain());
jrose@866 1066
jrose@866 1067 GrowableArray<Handle>* cp_patches = NULL;
jrose@866 1068 if (cp_patches_h.not_null()) {
jrose@866 1069 int alen = cp_patches_h->length();
jrose@866 1070 for (int i = alen-1; i >= 0; i--) {
jrose@866 1071 oop p = cp_patches_h->obj_at(i);
jrose@866 1072 if (p != NULL) {
jrose@866 1073 Handle patch(THREAD, p);
jrose@866 1074 if (cp_patches == NULL)
jrose@866 1075 cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle());
jrose@866 1076 cp_patches->at_put(i, patch);
jrose@866 1077 }
jrose@866 1078 }
jrose@866 1079 }
jrose@866 1080
jrose@866 1081 ClassFileStream st(class_bytes, class_bytes_length, (char*) host_source);
jrose@866 1082
jrose@866 1083 instanceKlassHandle anon_klass;
jrose@866 1084 {
coleenp@2497 1085 Symbol* no_class_name = NULL;
coleenp@4037 1086 Klass* anonk = SystemDictionary::parse_stream(no_class_name,
jrose@866 1087 host_loader, host_domain,
jrose@866 1088 &st, host_klass, cp_patches,
jrose@866 1089 CHECK_NULL);
jrose@866 1090 if (anonk == NULL) return NULL;
jrose@866 1091 anon_klass = instanceKlassHandle(THREAD, anonk);
jrose@866 1092 }
jrose@866 1093
coleenp@4304 1094 return anon_klass;
jrose@866 1095 }
jrose@866 1096
jrose@866 1097 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh))
jrose@866 1098 {
coleenp@4304 1099 instanceKlassHandle anon_klass;
coleenp@4304 1100 jobject res_jh = NULL;
coleenp@4304 1101
jrose@866 1102 UnsafeWrapper("Unsafe_DefineAnonymousClass");
jrose@866 1103 ResourceMark rm(THREAD);
jrose@866 1104
jrose@866 1105 HeapWord* temp_alloc = NULL;
jrose@866 1106
coleenp@4304 1107 anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data,
coleenp@4304 1108 cp_patches_jh,
jrose@866 1109 &temp_alloc, THREAD);
coleenp@4304 1110 if (anon_klass() != NULL)
coleenp@4304 1111 res_jh = JNIHandles::make_local(env, anon_klass->java_mirror());
jrose@866 1112
jrose@866 1113 // try/finally clause:
jrose@866 1114 if (temp_alloc != NULL) {
zgu@3900 1115 FREE_C_HEAP_ARRAY(HeapWord, temp_alloc, mtInternal);
jrose@866 1116 }
jrose@866 1117
coleenp@4304 1118 // The anonymous class loader data has been artificially been kept alive to
coleenp@4304 1119 // this point. The mirror and any instances of this class have to keep
coleenp@4304 1120 // it alive afterwards.
coleenp@4304 1121 if (anon_klass() != NULL) {
coleenp@4304 1122 anon_klass->class_loader_data()->set_keep_alive(false);
coleenp@4304 1123 }
coleenp@4304 1124
coleenp@4304 1125 // let caller initialize it as needed...
coleenp@4304 1126
jrose@866 1127 return (jclass) res_jh;
jrose@866 1128 }
jrose@866 1129 UNSAFE_END
jrose@866 1130
jrose@866 1131
duke@435 1132
duke@435 1133 UNSAFE_ENTRY(void, Unsafe_MonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj))
duke@435 1134 UnsafeWrapper("Unsafe_MonitorEnter");
duke@435 1135 {
duke@435 1136 if (jobj == NULL) {
duke@435 1137 THROW(vmSymbols::java_lang_NullPointerException());
duke@435 1138 }
duke@435 1139 Handle obj(thread, JNIHandles::resolve_non_null(jobj));
duke@435 1140 ObjectSynchronizer::jni_enter(obj, CHECK);
duke@435 1141 }
duke@435 1142 UNSAFE_END
duke@435 1143
duke@435 1144
duke@435 1145 UNSAFE_ENTRY(jboolean, Unsafe_TryMonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj))
duke@435 1146 UnsafeWrapper("Unsafe_TryMonitorEnter");
duke@435 1147 {
duke@435 1148 if (jobj == NULL) {
duke@435 1149 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
duke@435 1150 }
duke@435 1151 Handle obj(thread, JNIHandles::resolve_non_null(jobj));
duke@435 1152 bool res = ObjectSynchronizer::jni_try_enter(obj, CHECK_0);
duke@435 1153 return (res ? JNI_TRUE : JNI_FALSE);
duke@435 1154 }
duke@435 1155 UNSAFE_END
duke@435 1156
duke@435 1157
duke@435 1158 UNSAFE_ENTRY(void, Unsafe_MonitorExit(JNIEnv *env, jobject unsafe, jobject jobj))
duke@435 1159 UnsafeWrapper("Unsafe_MonitorExit");
duke@435 1160 {
duke@435 1161 if (jobj == NULL) {
duke@435 1162 THROW(vmSymbols::java_lang_NullPointerException());
duke@435 1163 }
duke@435 1164 Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
duke@435 1165 ObjectSynchronizer::jni_exit(obj(), CHECK);
duke@435 1166 }
duke@435 1167 UNSAFE_END
duke@435 1168
duke@435 1169
duke@435 1170 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr))
duke@435 1171 UnsafeWrapper("Unsafe_ThrowException");
duke@435 1172 {
duke@435 1173 ThreadToNativeFromVM ttnfv(thread);
duke@435 1174 env->Throw(thr);
duke@435 1175 }
duke@435 1176 UNSAFE_END
duke@435 1177
duke@435 1178 // JSR166 ------------------------------------------------------------------
duke@435 1179
duke@435 1180 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h))
duke@435 1181 UnsafeWrapper("Unsafe_CompareAndSwapObject");
duke@435 1182 oop x = JNIHandles::resolve(x_h);
duke@435 1183 oop e = JNIHandles::resolve(e_h);
duke@435 1184 oop p = JNIHandles::resolve(obj);
coleenp@548 1185 HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset);
coleenp@4037 1186 oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true);
coleenp@548 1187 jboolean success = (res == e);
duke@435 1188 if (success)
coleenp@548 1189 update_barrier_set((void*)addr, x);
duke@435 1190 return success;
duke@435 1191 UNSAFE_END
duke@435 1192
duke@435 1193 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
duke@435 1194 UnsafeWrapper("Unsafe_CompareAndSwapInt");
duke@435 1195 oop p = JNIHandles::resolve(obj);
duke@435 1196 jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
duke@435 1197 return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
duke@435 1198 UNSAFE_END
duke@435 1199
duke@435 1200 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x))
duke@435 1201 UnsafeWrapper("Unsafe_CompareAndSwapLong");
duke@435 1202 Handle p (THREAD, JNIHandles::resolve(obj));
duke@435 1203 jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
duke@435 1204 if (VM_Version::supports_cx8())
duke@435 1205 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
duke@435 1206 else {
duke@435 1207 jboolean success = false;
duke@435 1208 ObjectLocker ol(p, THREAD);
duke@435 1209 if (*addr == e) { *addr = x; success = true; }
duke@435 1210 return success;
duke@435 1211 }
duke@435 1212 UNSAFE_END
duke@435 1213
duke@435 1214 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time))
duke@435 1215 UnsafeWrapper("Unsafe_Park");
sla@5237 1216 EventThreadPark event;
dcubed@3202 1217 #ifndef USDT2
fparain@1759 1218 HS_DTRACE_PROBE3(hotspot, thread__park__begin, thread->parker(), (int) isAbsolute, time);
dcubed@3202 1219 #else /* USDT2 */
dcubed@3202 1220 HOTSPOT_THREAD_PARK_BEGIN(
dcubed@3202 1221 (uintptr_t) thread->parker(), (int) isAbsolute, time);
dcubed@3202 1222 #endif /* USDT2 */
duke@435 1223 JavaThreadParkedState jtps(thread, time != 0);
duke@435 1224 thread->parker()->park(isAbsolute != 0, time);
dcubed@3202 1225 #ifndef USDT2
fparain@1759 1226 HS_DTRACE_PROBE1(hotspot, thread__park__end, thread->parker());
dcubed@3202 1227 #else /* USDT2 */
dcubed@3202 1228 HOTSPOT_THREAD_PARK_END(
dcubed@3202 1229 (uintptr_t) thread->parker());
dcubed@3202 1230 #endif /* USDT2 */
sla@5237 1231 if (event.should_commit()) {
sla@5237 1232 oop obj = thread->current_park_blocker();
hseigel@5784 1233 event.set_klass((obj != NULL) ? obj->klass() : NULL);
sla@5237 1234 event.set_timeout(time);
hseigel@5784 1235 event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0);
sla@5237 1236 event.commit();
sla@5237 1237 }
duke@435 1238 UNSAFE_END
duke@435 1239
duke@435 1240 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread))
duke@435 1241 UnsafeWrapper("Unsafe_Unpark");
duke@435 1242 Parker* p = NULL;
duke@435 1243 if (jthread != NULL) {
duke@435 1244 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@435 1245 if (java_thread != NULL) {
duke@435 1246 jlong lp = java_lang_Thread::park_event(java_thread);
duke@435 1247 if (lp != 0) {
duke@435 1248 // This cast is OK even though the jlong might have been read
duke@435 1249 // non-atomically on 32bit systems, since there, one word will
duke@435 1250 // always be zero anyway and the value set is always the same
duke@435 1251 p = (Parker*)addr_from_java(lp);
duke@435 1252 } else {
duke@435 1253 // Grab lock if apparently null or using older version of library
duke@435 1254 MutexLocker mu(Threads_lock);
duke@435 1255 java_thread = JNIHandles::resolve_non_null(jthread);
duke@435 1256 if (java_thread != NULL) {
duke@435 1257 JavaThread* thr = java_lang_Thread::thread(java_thread);
duke@435 1258 if (thr != NULL) {
duke@435 1259 p = thr->parker();
duke@435 1260 if (p != NULL) { // Bind to Java thread for next time.
duke@435 1261 java_lang_Thread::set_park_event(java_thread, addr_to_java(p));
duke@435 1262 }
duke@435 1263 }
duke@435 1264 }
duke@435 1265 }
duke@435 1266 }
duke@435 1267 }
duke@435 1268 if (p != NULL) {
dcubed@3202 1269 #ifndef USDT2
fparain@1759 1270 HS_DTRACE_PROBE1(hotspot, thread__unpark, p);
dcubed@3202 1271 #else /* USDT2 */
dcubed@3202 1272 HOTSPOT_THREAD_UNPARK(
dcubed@3202 1273 (uintptr_t) p);
dcubed@3202 1274 #endif /* USDT2 */
duke@435 1275 p->unpark();
duke@435 1276 }
duke@435 1277 UNSAFE_END
duke@435 1278
duke@435 1279 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem))
duke@435 1280 UnsafeWrapper("Unsafe_Loadavg");
duke@435 1281 const int max_nelem = 3;
duke@435 1282 double la[max_nelem];
duke@435 1283 jint ret;
duke@435 1284
duke@435 1285 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg));
duke@435 1286 assert(a->is_typeArray(), "must be type array");
duke@435 1287
duke@435 1288 if (nelem < 0 || nelem > max_nelem || a->length() < nelem) {
duke@435 1289 ThreadToNativeFromVM ttnfv(thread);
duke@435 1290 throw_new(env, "ArrayIndexOutOfBoundsException");
duke@435 1291 return -1;
duke@435 1292 }
duke@435 1293
duke@435 1294 ret = os::loadavg(la, nelem);
duke@435 1295 if (ret == -1) return -1;
duke@435 1296
duke@435 1297 // if successful, ret is the number of samples actually retrieved.
duke@435 1298 assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value");
duke@435 1299 switch(ret) {
duke@435 1300 case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
duke@435 1301 case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
duke@435 1302 case 1: a->double_at_put(0, (jdouble)la[0]); break;
duke@435 1303 }
duke@435 1304 return ret;
duke@435 1305 UNSAFE_END
duke@435 1306
duke@435 1307 UNSAFE_ENTRY(void, Unsafe_PrefetchRead(JNIEnv* env, jclass ignored, jobject obj, jlong offset))
duke@435 1308 UnsafeWrapper("Unsafe_PrefetchRead");
duke@435 1309 oop p = JNIHandles::resolve(obj);
duke@435 1310 void* addr = index_oop_from_field_offset_long(p, 0);
duke@435 1311 Prefetch::read(addr, (intx)offset);
duke@435 1312 UNSAFE_END
duke@435 1313
duke@435 1314 UNSAFE_ENTRY(void, Unsafe_PrefetchWrite(JNIEnv* env, jclass ignored, jobject obj, jlong offset))
duke@435 1315 UnsafeWrapper("Unsafe_PrefetchWrite");
duke@435 1316 oop p = JNIHandles::resolve(obj);
duke@435 1317 void* addr = index_oop_from_field_offset_long(p, 0);
duke@435 1318 Prefetch::write(addr, (intx)offset);
duke@435 1319 UNSAFE_END
duke@435 1320
duke@435 1321
duke@435 1322 /// JVM_RegisterUnsafeMethods
duke@435 1323
duke@435 1324 #define ADR "J"
duke@435 1325
duke@435 1326 #define LANG "Ljava/lang/"
duke@435 1327
duke@435 1328 #define OBJ LANG"Object;"
duke@435 1329 #define CLS LANG"Class;"
duke@435 1330 #define CTR LANG"reflect/Constructor;"
duke@435 1331 #define FLD LANG"reflect/Field;"
duke@435 1332 #define MTH LANG"reflect/Method;"
duke@435 1333 #define THR LANG"Throwable;"
duke@435 1334
duke@435 1335 #define DC0_Args LANG"String;[BII"
twisti@4866 1336 #define DC_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;"
duke@435 1337
duke@435 1338 #define CC (char*) /*cast a literal from (const char*)*/
duke@435 1339 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
duke@435 1340
duke@435 1341 // define deprecated accessors for compabitility with 1.4.0
duke@435 1342 #define DECLARE_GETSETOOP_140(Boolean, Z) \
duke@435 1343 {CC"get"#Boolean, CC"("OBJ"I)"#Z, FN_PTR(Unsafe_Get##Boolean##140)}, \
duke@435 1344 {CC"put"#Boolean, CC"("OBJ"I"#Z")V", FN_PTR(Unsafe_Set##Boolean##140)}
duke@435 1345
duke@435 1346 // Note: In 1.4.1, getObject and kin take both int and long offsets.
duke@435 1347 #define DECLARE_GETSETOOP_141(Boolean, Z) \
duke@435 1348 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
duke@435 1349 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}
duke@435 1350
duke@435 1351 // Note: In 1.5.0, there are volatile versions too
duke@435 1352 #define DECLARE_GETSETOOP(Boolean, Z) \
duke@435 1353 {CC"get"#Boolean, CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean)}, \
duke@435 1354 {CC"put"#Boolean, CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean)}, \
duke@435 1355 {CC"get"#Boolean"Volatile", CC"("OBJ"J)"#Z, FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
duke@435 1356 {CC"put"#Boolean"Volatile", CC"("OBJ"J"#Z")V", FN_PTR(Unsafe_Set##Boolean##Volatile)}
duke@435 1357
duke@435 1358
duke@435 1359 #define DECLARE_GETSETNATIVE(Byte, B) \
duke@435 1360 {CC"get"#Byte, CC"("ADR")"#B, FN_PTR(Unsafe_GetNative##Byte)}, \
duke@435 1361 {CC"put"#Byte, CC"("ADR#B")V", FN_PTR(Unsafe_SetNative##Byte)}
duke@435 1362
duke@435 1363
duke@435 1364
twisti@4866 1365 // These are the methods for 1.4.0
duke@435 1366 static JNINativeMethod methods_140[] = {
duke@435 1367 {CC"getObject", CC"("OBJ"I)"OBJ"", FN_PTR(Unsafe_GetObject140)},
duke@435 1368 {CC"putObject", CC"("OBJ"I"OBJ")V", FN_PTR(Unsafe_SetObject140)},
duke@435 1369
duke@435 1370 DECLARE_GETSETOOP_140(Boolean, Z),
duke@435 1371 DECLARE_GETSETOOP_140(Byte, B),
duke@435 1372 DECLARE_GETSETOOP_140(Short, S),
duke@435 1373 DECLARE_GETSETOOP_140(Char, C),
duke@435 1374 DECLARE_GETSETOOP_140(Int, I),
duke@435 1375 DECLARE_GETSETOOP_140(Long, J),
duke@435 1376 DECLARE_GETSETOOP_140(Float, F),
duke@435 1377 DECLARE_GETSETOOP_140(Double, D),
duke@435 1378
duke@435 1379 DECLARE_GETSETNATIVE(Byte, B),
duke@435 1380 DECLARE_GETSETNATIVE(Short, S),
duke@435 1381 DECLARE_GETSETNATIVE(Char, C),
duke@435 1382 DECLARE_GETSETNATIVE(Int, I),
duke@435 1383 DECLARE_GETSETNATIVE(Long, J),
duke@435 1384 DECLARE_GETSETNATIVE(Float, F),
duke@435 1385 DECLARE_GETSETNATIVE(Double, D),
duke@435 1386
duke@435 1387 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
duke@435 1388 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
duke@435 1389
duke@435 1390 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
duke@435 1391 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
duke@435 1392 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
duke@435 1393
twisti@4866 1394 {CC"fieldOffset", CC"("FLD")I", FN_PTR(Unsafe_FieldOffset)},
twisti@4866 1395 {CC"staticFieldBase", CC"("CLS")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromClass)},
duke@435 1396 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
duke@435 1397 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
duke@435 1398 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
duke@435 1399 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
duke@435 1400 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
duke@435 1401
duke@435 1402 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
twisti@4866 1403 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
duke@435 1404 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
duke@435 1405 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
duke@435 1406 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
duke@435 1407 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
duke@435 1408 };
duke@435 1409
twisti@4866 1410 // These are the methods prior to the JSR 166 changes in 1.5.0
duke@435 1411 static JNINativeMethod methods_141[] = {
duke@435 1412 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
duke@435 1413 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
duke@435 1414
duke@435 1415 DECLARE_GETSETOOP_141(Boolean, Z),
duke@435 1416 DECLARE_GETSETOOP_141(Byte, B),
duke@435 1417 DECLARE_GETSETOOP_141(Short, S),
duke@435 1418 DECLARE_GETSETOOP_141(Char, C),
duke@435 1419 DECLARE_GETSETOOP_141(Int, I),
duke@435 1420 DECLARE_GETSETOOP_141(Long, J),
duke@435 1421 DECLARE_GETSETOOP_141(Float, F),
duke@435 1422 DECLARE_GETSETOOP_141(Double, D),
duke@435 1423
duke@435 1424 DECLARE_GETSETNATIVE(Byte, B),
duke@435 1425 DECLARE_GETSETNATIVE(Short, S),
duke@435 1426 DECLARE_GETSETNATIVE(Char, C),
duke@435 1427 DECLARE_GETSETNATIVE(Int, I),
duke@435 1428 DECLARE_GETSETNATIVE(Long, J),
duke@435 1429 DECLARE_GETSETNATIVE(Float, F),
duke@435 1430 DECLARE_GETSETNATIVE(Double, D),
duke@435 1431
duke@435 1432 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
duke@435 1433 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
duke@435 1434
duke@435 1435 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
duke@435 1436 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
duke@435 1437 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
duke@435 1438
duke@435 1439 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
duke@435 1440 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
duke@435 1441 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
duke@435 1442 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
duke@435 1443 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
duke@435 1444 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
duke@435 1445 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
duke@435 1446 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
duke@435 1447
duke@435 1448 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
twisti@4866 1449 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
duke@435 1450 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
duke@435 1451 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
duke@435 1452 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
duke@435 1453 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)}
duke@435 1454
duke@435 1455 };
duke@435 1456
twisti@4866 1457 // These are the methods prior to the JSR 166 changes in 1.6.0
duke@435 1458 static JNINativeMethod methods_15[] = {
duke@435 1459 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
duke@435 1460 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
duke@435 1461 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
duke@435 1462 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
duke@435 1463
duke@435 1464
duke@435 1465 DECLARE_GETSETOOP(Boolean, Z),
duke@435 1466 DECLARE_GETSETOOP(Byte, B),
duke@435 1467 DECLARE_GETSETOOP(Short, S),
duke@435 1468 DECLARE_GETSETOOP(Char, C),
duke@435 1469 DECLARE_GETSETOOP(Int, I),
duke@435 1470 DECLARE_GETSETOOP(Long, J),
duke@435 1471 DECLARE_GETSETOOP(Float, F),
duke@435 1472 DECLARE_GETSETOOP(Double, D),
duke@435 1473
duke@435 1474 DECLARE_GETSETNATIVE(Byte, B),
duke@435 1475 DECLARE_GETSETNATIVE(Short, S),
duke@435 1476 DECLARE_GETSETNATIVE(Char, C),
duke@435 1477 DECLARE_GETSETNATIVE(Int, I),
duke@435 1478 DECLARE_GETSETNATIVE(Long, J),
duke@435 1479 DECLARE_GETSETNATIVE(Float, F),
duke@435 1480 DECLARE_GETSETNATIVE(Double, D),
duke@435 1481
duke@435 1482 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
duke@435 1483 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
duke@435 1484
duke@435 1485 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
duke@435 1486 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
duke@435 1487 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
duke@435 1488
duke@435 1489 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
duke@435 1490 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
duke@435 1491 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
duke@435 1492 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
duke@435 1493 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
duke@435 1494 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
duke@435 1495 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
duke@435 1496 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
duke@435 1497
duke@435 1498 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
twisti@4866 1499 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
duke@435 1500 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
duke@435 1501 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
duke@435 1502 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
duke@435 1503 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
duke@435 1504 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
duke@435 1505 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
duke@435 1506 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
duke@435 1507 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
duke@435 1508 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
duke@435 1509
duke@435 1510 };
duke@435 1511
twisti@4866 1512 // These are the methods for 1.6.0 and 1.7.0
twisti@4866 1513 static JNINativeMethod methods_16[] = {
duke@435 1514 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
duke@435 1515 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
duke@435 1516 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
duke@435 1517 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
duke@435 1518
duke@435 1519 DECLARE_GETSETOOP(Boolean, Z),
duke@435 1520 DECLARE_GETSETOOP(Byte, B),
duke@435 1521 DECLARE_GETSETOOP(Short, S),
duke@435 1522 DECLARE_GETSETOOP(Char, C),
duke@435 1523 DECLARE_GETSETOOP(Int, I),
duke@435 1524 DECLARE_GETSETOOP(Long, J),
duke@435 1525 DECLARE_GETSETOOP(Float, F),
duke@435 1526 DECLARE_GETSETOOP(Double, D),
duke@435 1527
duke@435 1528 DECLARE_GETSETNATIVE(Byte, B),
duke@435 1529 DECLARE_GETSETNATIVE(Short, S),
duke@435 1530 DECLARE_GETSETNATIVE(Char, C),
duke@435 1531 DECLARE_GETSETNATIVE(Int, I),
duke@435 1532 DECLARE_GETSETNATIVE(Long, J),
duke@435 1533 DECLARE_GETSETNATIVE(Float, F),
duke@435 1534 DECLARE_GETSETNATIVE(Double, D),
duke@435 1535
duke@435 1536 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
duke@435 1537 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
duke@435 1538
duke@435 1539 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
duke@435 1540 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
duke@435 1541 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
duke@435 1542
duke@435 1543 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
duke@435 1544 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
duke@435 1545 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
duke@435 1546 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
duke@435 1547 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
duke@435 1548 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
duke@435 1549 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
duke@435 1550 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
duke@435 1551
duke@435 1552 {CC"defineClass", CC"("DC0_Args")"CLS, FN_PTR(Unsafe_DefineClass0)},
twisti@4866 1553 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
duke@435 1554 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
duke@435 1555 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
duke@435 1556 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
duke@435 1557 {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)},
duke@435 1558 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
duke@435 1559 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
duke@435 1560 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
duke@435 1561 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
duke@435 1562 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)},
duke@435 1563 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)},
duke@435 1564 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
duke@435 1565 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
duke@435 1566 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
twisti@4866 1567 };
duke@435 1568
twisti@4866 1569 // These are the methods for 1.8.0
twisti@4866 1570 static JNINativeMethod methods_18[] = {
twisti@4866 1571 {CC"getObject", CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObject)},
twisti@4866 1572 {CC"putObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObject)},
twisti@4866 1573 {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"", FN_PTR(Unsafe_GetObjectVolatile)},
twisti@4866 1574 {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetObjectVolatile)},
duke@435 1575
twisti@4866 1576 DECLARE_GETSETOOP(Boolean, Z),
twisti@4866 1577 DECLARE_GETSETOOP(Byte, B),
twisti@4866 1578 DECLARE_GETSETOOP(Short, S),
twisti@4866 1579 DECLARE_GETSETOOP(Char, C),
twisti@4866 1580 DECLARE_GETSETOOP(Int, I),
twisti@4866 1581 DECLARE_GETSETOOP(Long, J),
twisti@4866 1582 DECLARE_GETSETOOP(Float, F),
twisti@4866 1583 DECLARE_GETSETOOP(Double, D),
duke@435 1584
twisti@4866 1585 DECLARE_GETSETNATIVE(Byte, B),
twisti@4866 1586 DECLARE_GETSETNATIVE(Short, S),
twisti@4866 1587 DECLARE_GETSETNATIVE(Char, C),
twisti@4866 1588 DECLARE_GETSETNATIVE(Int, I),
twisti@4866 1589 DECLARE_GETSETNATIVE(Long, J),
twisti@4866 1590 DECLARE_GETSETNATIVE(Float, F),
twisti@4866 1591 DECLARE_GETSETNATIVE(Double, D),
twisti@4866 1592
twisti@4866 1593 {CC"getAddress", CC"("ADR")"ADR, FN_PTR(Unsafe_GetNativeAddress)},
twisti@4866 1594 {CC"putAddress", CC"("ADR""ADR")V", FN_PTR(Unsafe_SetNativeAddress)},
twisti@4866 1595
twisti@4866 1596 {CC"allocateMemory", CC"(J)"ADR, FN_PTR(Unsafe_AllocateMemory)},
twisti@4866 1597 {CC"reallocateMemory", CC"("ADR"J)"ADR, FN_PTR(Unsafe_ReallocateMemory)},
twisti@4866 1598 {CC"freeMemory", CC"("ADR")V", FN_PTR(Unsafe_FreeMemory)},
twisti@4866 1599
twisti@4866 1600 {CC"objectFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_ObjectFieldOffset)},
twisti@4866 1601 {CC"staticFieldOffset", CC"("FLD")J", FN_PTR(Unsafe_StaticFieldOffset)},
twisti@4866 1602 {CC"staticFieldBase", CC"("FLD")"OBJ, FN_PTR(Unsafe_StaticFieldBaseFromField)},
twisti@4866 1603 {CC"ensureClassInitialized",CC"("CLS")V", FN_PTR(Unsafe_EnsureClassInitialized)},
twisti@4866 1604 {CC"arrayBaseOffset", CC"("CLS")I", FN_PTR(Unsafe_ArrayBaseOffset)},
twisti@4866 1605 {CC"arrayIndexScale", CC"("CLS")I", FN_PTR(Unsafe_ArrayIndexScale)},
twisti@4866 1606 {CC"addressSize", CC"()I", FN_PTR(Unsafe_AddressSize)},
twisti@4866 1607 {CC"pageSize", CC"()I", FN_PTR(Unsafe_PageSize)},
twisti@4866 1608
twisti@4866 1609 {CC"defineClass", CC"("DC_Args")"CLS, FN_PTR(Unsafe_DefineClass)},
twisti@4866 1610 {CC"allocateInstance", CC"("CLS")"OBJ, FN_PTR(Unsafe_AllocateInstance)},
twisti@4866 1611 {CC"monitorEnter", CC"("OBJ")V", FN_PTR(Unsafe_MonitorEnter)},
twisti@4866 1612 {CC"monitorExit", CC"("OBJ")V", FN_PTR(Unsafe_MonitorExit)},
twisti@4866 1613 {CC"tryMonitorEnter", CC"("OBJ")Z", FN_PTR(Unsafe_TryMonitorEnter)},
twisti@4866 1614 {CC"throwException", CC"("THR")V", FN_PTR(Unsafe_ThrowException)},
twisti@4866 1615 {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z", FN_PTR(Unsafe_CompareAndSwapObject)},
twisti@4866 1616 {CC"compareAndSwapInt", CC"("OBJ"J""I""I"")Z", FN_PTR(Unsafe_CompareAndSwapInt)},
twisti@4866 1617 {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z", FN_PTR(Unsafe_CompareAndSwapLong)},
twisti@4866 1618 {CC"putOrderedObject", CC"("OBJ"J"OBJ")V", FN_PTR(Unsafe_SetOrderedObject)},
twisti@4866 1619 {CC"putOrderedInt", CC"("OBJ"JI)V", FN_PTR(Unsafe_SetOrderedInt)},
twisti@4866 1620 {CC"putOrderedLong", CC"("OBJ"JJ)V", FN_PTR(Unsafe_SetOrderedLong)},
twisti@4866 1621 {CC"park", CC"(ZJ)V", FN_PTR(Unsafe_Park)},
twisti@4866 1622 {CC"unpark", CC"("OBJ")V", FN_PTR(Unsafe_Unpark)}
duke@435 1623 };
duke@435 1624
duke@435 1625 JNINativeMethod loadavg_method[] = {
twisti@4866 1626 {CC"getLoadAverage", CC"([DI)I", FN_PTR(Unsafe_Loadavg)}
duke@435 1627 };
duke@435 1628
duke@435 1629 JNINativeMethod prefetch_methods[] = {
duke@435 1630 {CC"prefetchRead", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
duke@435 1631 {CC"prefetchWrite", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)},
duke@435 1632 {CC"prefetchReadStatic", CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchRead)},
duke@435 1633 {CC"prefetchWriteStatic",CC"("OBJ"J)V", FN_PTR(Unsafe_PrefetchWrite)}
duke@435 1634 };
duke@435 1635
twisti@4866 1636 JNINativeMethod memcopy_methods_17[] = {
duke@435 1637 {CC"copyMemory", CC"("OBJ"J"OBJ"JJ)V", FN_PTR(Unsafe_CopyMemory2)},
duke@435 1638 {CC"setMemory", CC"("OBJ"JJB)V", FN_PTR(Unsafe_SetMemory2)}
duke@435 1639 };
duke@435 1640
duke@435 1641 JNINativeMethod memcopy_methods_15[] = {
duke@435 1642 {CC"setMemory", CC"("ADR"JB)V", FN_PTR(Unsafe_SetMemory)},
duke@435 1643 {CC"copyMemory", CC"("ADR ADR"J)V", FN_PTR(Unsafe_CopyMemory)}
duke@435 1644 };
duke@435 1645
jrose@866 1646 JNINativeMethod anonk_methods[] = {
jrose@866 1647 {CC"defineAnonymousClass", CC"("DAC_Args")"CLS, FN_PTR(Unsafe_DefineAnonymousClass)},
jrose@866 1648 };
duke@435 1649
twisti@3969 1650 JNINativeMethod lform_methods[] = {
twisti@3969 1651 {CC"shouldBeInitialized",CC"("CLS")Z", FN_PTR(Unsafe_ShouldBeInitialized)},
twisti@3969 1652 };
twisti@3969 1653
twisti@4866 1654 JNINativeMethod fence_methods[] = {
twisti@4866 1655 {CC"loadFence", CC"()V", FN_PTR(Unsafe_LoadFence)},
twisti@4866 1656 {CC"storeFence", CC"()V", FN_PTR(Unsafe_StoreFence)},
twisti@4866 1657 {CC"fullFence", CC"()V", FN_PTR(Unsafe_FullFence)},
twisti@4866 1658 };
twisti@4866 1659
duke@435 1660 #undef CC
duke@435 1661 #undef FN_PTR
duke@435 1662
duke@435 1663 #undef ADR
duke@435 1664 #undef LANG
duke@435 1665 #undef OBJ
duke@435 1666 #undef CLS
duke@435 1667 #undef CTR
duke@435 1668 #undef FLD
duke@435 1669 #undef MTH
duke@435 1670 #undef THR
duke@435 1671 #undef DC0_Args
twisti@4866 1672 #undef DC_Args
duke@435 1673
duke@435 1674 #undef DECLARE_GETSETOOP
duke@435 1675 #undef DECLARE_GETSETNATIVE
duke@435 1676
duke@435 1677
twisti@4866 1678 /**
twisti@4866 1679 * Helper method to register native methods.
twisti@4866 1680 */
twisti@4866 1681 static bool register_natives(const char* message, JNIEnv* env, jclass clazz, const JNINativeMethod* methods, jint nMethods) {
twisti@4866 1682 int status = env->RegisterNatives(clazz, methods, nMethods);
twisti@4866 1683 if (status < 0 || env->ExceptionOccurred()) {
twisti@4866 1684 if (PrintMiscellaneous && (Verbose || WizardMode)) {
twisti@4866 1685 tty->print_cr("Unsafe: failed registering %s", message);
twisti@4866 1686 }
twisti@4866 1687 env->ExceptionClear();
twisti@4866 1688 return false;
twisti@4866 1689 } else {
twisti@4866 1690 if (PrintMiscellaneous && (Verbose || WizardMode)) {
twisti@4866 1691 tty->print_cr("Unsafe: successfully registered %s", message);
twisti@4866 1692 }
twisti@4866 1693 return true;
twisti@4866 1694 }
twisti@4866 1695 }
twisti@4866 1696
twisti@4866 1697
duke@435 1698 // This one function is exported, used by NativeLookup.
duke@435 1699 // The Unsafe_xxx functions above are called only from the interpreter.
duke@435 1700 // The optimizer looks at names and signatures to recognize
duke@435 1701 // individual functions.
duke@435 1702
duke@435 1703 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls))
duke@435 1704 UnsafeWrapper("JVM_RegisterUnsafeMethods");
duke@435 1705 {
duke@435 1706 ThreadToNativeFromVM ttnfv(thread);
twisti@4866 1707
twisti@4866 1708 // Unsafe methods
duke@435 1709 {
twisti@4866 1710 bool success = false;
twisti@4866 1711 // We need to register the 1.6 methods first because the 1.8 methods would register fine on 1.7 and 1.6
twisti@4866 1712 if (!success) {
twisti@4866 1713 success = register_natives("1.6 methods", env, unsafecls, methods_16, sizeof(methods_16)/sizeof(JNINativeMethod));
twisti@4866 1714 }
twisti@4866 1715 if (!success) {
twisti@4866 1716 success = register_natives("1.8 methods", env, unsafecls, methods_18, sizeof(methods_18)/sizeof(JNINativeMethod));
twisti@4866 1717 }
twisti@4866 1718 if (!success) {
twisti@4866 1719 success = register_natives("1.5 methods", env, unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod));
twisti@4866 1720 }
twisti@4866 1721 if (!success) {
twisti@4866 1722 success = register_natives("1.4.1 methods", env, unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod));
twisti@4866 1723 }
twisti@4866 1724 if (!success) {
twisti@4866 1725 success = register_natives("1.4.0 methods", env, unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod));
twisti@4866 1726 }
twisti@4866 1727 guarantee(success, "register unsafe natives");
twisti@4866 1728 }
twisti@4866 1729
twisti@4866 1730 // Unsafe.getLoadAverage
twisti@4866 1731 register_natives("1.6 loadavg method", env, unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod));
twisti@4866 1732
twisti@4866 1733 // Prefetch methods
twisti@4866 1734 register_natives("1.6 prefetch methods", env, unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod));
twisti@4866 1735
twisti@4866 1736 // Memory copy methods
twisti@4866 1737 {
twisti@4866 1738 bool success = false;
twisti@4866 1739 if (!success) {
twisti@4866 1740 success = register_natives("1.7 memory copy methods", env, unsafecls, memcopy_methods_17, sizeof(memcopy_methods_17)/sizeof(JNINativeMethod));
twisti@4866 1741 }
twisti@4866 1742 if (!success) {
twisti@4866 1743 success = register_natives("1.5 memory copy methods", env, unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod));
duke@435 1744 }
duke@435 1745 }
twisti@4866 1746
twisti@4866 1747 // Unsafe.defineAnonymousClass
twisti@4866 1748 if (EnableInvokeDynamic) {
twisti@4866 1749 register_natives("1.7 define anonymous class method", env, unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod));
duke@435 1750 }
twisti@4866 1751
twisti@4866 1752 // Unsafe.shouldBeInitialized
twisti@4866 1753 if (EnableInvokeDynamic) {
twisti@4866 1754 register_natives("1.7 LambdaForm support", env, unsafecls, lform_methods, sizeof(lform_methods)/sizeof(JNINativeMethod));
duke@435 1755 }
twisti@4866 1756
twisti@4866 1757 // Fence methods
twisti@4866 1758 register_natives("1.8 fence methods", env, unsafecls, fence_methods, sizeof(fence_methods)/sizeof(JNINativeMethod));
duke@435 1759 }
duke@435 1760 JVM_END

mercurial