< prev index next >

src/java.base/share/classes/java/lang/invoke/X-VarHandleByteArrayView.java.template

Print this page
rev 14353 : 8155258: VarHandle implementation improvements
Reviewed-by:
rev 14354 : 8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
Reviewed-by:


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 package java.lang.invoke;
  26 
  27 import jdk.internal.misc.Unsafe;

  28 import jdk.internal.vm.annotation.ForceInline;
  29 
  30 import java.nio.ByteBuffer;
  31 import java.nio.ReadOnlyBufferException;
  32 import java.util.Objects;
  33 
  34 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  35 
  36 #warn
  37 
  38 final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
  39 
  40     static final int ALIGN = $BoxType$.BYTES - 1;
  41 
  42 #if[floatingPoint]
  43     @ForceInline
  44     static $rawType$ convEndian(boolean big, $type$ v) {
  45         $rawType$ rv = $Type$.$type$ToRaw$RawType$Bits(v);
  46         return big == BE ? rv : $RawBoxType$.reverseBytes(rv);
  47     }


  64 
  65         ByteArrayViewVarHandle(VarForm form, boolean be) {
  66             super(form);
  67             this.be = be;
  68         }
  69     }
  70 
  71     static final class ArrayHandle extends ByteArrayViewVarHandle {
  72 
  73         ArrayHandle(boolean be) {
  74             super(ArrayHandle.FORM, be);
  75         }
  76 
  77         @Override
  78         final MethodType accessModeTypeUncached(AccessMode accessMode) {
  79             return accessMode.at.accessModeType(byte[].class, $type$.class, int.class);
  80         }
  81 
  82         @ForceInline
  83         static int index(byte[] ba, int index) {
  84             return Objects.checkIndex(index, ba.length - ALIGN, null);
  85         }
  86 
  87         @ForceInline
  88         static long address(byte[] ba, int index) {
  89             long address = ((long) index) + Unsafe.ARRAY_BYTE_BASE_OFFSET;
  90             if ((address & ALIGN) != 0)
  91                 throw newIllegalStateExceptionForMisalignedAccess(index);
  92             return address;
  93         }
  94 
  95         @ForceInline
  96         static $type$ get(ArrayHandle handle, Object oba, int index) {
  97             byte[] ba = (byte[]) oba;
  98 #if[floatingPoint]
  99             $rawType$ rawValue = UNSAFE.get$RawType$Unaligned(
 100                     ba,
 101                     ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 102                     handle.be);
 103             return $Type$.$rawType$BitsTo$Type$(rawValue);
 104 #else[floatingPoint]


 290         }
 291 #end[AtomicAdd]
 292 
 293         static final VarForm FORM = new VarForm(ArrayHandle.class, byte[].class, $type$.class, int.class);
 294     }
 295 
 296 
 297     static final class ByteBufferHandle extends ByteArrayViewVarHandle {
 298 
 299         ByteBufferHandle(boolean be) {
 300             super(ByteBufferHandle.FORM, be);
 301         }
 302 
 303         @Override
 304         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 305             return accessMode.at.accessModeType(ByteBuffer.class, $type$.class, int.class);
 306         }
 307 
 308         @ForceInline
 309         static int index(ByteBuffer bb, int index) {
 310             return Objects.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null);
 311         }
 312 
 313         @ForceInline
 314         static int indexRO(ByteBuffer bb, int index) {
 315             if (UNSAFE.getBoolean(bb, BYTE_BUFFER_IS_READ_ONLY))
 316                 throw new ReadOnlyBufferException();
 317             return Objects.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null);
 318         }
 319 
 320         @ForceInline
 321         static long address(ByteBuffer bb, int index) {
 322             long address = ((long) index) + UNSAFE.getLong(bb, BUFFER_ADDRESS);
 323             if ((address & ALIGN) != 0)
 324                 throw newIllegalStateExceptionForMisalignedAccess(index);
 325             return address;
 326         }
 327 
 328         @ForceInline
 329         static $type$ get(ByteBufferHandle handle, Object obb, int index) {
 330             ByteBuffer bb = (ByteBuffer) obb;
 331 #if[floatingPoint]
 332             $rawType$ rawValue = UNSAFE.get$RawType$Unaligned(
 333                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
 334                     ((long) index(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS),
 335                     handle.be);
 336             return $Type$.$rawType$BitsTo$Type$(rawValue);
 337 #else[floatingPoint]




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 package java.lang.invoke;
  26 
  27 import jdk.internal.misc.Unsafe;
  28 import jdk.internal.util.Preconditions;
  29 import jdk.internal.vm.annotation.ForceInline;
  30 
  31 import java.nio.ByteBuffer;
  32 import java.nio.ReadOnlyBufferException;
  33 import java.util.Objects;
  34 
  35 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  36 
  37 #warn
  38 
  39 final class VarHandleByteArrayAs$Type$s extends VarHandleByteArrayBase {
  40 
  41     static final int ALIGN = $BoxType$.BYTES - 1;
  42 
  43 #if[floatingPoint]
  44     @ForceInline
  45     static $rawType$ convEndian(boolean big, $type$ v) {
  46         $rawType$ rv = $Type$.$type$ToRaw$RawType$Bits(v);
  47         return big == BE ? rv : $RawBoxType$.reverseBytes(rv);
  48     }


  65 
  66         ByteArrayViewVarHandle(VarForm form, boolean be) {
  67             super(form);
  68             this.be = be;
  69         }
  70     }
  71 
  72     static final class ArrayHandle extends ByteArrayViewVarHandle {
  73 
  74         ArrayHandle(boolean be) {
  75             super(ArrayHandle.FORM, be);
  76         }
  77 
  78         @Override
  79         final MethodType accessModeTypeUncached(AccessMode accessMode) {
  80             return accessMode.at.accessModeType(byte[].class, $type$.class, int.class);
  81         }
  82 
  83         @ForceInline
  84         static int index(byte[] ba, int index) {
  85             return Preconditions.checkIndex(index, ba.length - ALIGN, null);
  86         }
  87 
  88         @ForceInline
  89         static long address(byte[] ba, int index) {
  90             long address = ((long) index) + Unsafe.ARRAY_BYTE_BASE_OFFSET;
  91             if ((address & ALIGN) != 0)
  92                 throw newIllegalStateExceptionForMisalignedAccess(index);
  93             return address;
  94         }
  95 
  96         @ForceInline
  97         static $type$ get(ArrayHandle handle, Object oba, int index) {
  98             byte[] ba = (byte[]) oba;
  99 #if[floatingPoint]
 100             $rawType$ rawValue = UNSAFE.get$RawType$Unaligned(
 101                     ba,
 102                     ((long) index(ba, index)) + Unsafe.ARRAY_BYTE_BASE_OFFSET,
 103                     handle.be);
 104             return $Type$.$rawType$BitsTo$Type$(rawValue);
 105 #else[floatingPoint]


 291         }
 292 #end[AtomicAdd]
 293 
 294         static final VarForm FORM = new VarForm(ArrayHandle.class, byte[].class, $type$.class, int.class);
 295     }
 296 
 297 
 298     static final class ByteBufferHandle extends ByteArrayViewVarHandle {
 299 
 300         ByteBufferHandle(boolean be) {
 301             super(ByteBufferHandle.FORM, be);
 302         }
 303 
 304         @Override
 305         final MethodType accessModeTypeUncached(AccessMode accessMode) {
 306             return accessMode.at.accessModeType(ByteBuffer.class, $type$.class, int.class);
 307         }
 308 
 309         @ForceInline
 310         static int index(ByteBuffer bb, int index) {
 311             return Preconditions.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null);
 312         }
 313 
 314         @ForceInline
 315         static int indexRO(ByteBuffer bb, int index) {
 316             if (UNSAFE.getBoolean(bb, BYTE_BUFFER_IS_READ_ONLY))
 317                 throw new ReadOnlyBufferException();
 318             return Preconditions.checkIndex(index, UNSAFE.getInt(bb, BUFFER_LIMIT) - ALIGN, null);
 319         }
 320 
 321         @ForceInline
 322         static long address(ByteBuffer bb, int index) {
 323             long address = ((long) index) + UNSAFE.getLong(bb, BUFFER_ADDRESS);
 324             if ((address & ALIGN) != 0)
 325                 throw newIllegalStateExceptionForMisalignedAccess(index);
 326             return address;
 327         }
 328 
 329         @ForceInline
 330         static $type$ get(ByteBufferHandle handle, Object obb, int index) {
 331             ByteBuffer bb = (ByteBuffer) obb;
 332 #if[floatingPoint]
 333             $rawType$ rawValue = UNSAFE.get$RawType$Unaligned(
 334                     UNSAFE.getObject(bb, BYTE_BUFFER_HB),
 335                     ((long) index(bb, index)) + UNSAFE.getLong(bb, BUFFER_ADDRESS),
 336                     handle.be);
 337             return $Type$.$rawType$BitsTo$Type$(rawValue);
 338 #else[floatingPoint]


< prev index next >