< prev index next >

src/java.base/share/classes/jdk/internal/misc/Unsafe.java

Print this page
rev 17358 : 8182487: Add Unsafe.objectFieldOffset(Class, String)
Reviewed-by: dsimms


 937      * and base.
 938      *
 939      * <p>As of 1.4.1, offsets for fields are represented as long values,
 940      * although the Sun JVM does not use the most significant 32 bits.
 941      * However, JVM implementations which store static fields at absolute
 942      * addresses can use long offsets and null base pointers to express
 943      * the field locations in a form usable by {@link #getInt(Object,long)}.
 944      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 945      * must preserve all bits of static field offsets.
 946      * @see #getInt(Object, long)
 947      */
 948     public long objectFieldOffset(Field f) {
 949         if (f == null) {
 950             throw new NullPointerException();
 951         }
 952 
 953         return objectFieldOffset0(f);
 954     }
 955 
 956     /**



















 957      * Reports the location of a given static field, in conjunction with {@link
 958      * #staticFieldBase}.
 959      * <p>Do not expect to perform any sort of arithmetic on this offset;
 960      * it is just a cookie which is passed to the unsafe heap memory accessors.
 961      *
 962      * <p>Any given field will always have the same offset, and no two distinct
 963      * fields of the same class will ever have the same offset.
 964      *
 965      * <p>As of 1.4.1, offsets for fields are represented as long values,
 966      * although the Sun JVM does not use the most significant 32 bits.
 967      * It is hard to imagine a JVM technology which needs more than
 968      * a few bits to encode an offset within a non-array object,
 969      * However, for consistency with other methods in this class,
 970      * this method reports its result as a long value.
 971      * @see #getInt(Object, long)
 972      */
 973     public long staticFieldOffset(Field f) {
 974         if (f == null) {
 975             throw new NullPointerException();
 976         }


3668     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
3669     private static long toUnsignedLong(short n) { return n & 0xffffl; }
3670     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
3671 
3672     // Maybe byte-reverse an integer
3673     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
3674     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
3675     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
3676     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
3677 
3678 
3679 
3680     private native long allocateMemory0(long bytes);
3681     private native long reallocateMemory0(long address, long bytes);
3682     private native void freeMemory0(long address);
3683     private native void setMemory0(Object o, long offset, long bytes, byte value);
3684     @HotSpotIntrinsicCandidate
3685     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3686     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3687     private native long objectFieldOffset0(Field f);

3688     private native long staticFieldOffset0(Field f);
3689     private native Object staticFieldBase0(Field f);
3690     private native boolean shouldBeInitialized0(Class<?> c);
3691     private native void ensureClassInitialized0(Class<?> c);
3692     private native int arrayBaseOffset0(Class<?> arrayClass);
3693     private native int arrayIndexScale0(Class<?> arrayClass);
3694     private native int addressSize0();
3695     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3696     private native int getLoadAverage0(double[] loadavg, int nelems);
3697     private native boolean unalignedAccess0();
3698     private native boolean isBigEndian0();
3699 }


 937      * and base.
 938      *
 939      * <p>As of 1.4.1, offsets for fields are represented as long values,
 940      * although the Sun JVM does not use the most significant 32 bits.
 941      * However, JVM implementations which store static fields at absolute
 942      * addresses can use long offsets and null base pointers to express
 943      * the field locations in a form usable by {@link #getInt(Object,long)}.
 944      * Therefore, code which will be ported to such JVMs on 64-bit platforms
 945      * must preserve all bits of static field offsets.
 946      * @see #getInt(Object, long)
 947      */
 948     public long objectFieldOffset(Field f) {
 949         if (f == null) {
 950             throw new NullPointerException();
 951         }
 952 
 953         return objectFieldOffset0(f);
 954     }
 955 
 956     /**
 957      * Reports the location of the field with a given name in the storage
 958      * allocation of its class.
 959      *
 960      * @throws NullPointerException if any parameter is {@code null}.
 961      * @throws InternalError if there is no field named {@code name} declared
 962      *         in class {@code c}, i.e., if {@code c.getDeclaredField(name)}
 963      *         would throw {@code java.lang.NoSuchFieldException}.
 964      *
 965      * @see #objectFieldOffset(Field)
 966      */
 967     public long objectFieldOffset(Class<?> c, String name) {
 968         if (c == null || name == null) {
 969             throw new NullPointerException();
 970         }
 971 
 972         return objectFieldOffset1(c, name);
 973     }
 974 
 975     /**
 976      * Reports the location of a given static field, in conjunction with {@link
 977      * #staticFieldBase}.
 978      * <p>Do not expect to perform any sort of arithmetic on this offset;
 979      * it is just a cookie which is passed to the unsafe heap memory accessors.
 980      *
 981      * <p>Any given field will always have the same offset, and no two distinct
 982      * fields of the same class will ever have the same offset.
 983      *
 984      * <p>As of 1.4.1, offsets for fields are represented as long values,
 985      * although the Sun JVM does not use the most significant 32 bits.
 986      * It is hard to imagine a JVM technology which needs more than
 987      * a few bits to encode an offset within a non-array object,
 988      * However, for consistency with other methods in this class,
 989      * this method reports its result as a long value.
 990      * @see #getInt(Object, long)
 991      */
 992     public long staticFieldOffset(Field f) {
 993         if (f == null) {
 994             throw new NullPointerException();
 995         }


3687     private static long toUnsignedLong(byte n)  { return n & 0xffl; }
3688     private static long toUnsignedLong(short n) { return n & 0xffffl; }
3689     private static long toUnsignedLong(int n)   { return n & 0xffffffffl; }
3690 
3691     // Maybe byte-reverse an integer
3692     private static char convEndian(boolean big, char n)   { return big == BE ? n : Character.reverseBytes(n); }
3693     private static short convEndian(boolean big, short n) { return big == BE ? n : Short.reverseBytes(n)    ; }
3694     private static int convEndian(boolean big, int n)     { return big == BE ? n : Integer.reverseBytes(n)  ; }
3695     private static long convEndian(boolean big, long n)   { return big == BE ? n : Long.reverseBytes(n)     ; }
3696 
3697 
3698 
3699     private native long allocateMemory0(long bytes);
3700     private native long reallocateMemory0(long address, long bytes);
3701     private native void freeMemory0(long address);
3702     private native void setMemory0(Object o, long offset, long bytes, byte value);
3703     @HotSpotIntrinsicCandidate
3704     private native void copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
3705     private native void copySwapMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes, long elemSize);
3706     private native long objectFieldOffset0(Field f);
3707     private native long objectFieldOffset1(Class<?> c, String name);
3708     private native long staticFieldOffset0(Field f);
3709     private native Object staticFieldBase0(Field f);
3710     private native boolean shouldBeInitialized0(Class<?> c);
3711     private native void ensureClassInitialized0(Class<?> c);
3712     private native int arrayBaseOffset0(Class<?> arrayClass);
3713     private native int arrayIndexScale0(Class<?> arrayClass);
3714     private native int addressSize0();
3715     private native Class<?> defineAnonymousClass0(Class<?> hostClass, byte[] data, Object[] cpPatches);
3716     private native int getLoadAverage0(double[] loadavg, int nelems);
3717     private native boolean unalignedAccess0();
3718     private native boolean isBigEndian0();
3719 }
< prev index next >