< prev index next >

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

Print this page

  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import jdk.internal.vm.annotation.ForceInline;
  29 import jdk.internal.misc.VM;
  30 import jdk.internal.reflect.CallerSensitive;
  31 import jdk.internal.reflect.Reflection;
  32 
  33 import java.lang.invoke.MethodHandles;
  34 import java.lang.reflect.Field;
  35 import java.util.Set;
  36 
  37 
  38 /**
  39  * A collection of methods for performing low-level, unsafe operations.
  40  * Although the class and all methods are public, use of this class is
  41  * limited because only trusted code can obtain instances of it.
  42  *
  43  * <em>Note:</em> It is the responsibility of the caller to make sure
  44  * arguments are checked before methods of this class are
  45  * called. While some rudimentary checks are performed on the input,
  46  * the checks are best effort and when performance is an overriding
  47  * priority, as when methods of this class are optimized by the
  48  * runtime compiler, some or all checks (if any) may be elided. Hence,
  49  * the caller must not rely on the checks and corresponding
  50  * exceptions!
  51  *
  52  * @author John R. Rose
  53  * @see #getUnsafe

 627      * two distinct fields of the same class will ever have the same offset
 628      * and base.
 629      *
 630      * <p>As of 1.4.1, offsets for fields are represented as long values,
 631      * although the Sun JVM does not use the most significant 32 bits.
 632      * However, JVM implementations which store static fields at absolute
 633      * addresses can use long offsets and null base pointers to express
 634      * the field locations in a form usable by {@link #getInt(Object,long)}.
 635      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 636      * must preserve all bits of static field offsets.
 637      *
 638      * @deprecated The guarantee that a field will always have the same offset
 639      * and base may not be true in a future release. The ability to provide an
 640      * offset and object reference to a heap memory accessor will be removed
 641      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 642      *
 643      * @see #getInt(Object, long)
 644      */
 645     @Deprecated(since="18")
 646     @ForceInline

 647     public long objectFieldOffset(Field f) {
 648         if (f == null) {
 649             throw new NullPointerException();
 650         }
 651         Class<?> declaringClass = f.getDeclaringClass();
 652         if (declaringClass.isHidden()) {
 653             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 654         }
 655         if (declaringClass.isRecord()) {
 656             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 657         }



 658         return theInternalUnsafe.objectFieldOffset(f);
 659     }
 660 
 661     /**
 662      * Reports the location of a given static field, in conjunction with {@link
 663      * #staticFieldBase}.
 664      * <p>Do not expect to perform any sort of arithmetic on this offset;
 665      * it is just a cookie which is passed to the unsafe heap memory accessors.
 666      *
 667      * <p>Any given field will always have the same offset, and no two distinct
 668      * fields of the same class will ever have the same offset.
 669      *
 670      * <p>As of 1.4.1, offsets for fields are represented as long values,
 671      * although the Sun JVM does not use the most significant 32 bits.
 672      * It is hard to imagine a JVM technology which needs more than
 673      * a few bits to encode an offset within a non-array object,
 674      * However, for consistency with other methods in this class,
 675      * this method reports its result as a long value.
 676      *
 677      * @deprecated The guarantee that a field will always have the same offset
 678      * and base may not be true in a future release. The ability to provide an
 679      * offset and object reference to a heap memory accessor will be removed
 680      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 681      *
 682      * @see #getInt(Object, long)
 683      */
 684     @Deprecated(since="18")
 685     @ForceInline

 686     public long staticFieldOffset(Field f) {
 687         if (f == null) {
 688             throw new NullPointerException();
 689         }
 690         Class<?> declaringClass = f.getDeclaringClass();
 691         if (declaringClass.isHidden()) {
 692             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 693         }
 694         if (declaringClass.isRecord()) {
 695             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 696         }



 697         return theInternalUnsafe.staticFieldOffset(f);
 698     }
 699 
 700     /**
 701      * Reports the location of a given static field, in conjunction with {@link
 702      * #staticFieldOffset}.
 703      * <p>Fetch the base "Object", if any, with which static fields of the
 704      * given class can be accessed via methods like {@link #getInt(Object,
 705      * long)}.  This value may be null.  This value may refer to an object
 706      * which is a "cookie", not guaranteed to be a real Object, and it should
 707      * not be used in any way except as argument to the get and put routines in
 708      * this class.
 709      *
 710      * @deprecated The guarantee that a field will always have the same offset
 711      * and base may not be true in a future release. The ability to provide an
 712      * offset and object reference to a heap memory accessor will be removed
 713      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 714      */
 715     @Deprecated(since="18")
 716     @ForceInline

 717     public Object staticFieldBase(Field f) {
 718         if (f == null) {
 719             throw new NullPointerException();
 720         }
 721         Class<?> declaringClass = f.getDeclaringClass();
 722         if (declaringClass.isHidden()) {
 723             throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
 724         }
 725         if (declaringClass.isRecord()) {
 726             throw new UnsupportedOperationException("can't get base address on a record class: " + f);
 727         }



 728         return theInternalUnsafe.staticFieldBase(f);
 729     }
 730 
 731     /**
 732      * Reports the offset of the first element in the storage allocation of a
 733      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 734      * for the same class, you may use that scale factor, together with this
 735      * base offset, to form new offsets to access elements of arrays of the
 736      * given class.
 737      *
 738      * @see #getInt(Object, long)
 739      * @see #putInt(Object, long, int)
 740      */
 741     @ForceInline
 742     public int arrayBaseOffset(Class<?> arrayClass) {
 743         return theInternalUnsafe.arrayBaseOffset(arrayClass);
 744     }
 745 
 746     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 747     public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;

  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.misc;
  27 
  28 import jdk.internal.vm.annotation.ForceInline;
  29 import jdk.internal.misc.VM;
  30 import jdk.internal.reflect.CallerSensitive;
  31 import jdk.internal.reflect.Reflection;
  32 

  33 import java.lang.reflect.Field;
  34 import java.util.Set;
  35 
  36 
  37 /**
  38  * A collection of methods for performing low-level, unsafe operations.
  39  * Although the class and all methods are public, use of this class is
  40  * limited because only trusted code can obtain instances of it.
  41  *
  42  * <em>Note:</em> It is the responsibility of the caller to make sure
  43  * arguments are checked before methods of this class are
  44  * called. While some rudimentary checks are performed on the input,
  45  * the checks are best effort and when performance is an overriding
  46  * priority, as when methods of this class are optimized by the
  47  * runtime compiler, some or all checks (if any) may be elided. Hence,
  48  * the caller must not rely on the checks and corresponding
  49  * exceptions!
  50  *
  51  * @author John R. Rose
  52  * @see #getUnsafe

 626      * two distinct fields of the same class will ever have the same offset
 627      * and base.
 628      *
 629      * <p>As of 1.4.1, offsets for fields are represented as long values,
 630      * although the Sun JVM does not use the most significant 32 bits.
 631      * However, JVM implementations which store static fields at absolute
 632      * addresses can use long offsets and null base pointers to express
 633      * the field locations in a form usable by {@link #getInt(Object,long)}.
 634      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 635      * must preserve all bits of static field offsets.
 636      *
 637      * @deprecated The guarantee that a field will always have the same offset
 638      * and base may not be true in a future release. The ability to provide an
 639      * offset and object reference to a heap memory accessor will be removed
 640      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 641      *
 642      * @see #getInt(Object, long)
 643      */
 644     @Deprecated(since="18")
 645     @ForceInline
 646     @SuppressWarnings("preview")
 647     public long objectFieldOffset(Field f) {
 648         if (f == null) {
 649             throw new NullPointerException();
 650         }
 651         Class<?> declaringClass = f.getDeclaringClass();
 652         if (declaringClass.isHidden()) {
 653             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 654         }
 655         if (declaringClass.isRecord()) {
 656             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 657         }
 658         if (declaringClass.isValue()) {
 659             throw new UnsupportedOperationException("can't get field offset on a value class: " + f);
 660         }
 661         return theInternalUnsafe.objectFieldOffset(f);
 662     }
 663 
 664     /**
 665      * Reports the location of a given static field, in conjunction with {@link
 666      * #staticFieldBase}.
 667      * <p>Do not expect to perform any sort of arithmetic on this offset;
 668      * it is just a cookie which is passed to the unsafe heap memory accessors.
 669      *
 670      * <p>Any given field will always have the same offset, and no two distinct
 671      * fields of the same class will ever have the same offset.
 672      *
 673      * <p>As of 1.4.1, offsets for fields are represented as long values,
 674      * although the Sun JVM does not use the most significant 32 bits.
 675      * It is hard to imagine a JVM technology which needs more than
 676      * a few bits to encode an offset within a non-array object,
 677      * However, for consistency with other methods in this class,
 678      * this method reports its result as a long value.
 679      *
 680      * @deprecated The guarantee that a field will always have the same offset
 681      * and base may not be true in a future release. The ability to provide an
 682      * offset and object reference to a heap memory accessor will be removed
 683      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 684      *
 685      * @see #getInt(Object, long)
 686      */
 687     @Deprecated(since="18")
 688     @ForceInline
 689     @SuppressWarnings("preview")
 690     public long staticFieldOffset(Field f) {
 691         if (f == null) {
 692             throw new NullPointerException();
 693         }
 694         Class<?> declaringClass = f.getDeclaringClass();
 695         if (declaringClass.isHidden()) {
 696             throw new UnsupportedOperationException("can't get field offset on a hidden class: " + f);
 697         }
 698         if (declaringClass.isRecord()) {
 699             throw new UnsupportedOperationException("can't get field offset on a record class: " + f);
 700         }
 701         if (declaringClass.isValue()) {
 702             throw new UnsupportedOperationException("can't get field offset on a value class: " + f);
 703         }
 704         return theInternalUnsafe.staticFieldOffset(f);
 705     }
 706 
 707     /**
 708      * Reports the location of a given static field, in conjunction with {@link
 709      * #staticFieldOffset}.
 710      * <p>Fetch the base "Object", if any, with which static fields of the
 711      * given class can be accessed via methods like {@link #getInt(Object,
 712      * long)}.  This value may be null.  This value may refer to an object
 713      * which is a "cookie", not guaranteed to be a real Object, and it should
 714      * not be used in any way except as argument to the get and put routines in
 715      * this class.
 716      *
 717      * @deprecated The guarantee that a field will always have the same offset
 718      * and base may not be true in a future release. The ability to provide an
 719      * offset and object reference to a heap memory accessor will be removed
 720      * in a future release. Use {@link java.lang.invoke.VarHandle} instead.
 721      */
 722     @Deprecated(since="18")
 723     @ForceInline
 724     @SuppressWarnings("preview")
 725     public Object staticFieldBase(Field f) {
 726         if (f == null) {
 727             throw new NullPointerException();
 728         }
 729         Class<?> declaringClass = f.getDeclaringClass();
 730         if (declaringClass.isHidden()) {
 731             throw new UnsupportedOperationException("can't get base address on a hidden class: " + f);
 732         }
 733         if (declaringClass.isRecord()) {
 734             throw new UnsupportedOperationException("can't get base address on a record class: " + f);
 735         }
 736         if (declaringClass.isValue()) {
 737             throw new UnsupportedOperationException("can't get field offset on a value class: " + f);
 738         }
 739         return theInternalUnsafe.staticFieldBase(f);
 740     }
 741 
 742     /**
 743      * Reports the offset of the first element in the storage allocation of a
 744      * given array class.  If {@link #arrayIndexScale} returns a non-zero value
 745      * for the same class, you may use that scale factor, together with this
 746      * base offset, to form new offsets to access elements of arrays of the
 747      * given class.
 748      *
 749      * @see #getInt(Object, long)
 750      * @see #putInt(Object, long, int)
 751      */
 752     @ForceInline
 753     public int arrayBaseOffset(Class<?> arrayClass) {
 754         return theInternalUnsafe.arrayBaseOffset(arrayClass);
 755     }
 756 
 757     /** The value of {@code arrayBaseOffset(boolean[].class)} */
 758     public static final int ARRAY_BOOLEAN_BASE_OFFSET = jdk.internal.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
< prev index next >