src/share/vm/prims/unsafe.cpp

Wed, 02 Feb 2011 14:38:01 -0500

author
kamg
date
Wed, 02 Feb 2011 14:38:01 -0500
changeset 2511
bf8517f4e4d0
parent 2497
3582bf76420e
child 2598
d411927672ed
permissions
-rw-r--r--

6766644: Redefinition of compiled method fails with assertion "Can not load classes with the Compiler thread"
Summary: Defer posting events from the compiler thread: use service thread
Reviewed-by: coleenp, dholmes, never, dcubed

     1 /*
     2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "classfile/vmSymbols.hpp"
    27 #include "memory/allocation.inline.hpp"
    28 #include "prims/jni.h"
    29 #include "prims/jvm.h"
    30 #include "runtime/globals.hpp"
    31 #include "runtime/interfaceSupport.hpp"
    32 #include "runtime/reflection.hpp"
    33 #include "runtime/reflectionCompat.hpp"
    34 #include "runtime/synchronizer.hpp"
    35 #include "services/threadService.hpp"
    36 #include "utilities/copy.hpp"
    37 #include "utilities/dtrace.hpp"
    39 /*
    40  *      Implementation of class sun.misc.Unsafe
    41  */
    43 HS_DTRACE_PROBE_DECL3(hotspot, thread__park__begin, uintptr_t, int, long long);
    44 HS_DTRACE_PROBE_DECL1(hotspot, thread__park__end, uintptr_t);
    45 HS_DTRACE_PROBE_DECL1(hotspot, thread__unpark, uintptr_t);
    47 #define MAX_OBJECT_SIZE \
    48   ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \
    49     + ((julong)max_jint * sizeof(double)) )
    52 #define UNSAFE_ENTRY(result_type, header) \
    53   JVM_ENTRY(result_type, header)
    55 // Can't use UNSAFE_LEAF because it has the signature of a straight
    56 // call into the runtime (just like JVM_LEAF, funny that) but it's
    57 // called like a Java Native and thus the wrapper built for it passes
    58 // arguments like a JNI call.  It expects those arguments to be popped
    59 // from the stack on Intel like all good JNI args are, and adjusts the
    60 // stack according.  Since the JVM_LEAF call expects no extra
    61 // arguments the stack isn't popped in the C code, is pushed by the
    62 // wrapper and we get sick.
    63 //#define UNSAFE_LEAF(result_type, header) \
    64 //  JVM_LEAF(result_type, header)
    66 #define UNSAFE_END JVM_END
    68 #define UnsafeWrapper(arg) /*nothing, for the present*/
    71 inline void* addr_from_java(jlong addr) {
    72   // This assert fails in a variety of ways on 32-bit systems.
    73   // It is impossible to predict whether native code that converts
    74   // pointers to longs will sign-extend or zero-extend the addresses.
    75   //assert(addr == (uintptr_t)addr, "must not be odd high bits");
    76   return (void*)(uintptr_t)addr;
    77 }
    79 inline jlong addr_to_java(void* p) {
    80   assert(p == (void*)(uintptr_t)p, "must not be odd high bits");
    81   return (uintptr_t)p;
    82 }
    85 // Note: The VM's obj_field and related accessors use byte-scaled
    86 // ("unscaled") offsets, just as the unsafe methods do.
    88 // However, the method Unsafe.fieldOffset explicitly declines to
    89 // guarantee this.  The field offset values manipulated by the Java user
    90 // through the Unsafe API are opaque cookies that just happen to be byte
    91 // offsets.  We represent this state of affairs by passing the cookies
    92 // through conversion functions when going between the VM and the Unsafe API.
    93 // The conversion functions just happen to be no-ops at present.
    95 inline jlong field_offset_to_byte_offset(jlong field_offset) {
    96   return field_offset;
    97 }
    99 inline jlong field_offset_from_byte_offset(jlong byte_offset) {
   100   return byte_offset;
   101 }
   103 inline jint invocation_key_from_method_slot(jint slot) {
   104   return slot;
   105 }
   107 inline jint invocation_key_to_method_slot(jint key) {
   108   return key;
   109 }
   111 inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) {
   112   jlong byte_offset = field_offset_to_byte_offset(field_offset);
   113 #ifdef ASSERT
   114   if (p != NULL) {
   115     assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset");
   116     if (byte_offset == (jint)byte_offset) {
   117       void* ptr_plus_disp = (address)p + byte_offset;
   118       assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp,
   119              "raw [ptr+disp] must be consistent with oop::field_base");
   120     }
   121   }
   122 #endif
   123   if (sizeof(char*) == sizeof(jint))    // (this constant folds!)
   124     return (address)p + (jint) byte_offset;
   125   else
   126     return (address)p +        byte_offset;
   127 }
   129 // Externally callable versions:
   130 // (Use these in compiler intrinsics which emulate unsafe primitives.)
   131 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) {
   132   return field_offset;
   133 }
   134 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) {
   135   return byte_offset;
   136 }
   137 jint Unsafe_invocation_key_from_method_slot(jint slot) {
   138   return invocation_key_from_method_slot(slot);
   139 }
   140 jint Unsafe_invocation_key_to_method_slot(jint key) {
   141   return invocation_key_to_method_slot(key);
   142 }
   145 ///// Data in the Java heap.
   147 #define GET_FIELD(obj, offset, type_name, v) \
   148   oop p = JNIHandles::resolve(obj); \
   149   type_name v = *(type_name*)index_oop_from_field_offset_long(p, offset)
   151 #define SET_FIELD(obj, offset, type_name, x) \
   152   oop p = JNIHandles::resolve(obj); \
   153   *(type_name*)index_oop_from_field_offset_long(p, offset) = x
   155 #define GET_FIELD_VOLATILE(obj, offset, type_name, v) \
   156   oop p = JNIHandles::resolve(obj); \
   157   volatile type_name v = OrderAccess::load_acquire((volatile type_name*)index_oop_from_field_offset_long(p, offset));
   159 #define SET_FIELD_VOLATILE(obj, offset, type_name, x) \
   160   oop p = JNIHandles::resolve(obj); \
   161   OrderAccess::release_store_fence((volatile type_name*)index_oop_from_field_offset_long(p, offset), x);
   163 // Macros for oops that check UseCompressedOops
   165 #define GET_OOP_FIELD(obj, offset, v) \
   166   oop p = JNIHandles::resolve(obj);   \
   167   oop v;                              \
   168   if (UseCompressedOops) {            \
   169     narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); \
   170     v = oopDesc::decode_heap_oop(n);                                \
   171   } else {                            \
   172     v = *(oop*)index_oop_from_field_offset_long(p, offset);                 \
   173   }
   175 #define GET_OOP_FIELD_VOLATILE(obj, offset, v) \
   176   oop p = JNIHandles::resolve(obj);   \
   177   volatile oop v;                     \
   178   if (UseCompressedOops) {            \
   179     volatile narrowOop n = *(volatile narrowOop*)index_oop_from_field_offset_long(p, offset); \
   180     v = oopDesc::decode_heap_oop(n);                               \
   181   } else {                            \
   182     v = *(volatile oop*)index_oop_from_field_offset_long(p, offset);       \
   183   } \
   184   OrderAccess::acquire();
   187 // Get/SetObject must be special-cased, since it works with handles.
   189 // The xxx140 variants for backward compatibility do not allow a full-width offset.
   190 UNSAFE_ENTRY(jobject, Unsafe_GetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset))
   191   UnsafeWrapper("Unsafe_GetObject");
   192   if (obj == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException());
   193   GET_OOP_FIELD(obj, offset, v)
   194   return JNIHandles::make_local(env, v);
   195 UNSAFE_END
   197 UNSAFE_ENTRY(void, Unsafe_SetObject140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jobject x_h))
   198   UnsafeWrapper("Unsafe_SetObject");
   199   if (obj == NULL)  THROW(vmSymbols::java_lang_NullPointerException());
   200   oop x = JNIHandles::resolve(x_h);
   201   //SET_FIELD(obj, offset, oop, x);
   202   oop p = JNIHandles::resolve(obj);
   203   if (UseCompressedOops) {
   204     if (x != NULL) {
   205       // If there is a heap base pointer, we are obliged to emit a store barrier.
   206       oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x);
   207     } else {
   208       narrowOop n = oopDesc::encode_heap_oop_not_null(x);
   209       *(narrowOop*)index_oop_from_field_offset_long(p, offset) = n;
   210     }
   211   } else {
   212     if (x != NULL) {
   213       // If there is a heap base pointer, we are obliged to emit a store barrier.
   214       oop_store((oop*)index_oop_from_field_offset_long(p, offset), x);
   215     } else {
   216       *(oop*)index_oop_from_field_offset_long(p, offset) = x;
   217     }
   218   }
   219 UNSAFE_END
   221 // The normal variants allow a null base pointer with an arbitrary address.
   222 // But if the base pointer is non-null, the offset should make some sense.
   223 // That is, it should be in the range [0, MAX_OBJECT_SIZE].
   224 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
   225   UnsafeWrapper("Unsafe_GetObject");
   226   GET_OOP_FIELD(obj, offset, v)
   227   return JNIHandles::make_local(env, v);
   228 UNSAFE_END
   230 UNSAFE_ENTRY(void, Unsafe_SetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
   231   UnsafeWrapper("Unsafe_SetObject");
   232   oop x = JNIHandles::resolve(x_h);
   233   oop p = JNIHandles::resolve(obj);
   234   if (UseCompressedOops) {
   235     oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x);
   236   } else {
   237     oop_store((oop*)index_oop_from_field_offset_long(p, offset), x);
   238   }
   239 UNSAFE_END
   241 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
   242   UnsafeWrapper("Unsafe_GetObjectVolatile");
   243   GET_OOP_FIELD_VOLATILE(obj, offset, v)
   244   return JNIHandles::make_local(env, v);
   245 UNSAFE_END
   247 UNSAFE_ENTRY(void, Unsafe_SetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
   248   UnsafeWrapper("Unsafe_SetObjectVolatile");
   249   oop x = JNIHandles::resolve(x_h);
   250   oop p = JNIHandles::resolve(obj);
   251   void* addr = index_oop_from_field_offset_long(p, offset);
   252   OrderAccess::release();
   253   if (UseCompressedOops) {
   254     oop_store((narrowOop*)addr, x);
   255   } else {
   256     oop_store((oop*)addr, x);
   257   }
   258   OrderAccess::fence();
   259 UNSAFE_END
   261 #if defined(SPARC) || defined(X86)
   262 // Sparc and X86 have atomic jlong (8 bytes) instructions
   264 #else
   265 // Keep old code for platforms which may not have atomic jlong (8 bytes) instructions
   267 // Volatile long versions must use locks if !VM_Version::supports_cx8().
   268 // support_cx8 is a surrogate for 'supports atomic long memory ops'.
   270 UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset))
   271   UnsafeWrapper("Unsafe_GetLongVolatile");
   272   {
   273     if (VM_Version::supports_cx8()) {
   274       GET_FIELD_VOLATILE(obj, offset, jlong, v);
   275       return v;
   276     }
   277     else {
   278       Handle p (THREAD, JNIHandles::resolve(obj));
   279       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
   280       ObjectLocker ol(p, THREAD);
   281       jlong value = *addr;
   282       return value;
   283     }
   284   }
   285 UNSAFE_END
   287 UNSAFE_ENTRY(void, Unsafe_SetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
   288   UnsafeWrapper("Unsafe_SetLongVolatile");
   289   {
   290     if (VM_Version::supports_cx8()) {
   291       SET_FIELD_VOLATILE(obj, offset, jlong, x);
   292     }
   293     else {
   294       Handle p (THREAD, JNIHandles::resolve(obj));
   295       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
   296       ObjectLocker ol(p, THREAD);
   297       *addr = x;
   298     }
   299   }
   300 UNSAFE_END
   302 #endif // not SPARC and not X86
   304 #define DEFINE_GETSETOOP(jboolean, Boolean) \
   305  \
   306 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset)) \
   307   UnsafeWrapper("Unsafe_Get"#Boolean); \
   308   if (obj == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException()); \
   309   GET_FIELD(obj, offset, jboolean, v); \
   310   return v; \
   311 UNSAFE_END \
   312  \
   313 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##140(JNIEnv *env, jobject unsafe, jobject obj, jint offset, jboolean x)) \
   314   UnsafeWrapper("Unsafe_Set"#Boolean); \
   315   if (obj == NULL)  THROW(vmSymbols::java_lang_NullPointerException()); \
   316   SET_FIELD(obj, offset, jboolean, x); \
   317 UNSAFE_END \
   318  \
   319 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
   320   UnsafeWrapper("Unsafe_Get"#Boolean); \
   321   GET_FIELD(obj, offset, jboolean, v); \
   322   return v; \
   323 UNSAFE_END \
   324  \
   325 UNSAFE_ENTRY(void, Unsafe_Set##Boolean(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
   326   UnsafeWrapper("Unsafe_Set"#Boolean); \
   327   SET_FIELD(obj, offset, jboolean, x); \
   328 UNSAFE_END \
   329  \
   330 // END DEFINE_GETSETOOP.
   332 DEFINE_GETSETOOP(jboolean, Boolean)
   333 DEFINE_GETSETOOP(jbyte, Byte)
   334 DEFINE_GETSETOOP(jshort, Short);
   335 DEFINE_GETSETOOP(jchar, Char);
   336 DEFINE_GETSETOOP(jint, Int);
   337 DEFINE_GETSETOOP(jlong, Long);
   338 DEFINE_GETSETOOP(jfloat, Float);
   339 DEFINE_GETSETOOP(jdouble, Double);
   341 #undef DEFINE_GETSETOOP
   343 #define DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) \
   344  \
   345 UNSAFE_ENTRY(jboolean, Unsafe_Get##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) \
   346   UnsafeWrapper("Unsafe_Get"#Boolean); \
   347   GET_FIELD_VOLATILE(obj, offset, jboolean, v); \
   348   return v; \
   349 UNSAFE_END \
   350  \
   351 UNSAFE_ENTRY(void, Unsafe_Set##Boolean##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jboolean x)) \
   352   UnsafeWrapper("Unsafe_Set"#Boolean); \
   353   SET_FIELD_VOLATILE(obj, offset, jboolean, x); \
   354 UNSAFE_END \
   355  \
   356 // END DEFINE_GETSETOOP_VOLATILE.
   358 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean)
   359 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte)
   360 DEFINE_GETSETOOP_VOLATILE(jshort, Short);
   361 DEFINE_GETSETOOP_VOLATILE(jchar, Char);
   362 DEFINE_GETSETOOP_VOLATILE(jint, Int);
   363 DEFINE_GETSETOOP_VOLATILE(jfloat, Float);
   364 DEFINE_GETSETOOP_VOLATILE(jdouble, Double);
   366 #if defined(SPARC) || defined(X86)
   367 // Sparc and X86 have atomic jlong (8 bytes) instructions
   368 DEFINE_GETSETOOP_VOLATILE(jlong, Long);
   369 #endif
   371 #undef DEFINE_GETSETOOP_VOLATILE
   373 // The non-intrinsified versions of setOrdered just use setVolatile
   375 UNSAFE_ENTRY(void, Unsafe_SetOrderedInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint x))
   376   UnsafeWrapper("Unsafe_SetOrderedInt");
   377   SET_FIELD_VOLATILE(obj, offset, jint, x);
   378 UNSAFE_END
   380 UNSAFE_ENTRY(void, Unsafe_SetOrderedObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h))
   381   UnsafeWrapper("Unsafe_SetOrderedObject");
   382   oop x = JNIHandles::resolve(x_h);
   383   oop p = JNIHandles::resolve(obj);
   384   void* addr = index_oop_from_field_offset_long(p, offset);
   385   OrderAccess::release();
   386   if (UseCompressedOops) {
   387     oop_store((narrowOop*)addr, x);
   388   } else {
   389     oop_store((oop*)addr, x);
   390   }
   391   OrderAccess::fence();
   392 UNSAFE_END
   394 UNSAFE_ENTRY(void, Unsafe_SetOrderedLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x))
   395   UnsafeWrapper("Unsafe_SetOrderedLong");
   396 #if defined(SPARC) || defined(X86)
   397   // Sparc and X86 have atomic jlong (8 bytes) instructions
   398   SET_FIELD_VOLATILE(obj, offset, jlong, x);
   399 #else
   400   // Keep old code for platforms which may not have atomic long (8 bytes) instructions
   401   {
   402     if (VM_Version::supports_cx8()) {
   403       SET_FIELD_VOLATILE(obj, offset, jlong, x);
   404     }
   405     else {
   406       Handle p (THREAD, JNIHandles::resolve(obj));
   407       jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
   408       ObjectLocker ol(p, THREAD);
   409       *addr = x;
   410     }
   411   }
   412 #endif
   413 UNSAFE_END
   415 ////// Data in the C heap.
   417 // Note:  These do not throw NullPointerException for bad pointers.
   418 // They just crash.  Only a oop base pointer can generate a NullPointerException.
   419 //
   420 #define DEFINE_GETSETNATIVE(java_type, Type, native_type) \
   421  \
   422 UNSAFE_ENTRY(java_type, Unsafe_GetNative##Type(JNIEnv *env, jobject unsafe, jlong addr)) \
   423   UnsafeWrapper("Unsafe_GetNative"#Type); \
   424   void* p = addr_from_java(addr); \
   425   JavaThread* t = JavaThread::current(); \
   426   t->set_doing_unsafe_access(true); \
   427   java_type x = *(volatile native_type*)p; \
   428   t->set_doing_unsafe_access(false); \
   429   return x; \
   430 UNSAFE_END \
   431  \
   432 UNSAFE_ENTRY(void, Unsafe_SetNative##Type(JNIEnv *env, jobject unsafe, jlong addr, java_type x)) \
   433   UnsafeWrapper("Unsafe_SetNative"#Type); \
   434   JavaThread* t = JavaThread::current(); \
   435   t->set_doing_unsafe_access(true); \
   436   void* p = addr_from_java(addr); \
   437   *(volatile native_type*)p = x; \
   438   t->set_doing_unsafe_access(false); \
   439 UNSAFE_END \
   440  \
   441 // END DEFINE_GETSETNATIVE.
   443 DEFINE_GETSETNATIVE(jbyte, Byte, signed char)
   444 DEFINE_GETSETNATIVE(jshort, Short, signed short);
   445 DEFINE_GETSETNATIVE(jchar, Char, unsigned short);
   446 DEFINE_GETSETNATIVE(jint, Int, jint);
   447 // no long -- handled specially
   448 DEFINE_GETSETNATIVE(jfloat, Float, float);
   449 DEFINE_GETSETNATIVE(jdouble, Double, double);
   451 #undef DEFINE_GETSETNATIVE
   453 UNSAFE_ENTRY(jlong, Unsafe_GetNativeLong(JNIEnv *env, jobject unsafe, jlong addr))
   454   UnsafeWrapper("Unsafe_GetNativeLong");
   455   JavaThread* t = JavaThread::current();
   456   // We do it this way to avoid problems with access to heap using 64
   457   // bit loads, as jlong in heap could be not 64-bit aligned, and on
   458   // some CPUs (SPARC) it leads to SIGBUS.
   459   t->set_doing_unsafe_access(true);
   460   void* p = addr_from_java(addr);
   461   jlong x;
   462   if (((intptr_t)p & 7) == 0) {
   463     // jlong is aligned, do a volatile access
   464     x = *(volatile jlong*)p;
   465   } else {
   466     jlong_accessor acc;
   467     acc.words[0] = ((volatile jint*)p)[0];
   468     acc.words[1] = ((volatile jint*)p)[1];
   469     x = acc.long_value;
   470   }
   471   t->set_doing_unsafe_access(false);
   472   return x;
   473 UNSAFE_END
   475 UNSAFE_ENTRY(void, Unsafe_SetNativeLong(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
   476   UnsafeWrapper("Unsafe_SetNativeLong");
   477   JavaThread* t = JavaThread::current();
   478   // see comment for Unsafe_GetNativeLong
   479   t->set_doing_unsafe_access(true);
   480   void* p = addr_from_java(addr);
   481   if (((intptr_t)p & 7) == 0) {
   482     // jlong is aligned, do a volatile access
   483     *(volatile jlong*)p = x;
   484   } else {
   485     jlong_accessor acc;
   486     acc.long_value = x;
   487     ((volatile jint*)p)[0] = acc.words[0];
   488     ((volatile jint*)p)[1] = acc.words[1];
   489   }
   490   t->set_doing_unsafe_access(false);
   491 UNSAFE_END
   494 UNSAFE_ENTRY(jlong, Unsafe_GetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr))
   495   UnsafeWrapper("Unsafe_GetNativeAddress");
   496   void* p = addr_from_java(addr);
   497   return addr_to_java(*(void**)p);
   498 UNSAFE_END
   500 UNSAFE_ENTRY(void, Unsafe_SetNativeAddress(JNIEnv *env, jobject unsafe, jlong addr, jlong x))
   501   UnsafeWrapper("Unsafe_SetNativeAddress");
   502   void* p = addr_from_java(addr);
   503   *(void**)p = addr_from_java(x);
   504 UNSAFE_END
   507 ////// Allocation requests
   509 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls))
   510   UnsafeWrapper("Unsafe_AllocateInstance");
   511   {
   512     ThreadToNativeFromVM ttnfv(thread);
   513     return env->AllocObject(cls);
   514   }
   515 UNSAFE_END
   517 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory(JNIEnv *env, jobject unsafe, jlong size))
   518   UnsafeWrapper("Unsafe_AllocateMemory");
   519   size_t sz = (size_t)size;
   520   if (sz != (julong)size || size < 0) {
   521     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   522   }
   523   if (sz == 0) {
   524     return 0;
   525   }
   526   sz = round_to(sz, HeapWordSize);
   527   void* x = os::malloc(sz);
   528   if (x == NULL) {
   529     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
   530   }
   531   //Copy::fill_to_words((HeapWord*)x, sz / HeapWordSize);
   532   return addr_to_java(x);
   533 UNSAFE_END
   535 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size))
   536   UnsafeWrapper("Unsafe_ReallocateMemory");
   537   void* p = addr_from_java(addr);
   538   size_t sz = (size_t)size;
   539   if (sz != (julong)size || size < 0) {
   540     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   541   }
   542   if (sz == 0) {
   543     os::free(p);
   544     return 0;
   545   }
   546   sz = round_to(sz, HeapWordSize);
   547   void* x = (p == NULL) ? os::malloc(sz) : os::realloc(p, sz);
   548   if (x == NULL) {
   549     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
   550   }
   551   return addr_to_java(x);
   552 UNSAFE_END
   554 UNSAFE_ENTRY(void, Unsafe_FreeMemory(JNIEnv *env, jobject unsafe, jlong addr))
   555   UnsafeWrapper("Unsafe_FreeMemory");
   556   void* p = addr_from_java(addr);
   557   if (p == NULL) {
   558     return;
   559   }
   560   os::free(p);
   561 UNSAFE_END
   563 UNSAFE_ENTRY(void, Unsafe_SetMemory(JNIEnv *env, jobject unsafe, jlong addr, jlong size, jbyte value))
   564   UnsafeWrapper("Unsafe_SetMemory");
   565   size_t sz = (size_t)size;
   566   if (sz != (julong)size || size < 0) {
   567     THROW(vmSymbols::java_lang_IllegalArgumentException());
   568   }
   569   char* p = (char*) addr_from_java(addr);
   570   Copy::fill_to_memory_atomic(p, sz, value);
   571 UNSAFE_END
   573 UNSAFE_ENTRY(void, Unsafe_SetMemory2(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value))
   574   UnsafeWrapper("Unsafe_SetMemory");
   575   size_t sz = (size_t)size;
   576   if (sz != (julong)size || size < 0) {
   577     THROW(vmSymbols::java_lang_IllegalArgumentException());
   578   }
   579   oop base = JNIHandles::resolve(obj);
   580   void* p = index_oop_from_field_offset_long(base, offset);
   581   Copy::fill_to_memory_atomic(p, sz, value);
   582 UNSAFE_END
   584 UNSAFE_ENTRY(void, Unsafe_CopyMemory(JNIEnv *env, jobject unsafe, jlong srcAddr, jlong dstAddr, jlong size))
   585   UnsafeWrapper("Unsafe_CopyMemory");
   586   if (size == 0) {
   587     return;
   588   }
   589   size_t sz = (size_t)size;
   590   if (sz != (julong)size || size < 0) {
   591     THROW(vmSymbols::java_lang_IllegalArgumentException());
   592   }
   593   void* src = addr_from_java(srcAddr);
   594   void* dst = addr_from_java(dstAddr);
   595   Copy::conjoint_memory_atomic(src, dst, sz);
   596 UNSAFE_END
   598 UNSAFE_ENTRY(void, Unsafe_CopyMemory2(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size))
   599   UnsafeWrapper("Unsafe_CopyMemory");
   600   if (size == 0) {
   601     return;
   602   }
   603   size_t sz = (size_t)size;
   604   if (sz != (julong)size || size < 0) {
   605     THROW(vmSymbols::java_lang_IllegalArgumentException());
   606   }
   607   oop srcp = JNIHandles::resolve(srcObj);
   608   oop dstp = JNIHandles::resolve(dstObj);
   609   if (dstp != NULL && !dstp->is_typeArray()) {
   610     // NYI:  This works only for non-oop arrays at present.
   611     // Generalizing it would be reasonable, but requires card marking.
   612     // Also, autoboxing a Long from 0L in copyMemory(x,y, 0L,z, n) would be bad.
   613     THROW(vmSymbols::java_lang_IllegalArgumentException());
   614   }
   615   void* src = index_oop_from_field_offset_long(srcp, srcOffset);
   616   void* dst = index_oop_from_field_offset_long(dstp, dstOffset);
   617   Copy::conjoint_memory_atomic(src, dst, sz);
   618 UNSAFE_END
   621 ////// Random queries
   623 // See comment at file start about UNSAFE_LEAF
   624 //UNSAFE_LEAF(jint, Unsafe_AddressSize())
   625 UNSAFE_ENTRY(jint, Unsafe_AddressSize(JNIEnv *env, jobject unsafe))
   626   UnsafeWrapper("Unsafe_AddressSize");
   627   return sizeof(void*);
   628 UNSAFE_END
   630 // See comment at file start about UNSAFE_LEAF
   631 //UNSAFE_LEAF(jint, Unsafe_PageSize())
   632 UNSAFE_ENTRY(jint, Unsafe_PageSize(JNIEnv *env, jobject unsafe))
   633   UnsafeWrapper("Unsafe_PageSize");
   634   return os::vm_page_size();
   635 UNSAFE_END
   637 jint find_field_offset(jobject field, int must_be_static, TRAPS) {
   638   if (field == NULL) {
   639     THROW_0(vmSymbols::java_lang_NullPointerException());
   640   }
   642   oop reflected   = JNIHandles::resolve_non_null(field);
   643   oop mirror      = java_lang_reflect_Field::clazz(reflected);
   644   klassOop k      = java_lang_Class::as_klassOop(mirror);
   645   int slot        = java_lang_reflect_Field::slot(reflected);
   646   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
   648   if (must_be_static >= 0) {
   649     int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0);
   650     if (must_be_static != really_is_static) {
   651       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   652     }
   653   }
   655   int offset = instanceKlass::cast(k)->offset_from_fields(slot);
   656   return field_offset_from_byte_offset(offset);
   657 }
   659 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
   660   UnsafeWrapper("Unsafe_ObjectFieldOffset");
   661   return find_field_offset(field, 0, THREAD);
   662 UNSAFE_END
   664 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset(JNIEnv *env, jobject unsafe, jobject field))
   665   UnsafeWrapper("Unsafe_StaticFieldOffset");
   666   return find_field_offset(field, 1, THREAD);
   667 UNSAFE_END
   669 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromField(JNIEnv *env, jobject unsafe, jobject field))
   670   UnsafeWrapper("Unsafe_StaticFieldBase");
   671   // Note:  In this VM implementation, a field address is always a short
   672   // offset from the base of a a klass metaobject.  Thus, the full dynamic
   673   // range of the return type is never used.  However, some implementations
   674   // might put the static field inside an array shared by many classes,
   675   // or even at a fixed address, in which case the address could be quite
   676   // large.  In that last case, this function would return NULL, since
   677   // the address would operate alone, without any base pointer.
   679   if (field == NULL)  THROW_0(vmSymbols::java_lang_NullPointerException());
   681   oop reflected   = JNIHandles::resolve_non_null(field);
   682   oop mirror      = java_lang_reflect_Field::clazz(reflected);
   683   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
   685   if ((modifiers & JVM_ACC_STATIC) == 0) {
   686     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   687   }
   689   return JNIHandles::make_local(env, java_lang_Class::as_klassOop(mirror));
   690 UNSAFE_END
   692 //@deprecated
   693 UNSAFE_ENTRY(jint, Unsafe_FieldOffset(JNIEnv *env, jobject unsafe, jobject field))
   694   UnsafeWrapper("Unsafe_FieldOffset");
   695   // tries (but fails) to be polymorphic between static and non-static:
   696   jlong offset = find_field_offset(field, -1, THREAD);
   697   guarantee(offset == (jint)offset, "offset fits in 32 bits");
   698   return (jint)offset;
   699 UNSAFE_END
   701 //@deprecated
   702 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBaseFromClass(JNIEnv *env, jobject unsafe, jobject clazz))
   703   UnsafeWrapper("Unsafe_StaticFieldBase");
   704   if (clazz == NULL) {
   705     THROW_0(vmSymbols::java_lang_NullPointerException());
   706   }
   707   return JNIHandles::make_local(env, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz)));
   708 UNSAFE_END
   710 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized(JNIEnv *env, jobject unsafe, jobject clazz))
   711   UnsafeWrapper("Unsafe_EnsureClassInitialized");
   712   if (clazz == NULL) {
   713     THROW(vmSymbols::java_lang_NullPointerException());
   714   }
   715   oop mirror = JNIHandles::resolve_non_null(clazz);
   716   instanceKlass* k = instanceKlass::cast(java_lang_Class::as_klassOop(mirror));
   717   if (k != NULL) {
   718     k->initialize(CHECK);
   719   }
   720 UNSAFE_END
   722 static void getBaseAndScale(int& base, int& scale, jclass acls, TRAPS) {
   723   if (acls == NULL) {
   724     THROW(vmSymbols::java_lang_NullPointerException());
   725   }
   726   oop      mirror = JNIHandles::resolve_non_null(acls);
   727   klassOop k      = java_lang_Class::as_klassOop(mirror);
   728   if (k == NULL || !k->klass_part()->oop_is_array()) {
   729     THROW(vmSymbols::java_lang_InvalidClassException());
   730   } else if (k->klass_part()->oop_is_objArray()) {
   731     base  = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
   732     scale = heapOopSize;
   733   } else if (k->klass_part()->oop_is_typeArray()) {
   734     typeArrayKlass* tak = typeArrayKlass::cast(k);
   735     base  = tak->array_header_in_bytes();
   736     assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok");
   737     scale = (1 << tak->log2_element_size());
   738   } else {
   739     ShouldNotReachHere();
   740   }
   741 }
   743 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset(JNIEnv *env, jobject unsafe, jclass acls))
   744   UnsafeWrapper("Unsafe_ArrayBaseOffset");
   745   int base, scale;
   746   getBaseAndScale(base, scale, acls, CHECK_0);
   747   return field_offset_from_byte_offset(base);
   748 UNSAFE_END
   751 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale(JNIEnv *env, jobject unsafe, jclass acls))
   752   UnsafeWrapper("Unsafe_ArrayIndexScale");
   753   int base, scale;
   754   getBaseAndScale(base, scale, acls, CHECK_0);
   755   // This VM packs both fields and array elements down to the byte.
   756   // But watch out:  If this changes, so that array references for
   757   // a given primitive type (say, T_BOOLEAN) use different memory units
   758   // than fields, this method MUST return zero for such arrays.
   759   // For example, the VM used to store sub-word sized fields in full
   760   // words in the object layout, so that accessors like getByte(Object,int)
   761   // did not really do what one might expect for arrays.  Therefore,
   762   // this function used to report a zero scale factor, so that the user
   763   // would know not to attempt to access sub-word array elements.
   764   // // Code for unpacked fields:
   765   // if (scale < wordSize)  return 0;
   767   // The following allows for a pretty general fieldOffset cookie scheme,
   768   // but requires it to be linear in byte offset.
   769   return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0);
   770 UNSAFE_END
   773 static inline void throw_new(JNIEnv *env, const char *ename) {
   774   char buf[100];
   775   strcpy(buf, "java/lang/");
   776   strcat(buf, ename);
   777   jclass cls = env->FindClass(buf);
   778   char* msg = NULL;
   779   env->ThrowNew(cls, msg);
   780 }
   782 static jclass Unsafe_DefineClass(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) {
   783   {
   784     // Code lifted from JDK 1.3 ClassLoader.c
   786     jbyte *body;
   787     char *utfName;
   788     jclass result = 0;
   789     char buf[128];
   791     if (UsePerfData) {
   792       ClassLoader::unsafe_defineClassCallCounter()->inc();
   793     }
   795     if (data == NULL) {
   796         throw_new(env, "NullPointerException");
   797         return 0;
   798     }
   800     /* Work around 4153825. malloc crashes on Solaris when passed a
   801      * negative size.
   802      */
   803     if (length < 0) {
   804         throw_new(env, "ArrayIndexOutOfBoundsException");
   805         return 0;
   806     }
   808     body = NEW_C_HEAP_ARRAY(jbyte, length);
   810     if (body == 0) {
   811         throw_new(env, "OutOfMemoryError");
   812         return 0;
   813     }
   815     env->GetByteArrayRegion(data, offset, length, body);
   817     if (env->ExceptionOccurred())
   818         goto free_body;
   820     if (name != NULL) {
   821         uint len = env->GetStringUTFLength(name);
   822         int unicode_len = env->GetStringLength(name);
   823         if (len >= sizeof(buf)) {
   824             utfName = NEW_C_HEAP_ARRAY(char, len + 1);
   825             if (utfName == NULL) {
   826                 throw_new(env, "OutOfMemoryError");
   827                 goto free_body;
   828             }
   829         } else {
   830             utfName = buf;
   831         }
   832         env->GetStringUTFRegion(name, 0, unicode_len, utfName);
   833         //VerifyFixClassname(utfName);
   834         for (uint i = 0; i < len; i++) {
   835           if (utfName[i] == '.')   utfName[i] = '/';
   836         }
   837     } else {
   838         utfName = NULL;
   839     }
   841     result = JVM_DefineClass(env, utfName, loader, body, length, pd);
   843     if (utfName && utfName != buf)
   844         FREE_C_HEAP_ARRAY(char, utfName);
   846  free_body:
   847     FREE_C_HEAP_ARRAY(jbyte, body);
   848     return result;
   849   }
   850 }
   853 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length))
   854   UnsafeWrapper("Unsafe_DefineClass");
   855   {
   856     ThreadToNativeFromVM ttnfv(thread);
   858     int depthFromDefineClass0 = 1;
   859     jclass  caller = JVM_GetCallerClass(env, depthFromDefineClass0);
   860     jobject loader = (caller == NULL) ? NULL : JVM_GetClassLoader(env, caller);
   861     jobject pd     = (caller == NULL) ? NULL : JVM_GetProtectionDomain(env, caller);
   863     return Unsafe_DefineClass(env, name, data, offset, length, loader, pd);
   864   }
   865 UNSAFE_END
   868 UNSAFE_ENTRY(jclass, Unsafe_DefineClass1(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd))
   869   UnsafeWrapper("Unsafe_DefineClass");
   870   {
   871     ThreadToNativeFromVM ttnfv(thread);
   873     return Unsafe_DefineClass(env, name, data, offset, length, loader, pd);
   874   }
   875 UNSAFE_END
   877 #define DAC_Args CLS"[B["OBJ
   878 // define a class but do not make it known to the class loader or system dictionary
   879 // - host_class:  supplies context for linkage, access control, protection domain, and class loader
   880 // - data:  bytes of a class file, a raw memory address (length gives the number of bytes)
   881 // - cp_patches:  where non-null entries exist, they replace corresponding CP entries in data
   883 // When you load an anonymous class U, it works as if you changed its name just before loading,
   884 // to a name that you will never use again.  Since the name is lost, no other class can directly
   885 // link to any member of U.  Just after U is loaded, the only way to use it is reflectively,
   886 // through java.lang.Class methods like Class.newInstance.
   888 // Access checks for linkage sites within U continue to follow the same rules as for named classes.
   889 // The package of an anonymous class is given by the package qualifier on the name under which it was loaded.
   890 // An anonymous class also has special privileges to access any member of its host class.
   891 // This is the main reason why this loading operation is unsafe.  The purpose of this is to
   892 // allow language implementations to simulate "open classes"; a host class in effect gets
   893 // new code when an anonymous class is loaded alongside it.  A less convenient but more
   894 // standard way to do this is with reflection, which can also be set to ignore access
   895 // restrictions.
   897 // Access into an anonymous class is possible only through reflection.  Therefore, there
   898 // are no special access rules for calling into an anonymous class.  The relaxed access
   899 // rule for the host class is applied in the opposite direction:  A host class reflectively
   900 // access one of its anonymous classes.
   902 // If you load the same bytecodes twice, you get two different classes.  You can reload
   903 // the same bytecodes with or without varying CP patches.
   905 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1.
   906 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is).
   907 // The CONSTANT_Class entry for that name can be patched to refer directly to U1.
   909 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as
   910 // an outer class (so that U2 is an anonymous inner class of anonymous U1).
   911 // It is not possible for a named class, or an older anonymous class, to refer by
   912 // name (via its CP) to a newer anonymous class.
   914 // CP patching may also be used to modify (i.e., hack) the names of methods, classes,
   915 // or type descriptors used in the loaded anonymous class.
   917 // Finally, CP patching may be used to introduce "live" objects into the constant pool,
   918 // instead of "dead" strings.  A compiled statement like println((Object)"hello") can
   919 // be changed to println(greeting), where greeting is an arbitrary object created before
   920 // the anonymous class is loaded.  This is useful in dynamic languages, in which
   921 // various kinds of metaobjects must be introduced as constants into bytecode.
   922 // Note the cast (Object), which tells the verifier to expect an arbitrary object,
   923 // not just a literal string.  For such ldc instructions, the verifier uses the
   924 // type Object instead of String, if the loaded constant is not in fact a String.
   926 static oop
   927 Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
   928                                  jclass host_class, jbyteArray data, jobjectArray cp_patches_jh,
   929                                  HeapWord* *temp_alloc,
   930                                  TRAPS) {
   932   if (UsePerfData) {
   933     ClassLoader::unsafe_defineClassCallCounter()->inc();
   934   }
   936   if (data == NULL) {
   937     THROW_0(vmSymbols::java_lang_NullPointerException());
   938   }
   940   jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length();
   941   jint word_length = (length + sizeof(HeapWord)-1) / sizeof(HeapWord);
   942   HeapWord* body = NEW_C_HEAP_ARRAY(HeapWord, word_length);
   943   if (body == NULL) {
   944     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
   945   }
   947   // caller responsible to free it:
   948   (*temp_alloc) = body;
   950   {
   951     jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0);
   952     Copy::conjoint_words((HeapWord*) array_base, body, word_length);
   953   }
   955   u1* class_bytes = (u1*) body;
   956   int class_bytes_length = (int) length;
   957   if (class_bytes_length < 0)  class_bytes_length = 0;
   958   if (class_bytes == NULL
   959       || host_class == NULL
   960       || length != class_bytes_length)
   961     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   963   objArrayHandle cp_patches_h;
   964   if (cp_patches_jh != NULL) {
   965     oop p = JNIHandles::resolve_non_null(cp_patches_jh);
   966     if (!p->is_objArray())
   967       THROW_0(vmSymbols::java_lang_IllegalArgumentException());
   968     cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p);
   969   }
   971   KlassHandle host_klass(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(host_class)));
   972   const char* host_source = host_klass->external_name();
   973   Handle      host_loader(THREAD, host_klass->class_loader());
   974   Handle      host_domain(THREAD, host_klass->protection_domain());
   976   GrowableArray<Handle>* cp_patches = NULL;
   977   if (cp_patches_h.not_null()) {
   978     int alen = cp_patches_h->length();
   979     for (int i = alen-1; i >= 0; i--) {
   980       oop p = cp_patches_h->obj_at(i);
   981       if (p != NULL) {
   982         Handle patch(THREAD, p);
   983         if (cp_patches == NULL)
   984           cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle());
   985         cp_patches->at_put(i, patch);
   986       }
   987     }
   988   }
   990   ClassFileStream st(class_bytes, class_bytes_length, (char*) host_source);
   992   instanceKlassHandle anon_klass;
   993   {
   994     Symbol* no_class_name = NULL;
   995     klassOop anonk = SystemDictionary::parse_stream(no_class_name,
   996                                                     host_loader, host_domain,
   997                                                     &st, host_klass, cp_patches,
   998                                                     CHECK_NULL);
   999     if (anonk == NULL)  return NULL;
  1000     anon_klass = instanceKlassHandle(THREAD, anonk);
  1003   // let caller initialize it as needed...
  1005   return anon_klass->java_mirror();
  1008 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh))
  1010   UnsafeWrapper("Unsafe_DefineAnonymousClass");
  1011   ResourceMark rm(THREAD);
  1013   HeapWord* temp_alloc = NULL;
  1015   jobject res_jh = NULL;
  1017   { oop res_oop = Unsafe_DefineAnonymousClass_impl(env,
  1018                                                    host_class, data, cp_patches_jh,
  1019                                                    &temp_alloc, THREAD);
  1020     if (res_oop != NULL)
  1021       res_jh = JNIHandles::make_local(env, res_oop);
  1024   // try/finally clause:
  1025   if (temp_alloc != NULL) {
  1026     FREE_C_HEAP_ARRAY(HeapWord, temp_alloc);
  1029   return (jclass) res_jh;
  1031 UNSAFE_END
  1035 UNSAFE_ENTRY(void, Unsafe_MonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj))
  1036   UnsafeWrapper("Unsafe_MonitorEnter");
  1038     if (jobj == NULL) {
  1039       THROW(vmSymbols::java_lang_NullPointerException());
  1041     Handle obj(thread, JNIHandles::resolve_non_null(jobj));
  1042     ObjectSynchronizer::jni_enter(obj, CHECK);
  1044 UNSAFE_END
  1047 UNSAFE_ENTRY(jboolean, Unsafe_TryMonitorEnter(JNIEnv *env, jobject unsafe, jobject jobj))
  1048   UnsafeWrapper("Unsafe_TryMonitorEnter");
  1050     if (jobj == NULL) {
  1051       THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
  1053     Handle obj(thread, JNIHandles::resolve_non_null(jobj));
  1054     bool res = ObjectSynchronizer::jni_try_enter(obj, CHECK_0);
  1055     return (res ? JNI_TRUE : JNI_FALSE);
  1057 UNSAFE_END
  1060 UNSAFE_ENTRY(void, Unsafe_MonitorExit(JNIEnv *env, jobject unsafe, jobject jobj))
  1061   UnsafeWrapper("Unsafe_MonitorExit");
  1063     if (jobj == NULL) {
  1064       THROW(vmSymbols::java_lang_NullPointerException());
  1066     Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
  1067     ObjectSynchronizer::jni_exit(obj(), CHECK);
  1069 UNSAFE_END
  1072 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr))
  1073   UnsafeWrapper("Unsafe_ThrowException");
  1075     ThreadToNativeFromVM ttnfv(thread);
  1076     env->Throw(thr);
  1078 UNSAFE_END
  1080 // JSR166 ------------------------------------------------------------------
  1082 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h))
  1083   UnsafeWrapper("Unsafe_CompareAndSwapObject");
  1084   oop x = JNIHandles::resolve(x_h);
  1085   oop e = JNIHandles::resolve(e_h);
  1086   oop p = JNIHandles::resolve(obj);
  1087   HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset);
  1088   if (UseCompressedOops) {
  1089     update_barrier_set_pre((narrowOop*)addr, e);
  1090   } else {
  1091     update_barrier_set_pre((oop*)addr, e);
  1093   oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e);
  1094   jboolean success  = (res == e);
  1095   if (success)
  1096     update_barrier_set((void*)addr, x);
  1097   return success;
  1098 UNSAFE_END
  1100 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x))
  1101   UnsafeWrapper("Unsafe_CompareAndSwapInt");
  1102   oop p = JNIHandles::resolve(obj);
  1103   jint* addr = (jint *) index_oop_from_field_offset_long(p, offset);
  1104   return (jint)(Atomic::cmpxchg(x, addr, e)) == e;
  1105 UNSAFE_END
  1107 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x))
  1108   UnsafeWrapper("Unsafe_CompareAndSwapLong");
  1109   Handle p (THREAD, JNIHandles::resolve(obj));
  1110   jlong* addr = (jlong*)(index_oop_from_field_offset_long(p(), offset));
  1111   if (VM_Version::supports_cx8())
  1112     return (jlong)(Atomic::cmpxchg(x, addr, e)) == e;
  1113   else {
  1114     jboolean success = false;
  1115     ObjectLocker ol(p, THREAD);
  1116     if (*addr == e) { *addr = x; success = true; }
  1117     return success;
  1119 UNSAFE_END
  1121 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time))
  1122   UnsafeWrapper("Unsafe_Park");
  1123   HS_DTRACE_PROBE3(hotspot, thread__park__begin, thread->parker(), (int) isAbsolute, time);
  1124   JavaThreadParkedState jtps(thread, time != 0);
  1125   thread->parker()->park(isAbsolute != 0, time);
  1126   HS_DTRACE_PROBE1(hotspot, thread__park__end, thread->parker());
  1127 UNSAFE_END
  1129 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread))
  1130   UnsafeWrapper("Unsafe_Unpark");
  1131   Parker* p = NULL;
  1132   if (jthread != NULL) {
  1133     oop java_thread = JNIHandles::resolve_non_null(jthread);
  1134     if (java_thread != NULL) {
  1135       jlong lp = java_lang_Thread::park_event(java_thread);
  1136       if (lp != 0) {
  1137         // This cast is OK even though the jlong might have been read
  1138         // non-atomically on 32bit systems, since there, one word will
  1139         // always be zero anyway and the value set is always the same
  1140         p = (Parker*)addr_from_java(lp);
  1141       } else {
  1142         // Grab lock if apparently null or using older version of library
  1143         MutexLocker mu(Threads_lock);
  1144         java_thread = JNIHandles::resolve_non_null(jthread);
  1145         if (java_thread != NULL) {
  1146           JavaThread* thr = java_lang_Thread::thread(java_thread);
  1147           if (thr != NULL) {
  1148             p = thr->parker();
  1149             if (p != NULL) { // Bind to Java thread for next time.
  1150               java_lang_Thread::set_park_event(java_thread, addr_to_java(p));
  1157   if (p != NULL) {
  1158     HS_DTRACE_PROBE1(hotspot, thread__unpark, p);
  1159     p->unpark();
  1161 UNSAFE_END
  1163 UNSAFE_ENTRY(jint, Unsafe_Loadavg(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem))
  1164   UnsafeWrapper("Unsafe_Loadavg");
  1165   const int max_nelem = 3;
  1166   double la[max_nelem];
  1167   jint ret;
  1169   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg));
  1170   assert(a->is_typeArray(), "must be type array");
  1172   if (nelem < 0 || nelem > max_nelem || a->length() < nelem) {
  1173     ThreadToNativeFromVM ttnfv(thread);
  1174     throw_new(env, "ArrayIndexOutOfBoundsException");
  1175     return -1;
  1178   ret = os::loadavg(la, nelem);
  1179   if (ret == -1) return -1;
  1181   // if successful, ret is the number of samples actually retrieved.
  1182   assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value");
  1183   switch(ret) {
  1184     case 3: a->double_at_put(2, (jdouble)la[2]); // fall through
  1185     case 2: a->double_at_put(1, (jdouble)la[1]); // fall through
  1186     case 1: a->double_at_put(0, (jdouble)la[0]); break;
  1188   return ret;
  1189 UNSAFE_END
  1191 UNSAFE_ENTRY(void, Unsafe_PrefetchRead(JNIEnv* env, jclass ignored, jobject obj, jlong offset))
  1192   UnsafeWrapper("Unsafe_PrefetchRead");
  1193   oop p = JNIHandles::resolve(obj);
  1194   void* addr = index_oop_from_field_offset_long(p, 0);
  1195   Prefetch::read(addr, (intx)offset);
  1196 UNSAFE_END
  1198 UNSAFE_ENTRY(void, Unsafe_PrefetchWrite(JNIEnv* env, jclass ignored, jobject obj, jlong offset))
  1199   UnsafeWrapper("Unsafe_PrefetchWrite");
  1200   oop p = JNIHandles::resolve(obj);
  1201   void* addr = index_oop_from_field_offset_long(p, 0);
  1202   Prefetch::write(addr, (intx)offset);
  1203 UNSAFE_END
  1206 /// JVM_RegisterUnsafeMethods
  1208 #define ADR "J"
  1210 #define LANG "Ljava/lang/"
  1212 #define OBJ LANG"Object;"
  1213 #define CLS LANG"Class;"
  1214 #define CTR LANG"reflect/Constructor;"
  1215 #define FLD LANG"reflect/Field;"
  1216 #define MTH LANG"reflect/Method;"
  1217 #define THR LANG"Throwable;"
  1219 #define DC0_Args LANG"String;[BII"
  1220 #define DC1_Args DC0_Args LANG"ClassLoader;" "Ljava/security/ProtectionDomain;"
  1222 #define CC (char*)  /*cast a literal from (const char*)*/
  1223 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
  1225 // define deprecated accessors for compabitility with 1.4.0
  1226 #define DECLARE_GETSETOOP_140(Boolean, Z) \
  1227     {CC"get"#Boolean,      CC"("OBJ"I)"#Z,      FN_PTR(Unsafe_Get##Boolean##140)}, \
  1228     {CC"put"#Boolean,      CC"("OBJ"I"#Z")V",   FN_PTR(Unsafe_Set##Boolean##140)}
  1230 // Note:  In 1.4.1, getObject and kin take both int and long offsets.
  1231 #define DECLARE_GETSETOOP_141(Boolean, Z) \
  1232     {CC"get"#Boolean,      CC"("OBJ"J)"#Z,      FN_PTR(Unsafe_Get##Boolean)}, \
  1233     {CC"put"#Boolean,      CC"("OBJ"J"#Z")V",   FN_PTR(Unsafe_Set##Boolean)}
  1235 // Note:  In 1.5.0, there are volatile versions too
  1236 #define DECLARE_GETSETOOP(Boolean, Z) \
  1237     {CC"get"#Boolean,      CC"("OBJ"J)"#Z,      FN_PTR(Unsafe_Get##Boolean)}, \
  1238     {CC"put"#Boolean,      CC"("OBJ"J"#Z")V",   FN_PTR(Unsafe_Set##Boolean)}, \
  1239     {CC"get"#Boolean"Volatile",      CC"("OBJ"J)"#Z,      FN_PTR(Unsafe_Get##Boolean##Volatile)}, \
  1240     {CC"put"#Boolean"Volatile",      CC"("OBJ"J"#Z")V",   FN_PTR(Unsafe_Set##Boolean##Volatile)}
  1243 #define DECLARE_GETSETNATIVE(Byte, B) \
  1244     {CC"get"#Byte,         CC"("ADR")"#B,       FN_PTR(Unsafe_GetNative##Byte)}, \
  1245     {CC"put"#Byte,         CC"("ADR#B")V",      FN_PTR(Unsafe_SetNative##Byte)}
  1249 // %%% These are temporarily supported until the SDK sources
  1250 // contain the necessarily updated Unsafe.java.
  1251 static JNINativeMethod methods_140[] = {
  1253     {CC"getObject",        CC"("OBJ"I)"OBJ"",   FN_PTR(Unsafe_GetObject140)},
  1254     {CC"putObject",        CC"("OBJ"I"OBJ")V",  FN_PTR(Unsafe_SetObject140)},
  1256     DECLARE_GETSETOOP_140(Boolean, Z),
  1257     DECLARE_GETSETOOP_140(Byte, B),
  1258     DECLARE_GETSETOOP_140(Short, S),
  1259     DECLARE_GETSETOOP_140(Char, C),
  1260     DECLARE_GETSETOOP_140(Int, I),
  1261     DECLARE_GETSETOOP_140(Long, J),
  1262     DECLARE_GETSETOOP_140(Float, F),
  1263     DECLARE_GETSETOOP_140(Double, D),
  1265     DECLARE_GETSETNATIVE(Byte, B),
  1266     DECLARE_GETSETNATIVE(Short, S),
  1267     DECLARE_GETSETNATIVE(Char, C),
  1268     DECLARE_GETSETNATIVE(Int, I),
  1269     DECLARE_GETSETNATIVE(Long, J),
  1270     DECLARE_GETSETNATIVE(Float, F),
  1271     DECLARE_GETSETNATIVE(Double, D),
  1273     {CC"getAddress",         CC"("ADR")"ADR,             FN_PTR(Unsafe_GetNativeAddress)},
  1274     {CC"putAddress",         CC"("ADR""ADR")V",          FN_PTR(Unsafe_SetNativeAddress)},
  1276     {CC"allocateMemory",     CC"(J)"ADR,                 FN_PTR(Unsafe_AllocateMemory)},
  1277     {CC"reallocateMemory",   CC"("ADR"J)"ADR,            FN_PTR(Unsafe_ReallocateMemory)},
  1278 //  {CC"setMemory",          CC"("ADR"JB)V",             FN_PTR(Unsafe_SetMemory)},
  1279 //  {CC"copyMemory",         CC"("ADR ADR"J)V",          FN_PTR(Unsafe_CopyMemory)},
  1280     {CC"freeMemory",         CC"("ADR")V",               FN_PTR(Unsafe_FreeMemory)},
  1282     {CC"fieldOffset",        CC"("FLD")I",               FN_PTR(Unsafe_FieldOffset)}, //deprecated
  1283     {CC"staticFieldBase",    CC"("CLS")"OBJ,             FN_PTR(Unsafe_StaticFieldBaseFromClass)}, //deprecated
  1284     {CC"ensureClassInitialized",CC"("CLS")V",            FN_PTR(Unsafe_EnsureClassInitialized)},
  1285     {CC"arrayBaseOffset",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayBaseOffset)},
  1286     {CC"arrayIndexScale",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayIndexScale)},
  1287     {CC"addressSize",        CC"()I",                    FN_PTR(Unsafe_AddressSize)},
  1288     {CC"pageSize",           CC"()I",                    FN_PTR(Unsafe_PageSize)},
  1290     {CC"defineClass",        CC"("DC0_Args")"CLS,        FN_PTR(Unsafe_DefineClass0)},
  1291     {CC"defineClass",        CC"("DC1_Args")"CLS,        FN_PTR(Unsafe_DefineClass1)},
  1292     {CC"allocateInstance",   CC"("CLS")"OBJ,             FN_PTR(Unsafe_AllocateInstance)},
  1293     {CC"monitorEnter",       CC"("OBJ")V",               FN_PTR(Unsafe_MonitorEnter)},
  1294     {CC"monitorExit",        CC"("OBJ")V",               FN_PTR(Unsafe_MonitorExit)},
  1295     {CC"throwException",     CC"("THR")V",               FN_PTR(Unsafe_ThrowException)}
  1296 };
  1298 // These are the old methods prior to the JSR 166 changes in 1.5.0
  1299 static JNINativeMethod methods_141[] = {
  1301     {CC"getObject",        CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObject)},
  1302     {CC"putObject",        CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObject)},
  1304     DECLARE_GETSETOOP_141(Boolean, Z),
  1305     DECLARE_GETSETOOP_141(Byte, B),
  1306     DECLARE_GETSETOOP_141(Short, S),
  1307     DECLARE_GETSETOOP_141(Char, C),
  1308     DECLARE_GETSETOOP_141(Int, I),
  1309     DECLARE_GETSETOOP_141(Long, J),
  1310     DECLARE_GETSETOOP_141(Float, F),
  1311     DECLARE_GETSETOOP_141(Double, D),
  1313     DECLARE_GETSETNATIVE(Byte, B),
  1314     DECLARE_GETSETNATIVE(Short, S),
  1315     DECLARE_GETSETNATIVE(Char, C),
  1316     DECLARE_GETSETNATIVE(Int, I),
  1317     DECLARE_GETSETNATIVE(Long, J),
  1318     DECLARE_GETSETNATIVE(Float, F),
  1319     DECLARE_GETSETNATIVE(Double, D),
  1321     {CC"getAddress",         CC"("ADR")"ADR,             FN_PTR(Unsafe_GetNativeAddress)},
  1322     {CC"putAddress",         CC"("ADR""ADR")V",          FN_PTR(Unsafe_SetNativeAddress)},
  1324     {CC"allocateMemory",     CC"(J)"ADR,                 FN_PTR(Unsafe_AllocateMemory)},
  1325     {CC"reallocateMemory",   CC"("ADR"J)"ADR,            FN_PTR(Unsafe_ReallocateMemory)},
  1326 //  {CC"setMemory",          CC"("ADR"JB)V",             FN_PTR(Unsafe_SetMemory)},
  1327 //  {CC"copyMemory",         CC"("ADR ADR"J)V",          FN_PTR(Unsafe_CopyMemory)},
  1328     {CC"freeMemory",         CC"("ADR")V",               FN_PTR(Unsafe_FreeMemory)},
  1330     {CC"objectFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_ObjectFieldOffset)},
  1331     {CC"staticFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_StaticFieldOffset)},
  1332     {CC"staticFieldBase",    CC"("FLD")"OBJ,             FN_PTR(Unsafe_StaticFieldBaseFromField)},
  1333     {CC"ensureClassInitialized",CC"("CLS")V",            FN_PTR(Unsafe_EnsureClassInitialized)},
  1334     {CC"arrayBaseOffset",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayBaseOffset)},
  1335     {CC"arrayIndexScale",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayIndexScale)},
  1336     {CC"addressSize",        CC"()I",                    FN_PTR(Unsafe_AddressSize)},
  1337     {CC"pageSize",           CC"()I",                    FN_PTR(Unsafe_PageSize)},
  1339     {CC"defineClass",        CC"("DC0_Args")"CLS,        FN_PTR(Unsafe_DefineClass0)},
  1340     {CC"defineClass",        CC"("DC1_Args")"CLS,        FN_PTR(Unsafe_DefineClass1)},
  1341     {CC"allocateInstance",   CC"("CLS")"OBJ,             FN_PTR(Unsafe_AllocateInstance)},
  1342     {CC"monitorEnter",       CC"("OBJ")V",               FN_PTR(Unsafe_MonitorEnter)},
  1343     {CC"monitorExit",        CC"("OBJ")V",               FN_PTR(Unsafe_MonitorExit)},
  1344     {CC"throwException",     CC"("THR")V",               FN_PTR(Unsafe_ThrowException)}
  1346 };
  1348 // These are the old methods prior to the JSR 166 changes in 1.6.0
  1349 static JNINativeMethod methods_15[] = {
  1351     {CC"getObject",        CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObject)},
  1352     {CC"putObject",        CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObject)},
  1353     {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObjectVolatile)},
  1354     {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
  1357     DECLARE_GETSETOOP(Boolean, Z),
  1358     DECLARE_GETSETOOP(Byte, B),
  1359     DECLARE_GETSETOOP(Short, S),
  1360     DECLARE_GETSETOOP(Char, C),
  1361     DECLARE_GETSETOOP(Int, I),
  1362     DECLARE_GETSETOOP(Long, J),
  1363     DECLARE_GETSETOOP(Float, F),
  1364     DECLARE_GETSETOOP(Double, D),
  1366     DECLARE_GETSETNATIVE(Byte, B),
  1367     DECLARE_GETSETNATIVE(Short, S),
  1368     DECLARE_GETSETNATIVE(Char, C),
  1369     DECLARE_GETSETNATIVE(Int, I),
  1370     DECLARE_GETSETNATIVE(Long, J),
  1371     DECLARE_GETSETNATIVE(Float, F),
  1372     DECLARE_GETSETNATIVE(Double, D),
  1374     {CC"getAddress",         CC"("ADR")"ADR,             FN_PTR(Unsafe_GetNativeAddress)},
  1375     {CC"putAddress",         CC"("ADR""ADR")V",          FN_PTR(Unsafe_SetNativeAddress)},
  1377     {CC"allocateMemory",     CC"(J)"ADR,                 FN_PTR(Unsafe_AllocateMemory)},
  1378     {CC"reallocateMemory",   CC"("ADR"J)"ADR,            FN_PTR(Unsafe_ReallocateMemory)},
  1379 //  {CC"setMemory",          CC"("ADR"JB)V",             FN_PTR(Unsafe_SetMemory)},
  1380 //  {CC"copyMemory",         CC"("ADR ADR"J)V",          FN_PTR(Unsafe_CopyMemory)},
  1381     {CC"freeMemory",         CC"("ADR")V",               FN_PTR(Unsafe_FreeMemory)},
  1383     {CC"objectFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_ObjectFieldOffset)},
  1384     {CC"staticFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_StaticFieldOffset)},
  1385     {CC"staticFieldBase",    CC"("FLD")"OBJ,             FN_PTR(Unsafe_StaticFieldBaseFromField)},
  1386     {CC"ensureClassInitialized",CC"("CLS")V",            FN_PTR(Unsafe_EnsureClassInitialized)},
  1387     {CC"arrayBaseOffset",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayBaseOffset)},
  1388     {CC"arrayIndexScale",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayIndexScale)},
  1389     {CC"addressSize",        CC"()I",                    FN_PTR(Unsafe_AddressSize)},
  1390     {CC"pageSize",           CC"()I",                    FN_PTR(Unsafe_PageSize)},
  1392     {CC"defineClass",        CC"("DC0_Args")"CLS,        FN_PTR(Unsafe_DefineClass0)},
  1393     {CC"defineClass",        CC"("DC1_Args")"CLS,        FN_PTR(Unsafe_DefineClass1)},
  1394     {CC"allocateInstance",   CC"("CLS")"OBJ,             FN_PTR(Unsafe_AllocateInstance)},
  1395     {CC"monitorEnter",       CC"("OBJ")V",               FN_PTR(Unsafe_MonitorEnter)},
  1396     {CC"monitorExit",        CC"("OBJ")V",               FN_PTR(Unsafe_MonitorExit)},
  1397     {CC"throwException",     CC"("THR")V",               FN_PTR(Unsafe_ThrowException)},
  1398     {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z",  FN_PTR(Unsafe_CompareAndSwapObject)},
  1399     {CC"compareAndSwapInt",  CC"("OBJ"J""I""I"")Z",      FN_PTR(Unsafe_CompareAndSwapInt)},
  1400     {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z",      FN_PTR(Unsafe_CompareAndSwapLong)},
  1401     {CC"park",               CC"(ZJ)V",                  FN_PTR(Unsafe_Park)},
  1402     {CC"unpark",             CC"("OBJ")V",               FN_PTR(Unsafe_Unpark)}
  1404 };
  1406 // These are the correct methods, moving forward:
  1407 static JNINativeMethod methods[] = {
  1409     {CC"getObject",        CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObject)},
  1410     {CC"putObject",        CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObject)},
  1411     {CC"getObjectVolatile",CC"("OBJ"J)"OBJ"",   FN_PTR(Unsafe_GetObjectVolatile)},
  1412     {CC"putObjectVolatile",CC"("OBJ"J"OBJ")V",  FN_PTR(Unsafe_SetObjectVolatile)},
  1415     DECLARE_GETSETOOP(Boolean, Z),
  1416     DECLARE_GETSETOOP(Byte, B),
  1417     DECLARE_GETSETOOP(Short, S),
  1418     DECLARE_GETSETOOP(Char, C),
  1419     DECLARE_GETSETOOP(Int, I),
  1420     DECLARE_GETSETOOP(Long, J),
  1421     DECLARE_GETSETOOP(Float, F),
  1422     DECLARE_GETSETOOP(Double, D),
  1424     DECLARE_GETSETNATIVE(Byte, B),
  1425     DECLARE_GETSETNATIVE(Short, S),
  1426     DECLARE_GETSETNATIVE(Char, C),
  1427     DECLARE_GETSETNATIVE(Int, I),
  1428     DECLARE_GETSETNATIVE(Long, J),
  1429     DECLARE_GETSETNATIVE(Float, F),
  1430     DECLARE_GETSETNATIVE(Double, D),
  1432     {CC"getAddress",         CC"("ADR")"ADR,             FN_PTR(Unsafe_GetNativeAddress)},
  1433     {CC"putAddress",         CC"("ADR""ADR")V",          FN_PTR(Unsafe_SetNativeAddress)},
  1435     {CC"allocateMemory",     CC"(J)"ADR,                 FN_PTR(Unsafe_AllocateMemory)},
  1436     {CC"reallocateMemory",   CC"("ADR"J)"ADR,            FN_PTR(Unsafe_ReallocateMemory)},
  1437 //  {CC"setMemory",          CC"("ADR"JB)V",             FN_PTR(Unsafe_SetMemory)},
  1438 //  {CC"copyMemory",         CC"("ADR ADR"J)V",          FN_PTR(Unsafe_CopyMemory)},
  1439     {CC"freeMemory",         CC"("ADR")V",               FN_PTR(Unsafe_FreeMemory)},
  1441     {CC"objectFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_ObjectFieldOffset)},
  1442     {CC"staticFieldOffset",  CC"("FLD")J",               FN_PTR(Unsafe_StaticFieldOffset)},
  1443     {CC"staticFieldBase",    CC"("FLD")"OBJ,             FN_PTR(Unsafe_StaticFieldBaseFromField)},
  1444     {CC"ensureClassInitialized",CC"("CLS")V",            FN_PTR(Unsafe_EnsureClassInitialized)},
  1445     {CC"arrayBaseOffset",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayBaseOffset)},
  1446     {CC"arrayIndexScale",    CC"("CLS")I",               FN_PTR(Unsafe_ArrayIndexScale)},
  1447     {CC"addressSize",        CC"()I",                    FN_PTR(Unsafe_AddressSize)},
  1448     {CC"pageSize",           CC"()I",                    FN_PTR(Unsafe_PageSize)},
  1450     {CC"defineClass",        CC"("DC0_Args")"CLS,        FN_PTR(Unsafe_DefineClass0)},
  1451     {CC"defineClass",        CC"("DC1_Args")"CLS,        FN_PTR(Unsafe_DefineClass1)},
  1452     {CC"allocateInstance",   CC"("CLS")"OBJ,             FN_PTR(Unsafe_AllocateInstance)},
  1453     {CC"monitorEnter",       CC"("OBJ")V",               FN_PTR(Unsafe_MonitorEnter)},
  1454     {CC"monitorExit",        CC"("OBJ")V",               FN_PTR(Unsafe_MonitorExit)},
  1455     {CC"tryMonitorEnter",    CC"("OBJ")Z",               FN_PTR(Unsafe_TryMonitorEnter)},
  1456     {CC"throwException",     CC"("THR")V",               FN_PTR(Unsafe_ThrowException)},
  1457     {CC"compareAndSwapObject", CC"("OBJ"J"OBJ""OBJ")Z",  FN_PTR(Unsafe_CompareAndSwapObject)},
  1458     {CC"compareAndSwapInt",  CC"("OBJ"J""I""I"")Z",      FN_PTR(Unsafe_CompareAndSwapInt)},
  1459     {CC"compareAndSwapLong", CC"("OBJ"J""J""J"")Z",      FN_PTR(Unsafe_CompareAndSwapLong)},
  1460     {CC"putOrderedObject",   CC"("OBJ"J"OBJ")V",         FN_PTR(Unsafe_SetOrderedObject)},
  1461     {CC"putOrderedInt",      CC"("OBJ"JI)V",             FN_PTR(Unsafe_SetOrderedInt)},
  1462     {CC"putOrderedLong",     CC"("OBJ"JJ)V",             FN_PTR(Unsafe_SetOrderedLong)},
  1463     {CC"park",               CC"(ZJ)V",                  FN_PTR(Unsafe_Park)},
  1464     {CC"unpark",             CC"("OBJ")V",               FN_PTR(Unsafe_Unpark)}
  1466 //    {CC"getLoadAverage",     CC"([DI)I",                 FN_PTR(Unsafe_Loadavg)},
  1468 //    {CC"prefetchRead",       CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchRead)},
  1469 //    {CC"prefetchWrite",      CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchWrite)}
  1470 //    {CC"prefetchReadStatic", CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchRead)},
  1471 //    {CC"prefetchWriteStatic",CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchWrite)}
  1473 };
  1475 JNINativeMethod loadavg_method[] = {
  1476     {CC"getLoadAverage",            CC"([DI)I",                 FN_PTR(Unsafe_Loadavg)}
  1477 };
  1479 JNINativeMethod prefetch_methods[] = {
  1480     {CC"prefetchRead",       CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchRead)},
  1481     {CC"prefetchWrite",      CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchWrite)},
  1482     {CC"prefetchReadStatic", CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchRead)},
  1483     {CC"prefetchWriteStatic",CC"("OBJ"J)V",              FN_PTR(Unsafe_PrefetchWrite)}
  1484 };
  1486 JNINativeMethod memcopy_methods[] = {
  1487     {CC"copyMemory",         CC"("OBJ"J"OBJ"JJ)V",       FN_PTR(Unsafe_CopyMemory2)},
  1488     {CC"setMemory",          CC"("OBJ"JJB)V",            FN_PTR(Unsafe_SetMemory2)}
  1489 };
  1491 JNINativeMethod memcopy_methods_15[] = {
  1492     {CC"setMemory",          CC"("ADR"JB)V",             FN_PTR(Unsafe_SetMemory)},
  1493     {CC"copyMemory",         CC"("ADR ADR"J)V",          FN_PTR(Unsafe_CopyMemory)}
  1494 };
  1496 JNINativeMethod anonk_methods[] = {
  1497     {CC"defineAnonymousClass", CC"("DAC_Args")"CLS,      FN_PTR(Unsafe_DefineAnonymousClass)},
  1498 };
  1500 #undef CC
  1501 #undef FN_PTR
  1503 #undef ADR
  1504 #undef LANG
  1505 #undef OBJ
  1506 #undef CLS
  1507 #undef CTR
  1508 #undef FLD
  1509 #undef MTH
  1510 #undef THR
  1511 #undef DC0_Args
  1512 #undef DC1_Args
  1514 #undef DECLARE_GETSETOOP
  1515 #undef DECLARE_GETSETNATIVE
  1518 // This one function is exported, used by NativeLookup.
  1519 // The Unsafe_xxx functions above are called only from the interpreter.
  1520 // The optimizer looks at names and signatures to recognize
  1521 // individual functions.
  1523 JVM_ENTRY(void, JVM_RegisterUnsafeMethods(JNIEnv *env, jclass unsafecls))
  1524   UnsafeWrapper("JVM_RegisterUnsafeMethods");
  1526     ThreadToNativeFromVM ttnfv(thread);
  1528       env->RegisterNatives(unsafecls, loadavg_method, sizeof(loadavg_method)/sizeof(JNINativeMethod));
  1529       if (env->ExceptionOccurred()) {
  1530         if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1531           tty->print_cr("Warning:  SDK 1.6 Unsafe.loadavg not found.");
  1533         env->ExceptionClear();
  1537       env->RegisterNatives(unsafecls, prefetch_methods, sizeof(prefetch_methods)/sizeof(JNINativeMethod));
  1538       if (env->ExceptionOccurred()) {
  1539         if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1540           tty->print_cr("Warning:  SDK 1.6 Unsafe.prefetchRead/Write not found.");
  1542         env->ExceptionClear();
  1546       env->RegisterNatives(unsafecls, memcopy_methods, sizeof(memcopy_methods)/sizeof(JNINativeMethod));
  1547       if (env->ExceptionOccurred()) {
  1548         if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1549           tty->print_cr("Warning:  SDK 1.7 Unsafe.copyMemory not found.");
  1551         env->ExceptionClear();
  1552         env->RegisterNatives(unsafecls, memcopy_methods_15, sizeof(memcopy_methods_15)/sizeof(JNINativeMethod));
  1553         if (env->ExceptionOccurred()) {
  1554           if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1555             tty->print_cr("Warning:  SDK 1.5 Unsafe.copyMemory not found.");
  1557           env->ExceptionClear();
  1561     if (AnonymousClasses) {
  1562       env->RegisterNatives(unsafecls, anonk_methods, sizeof(anonk_methods)/sizeof(JNINativeMethod));
  1563       if (env->ExceptionOccurred()) {
  1564         if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1565           tty->print_cr("Warning:  SDK 1.7 Unsafe.defineClass (anonymous version) not found.");
  1567         env->ExceptionClear();
  1570     int status = env->RegisterNatives(unsafecls, methods, sizeof(methods)/sizeof(JNINativeMethod));
  1571     if (env->ExceptionOccurred()) {
  1572       if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1573         tty->print_cr("Warning:  SDK 1.6 version of Unsafe not found.");
  1575       env->ExceptionClear();
  1576       // %%% For now, be backward compatible with an older class:
  1577       status = env->RegisterNatives(unsafecls, methods_15, sizeof(methods_15)/sizeof(JNINativeMethod));
  1579     if (env->ExceptionOccurred()) {
  1580       if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1581         tty->print_cr("Warning:  SDK 1.5 version of Unsafe not found.");
  1583       env->ExceptionClear();
  1584       // %%% For now, be backward compatible with an older class:
  1585       status = env->RegisterNatives(unsafecls, methods_141, sizeof(methods_141)/sizeof(JNINativeMethod));
  1587     if (env->ExceptionOccurred()) {
  1588       if (PrintMiscellaneous && (Verbose || WizardMode)) {
  1589         tty->print_cr("Warning:  SDK 1.4.1 version of Unsafe not found.");
  1591       env->ExceptionClear();
  1592       // %%% For now, be backward compatible with an older class:
  1593       status = env->RegisterNatives(unsafecls, methods_140, sizeof(methods_140)/sizeof(JNINativeMethod));
  1595     guarantee(status == 0, "register unsafe natives");
  1597 JVM_END

mercurial