src/share/classes/sun/misc/Unsafe.java

Print this page




  65      * <p> Here is a suggested idiom for using unsafe operations:
  66      *
  67      * <blockquote><pre>
  68      * class MyTrustedClass {
  69      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  70      *   ...
  71      *   private long myCountAddress = ...;
  72      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  73      * }
  74      * </pre></blockquote>
  75      *
  76      * (It may assist compilers to make the local variable be
  77      * <code>final</code>.)
  78      *
  79      * @exception  SecurityException  if a security manager exists and its
  80      *             <code>checkPropertiesAccess</code> method doesn't allow
  81      *             access to the system properties.
  82      */
  83     public static Unsafe getUnsafe() {
  84         Class<?> cc = sun.reflect.Reflection.getCallerClass(2);
  85         if (cc.getClassLoader() != null)
  86             throw new SecurityException("Unsafe");
  87         return theUnsafe;
  88     }
  89 
  90     /// peek and poke operations
  91     /// (compilers should optimize these to memory ops)
  92 
  93     // These work on object fields in the Java heap.
  94     // They will not work on elements of packed arrays.
  95 
  96     /**
  97      * Fetches a value from a given Java variable.
  98      * More specifically, fetches a field or array element within the given
  99      * object <code>o</code> at the given offset, or (if <code>o</code> is
 100      * null) from the memory address whose numerical value is the given
 101      * offset.
 102      * <p>
 103      * The results are undefined unless one of the following cases is true:
 104      * <ul>
 105      * <li>The offset was obtained from {@link #objectFieldOffset} on




  65      * <p> Here is a suggested idiom for using unsafe operations:
  66      *
  67      * <blockquote><pre>
  68      * class MyTrustedClass {
  69      *   private static final Unsafe unsafe = Unsafe.getUnsafe();
  70      *   ...
  71      *   private long myCountAddress = ...;
  72      *   public int getCount() { return unsafe.getByte(myCountAddress); }
  73      * }
  74      * </pre></blockquote>
  75      *
  76      * (It may assist compilers to make the local variable be
  77      * <code>final</code>.)
  78      *
  79      * @exception  SecurityException  if a security manager exists and its
  80      *             <code>checkPropertiesAccess</code> method doesn't allow
  81      *             access to the system properties.
  82      */
  83     public static Unsafe getUnsafe() {
  84         Class<?> cc = sun.reflect.Reflection.getCallerClass(2);
  85         if (!VM.isSystemDomainLoader(cc.getClassLoader()))
  86             throw new SecurityException("Unsafe");
  87         return theUnsafe;
  88     }
  89 
  90     /// peek and poke operations
  91     /// (compilers should optimize these to memory ops)
  92 
  93     // These work on object fields in the Java heap.
  94     // They will not work on elements of packed arrays.
  95 
  96     /**
  97      * Fetches a value from a given Java variable.
  98      * More specifically, fetches a field or array element within the given
  99      * object <code>o</code> at the given offset, or (if <code>o</code> is
 100      * null) from the memory address whose numerical value is the given
 101      * offset.
 102      * <p>
 103      * The results are undefined unless one of the following cases is true:
 104      * <ul>
 105      * <li>The offset was obtained from {@link #objectFieldOffset} on