< prev index next >

src/java.base/share/classes/jdk/internal/misc/X-ScopedMemoryAccess.java.template

Print this page
*** 29,12 ***
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  import java.lang.ref.Reference;
  import java.io.FileDescriptor;
- import java.nio.Buffer;
- import java.nio.ByteBuffer;
  
  import jdk.internal.access.JavaNioAccess;
  import jdk.internal.access.SharedSecrets;
  import jdk.internal.access.foreign.MemorySegmentProxy;
  import jdk.internal.util.ArraysSupport;
--- 29,10 ---

*** 332,63 ***
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
!     // ByteBuffer vector access ops
- 
-     // Buffer access constants, to be initialized when required.
-     // Avoids a null value for NIO_ACCESS, due to class initialization dependencies
-     static final class BufferAccess {
-         // Buffer.address
-         static final long BUFFER_ADDRESS
-                 = UNSAFE.objectFieldOffset(Buffer.class, "address");
- 
-         // ByteBuffer.hb
-         static final long BYTE_BUFFER_HB
-                 = UNSAFE.objectFieldOffset(ByteBuffer.class, "hb");
- 
-         static final long BYTE_BUFFER_IS_READ_ONLY
-                 = UNSAFE.objectFieldOffset(ByteBuffer.class, "isReadOnly");
- 
-         @ForceInline
-         static Object bufferBase(ByteBuffer bb) {
-             return UNSAFE.getReference(bb, BYTE_BUFFER_HB);
-         }
- 
-         @ForceInline
-         static long bufferAddress(ByteBuffer bb, long offset) {
-             return UNSAFE.getLong(bb, BUFFER_ADDRESS) + offset;
-         }
- 
-         static final JavaNioAccess NIO_ACCESS = SharedSecrets.getJavaNioAccess();
- 
-         @ForceInline
-         static ScopedMemoryAccess.Scope scope(ByteBuffer bb) {
-             MemorySegmentProxy segmentProxy = NIO_ACCESS.bufferSegment(bb);
-             return segmentProxy != null ?
-                     segmentProxy.scope() : null;
-         }
-     }
- 
-     @ForceInline
-     public static boolean isReadOnly(ByteBuffer bb) {
-         return UNSAFE.getBoolean(bb, BufferAccess.BYTE_BUFFER_IS_READ_ONLY);
-     }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
!     V loadFromByteBuffer(Class<? extends V> vmClass, Class<E> e, int length,
!                           ByteBuffer bb, int offset,
!                           S s,
!                           VectorSupport.LoadOperation<ByteBuffer, V, S> defaultImpl) {
          try {
!             return loadFromByteBufferScoped(
!                     BufferAccess.scope(bb),
                      vmClass, e, length,
!                     bb, offset,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
--- 330,29 ---
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
!     // MemorySegment vector access ops
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
!     V loadFromMemorySegment(Class<? extends V> vmClass, Class<E> e, int length,
!                          MemorySegmentProxy msp, long offset,
!                          S s,
!                          VectorSupport.LoadOperation<MemorySegmentProxy, V, S> defaultImpl) {
+         // @@@ Smarter alignment checking if accessing heap segment backing non-byte[] array
+         if (msp.maxAlignMask() > 1) {
+             throw new IllegalArgumentException();
+         }
+ 
          try {
!             return loadFromMemorySegmentScopedInternal(
!                     msp.scope(),
                      vmClass, e, length,
!                     msp, offset,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }

*** 396,43 ***
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
!     V loadFromByteBufferScoped(ScopedMemoryAccess.Scope scope,
!                           Class<? extends V> vmClass, Class<E> e, int length,
!                           ByteBuffer bb, int offset,
!                           S s,
!                           VectorSupport.LoadOperation<ByteBuffer, V, S> defaultImpl) {
          try {
!             if (scope != null) {
-                 scope.checkValidState();
-             }
- 
-             final byte[] base = (byte[]) BufferAccess.bufferBase(bb);
  
              return VectorSupport.load(vmClass, e, length,
!                       base, BufferAccess.bufferAddress(bb, offset),
!                       bb, offset, s,
!                       defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
       M extends VectorSupport.VectorMask<E>>
!     V loadFromByteBufferMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
!                                int length, ByteBuffer bb, int offset, M m, S s,
!                                VectorSupport.LoadVectorMaskedOperation<ByteBuffer, V, S, M> defaultImpl) {
          try {
!             return loadFromByteBufferMaskedScoped(
!                     BufferAccess.scope(bb),
                      vmClass, maskClass, e, length,
!                     bb, offset, m,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
--- 360,44 ---
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>>
!     V loadFromMemorySegmentScopedInternal(ScopedMemoryAccess.Scope scope,
!                                           Class<? extends V> vmClass, Class<E> e, int length,
!                                           MemorySegmentProxy msp, long offset,
!                                           S s,
!                                           VectorSupport.LoadOperation<MemorySegmentProxy, V, S> defaultImpl) {
          try {
!             scope.checkValidState();
  
              return VectorSupport.load(vmClass, e, length,
!                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset,
!                     msp, offset, s,
!                     defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
       M extends VectorSupport.VectorMask<E>>
!     V loadFromMemorySegmentMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
!                                   int length, MemorySegmentProxy msp, long offset, M m, S s,
!                                   VectorSupport.LoadVectorMaskedOperation<MemorySegmentProxy, V, S, M> defaultImpl) {
+         // @@@ Smarter alignment checking if accessing heap segment backing non-byte[] array
+         if (msp.maxAlignMask() > 1) {
+             throw new IllegalArgumentException();
+         }
+ 
          try {
!             return loadFromMemorySegmentMaskedScopedInternal(
!                     msp.scope(),
                      vmClass, maskClass, e, length,
!                     msp, offset, m,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }

*** 441,111 ***
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
       M extends VectorSupport.VectorMask<E>>
!     V loadFromByteBufferMaskedScoped(ScopedMemoryAccess.Scope scope, Class<? extends V> vmClass,
!                                      Class<M> maskClass, Class<E> e, int length,
!                                      ByteBuffer bb, int offset, M m,
!                                      S s,
!                                      VectorSupport.LoadVectorMaskedOperation<ByteBuffer, V, S, M> defaultImpl) {
          try {
!             if (scope != null) {
-                 scope.checkValidState();
-             }
  
              return VectorSupport.loadMasked(vmClass, maskClass, e, length,
!                     BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset), m,
!                     bb, offset, s,
                      defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E>
!     void storeIntoByteBuffer(Class<? extends V> vmClass, Class<E> e, int length,
!                              V v,
!                              ByteBuffer bb, int offset,
!                              VectorSupport.StoreVectorOperation<ByteBuffer, V> defaultImpl) {
          try {
!             storeIntoByteBufferScoped(
!                     BufferAccess.scope(bb),
                      vmClass, e, length,
                      v,
!                     bb, offset,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
      }
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E>
!     void storeIntoByteBufferScoped(ScopedMemoryAccess.Scope scope,
!                                    Class<? extends V> vmClass, Class<E> e, int length,
!                                    V v,
!                                    ByteBuffer bb, int offset,
!                                    VectorSupport.StoreVectorOperation<ByteBuffer, V> defaultImpl) {
          try {
!             if (scope != null) {
-                 scope.checkValidState();
-             }
- 
-             final byte[] base = (byte[]) BufferAccess.bufferBase(bb);
  
              VectorSupport.store(vmClass, e, length,
!                                 base, BufferAccess.bufferAddress(bb, offset),
!                                 v,
!                                 bb, offset,
!                                 defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
!     void storeIntoByteBufferMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
!                                    int length, V v, M m,
!                                    ByteBuffer bb, int offset,
!                                    VectorSupport.StoreVectorMaskedOperation<ByteBuffer, V, M> defaultImpl) {
          try {
!             storeIntoByteBufferMaskedScoped(
!                     BufferAccess.scope(bb),
                      vmClass, maskClass, e, length,
                      v, m,
!                     bb, offset,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
      }
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
!     void storeIntoByteBufferMaskedScoped(ScopedMemoryAccess.Scope scope,
!                                          Class<? extends V> vmClass, Class<M> maskClass,
!                                          Class<E> e, int length, V v, M m,
!                                          ByteBuffer bb, int offset,
!                                          VectorSupport.StoreVectorMaskedOperation<ByteBuffer, V, M> defaultImpl) {
          try {
!             if (scope != null) {
-                 scope.checkValidState();
-             }
  
              VectorSupport.storeMasked(vmClass, maskClass, e, length,
!                     BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset),
                      v, m,
!                     bb, offset,
                      defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
--- 406,113 ---
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, S extends VectorSupport.VectorSpecies<E>,
       M extends VectorSupport.VectorMask<E>>
!     V loadFromMemorySegmentMaskedScopedInternal(ScopedMemoryAccess.Scope scope, Class<? extends V> vmClass,
!                                                 Class<M> maskClass, Class<E> e, int length,
!                                                 MemorySegmentProxy msp, long offset, M m,
!                                                 S s,
!                                                 VectorSupport.LoadVectorMaskedOperation<MemorySegmentProxy, V, S, M> defaultImpl) {
          try {
!             scope.checkValidState();
  
              return VectorSupport.loadMasked(vmClass, maskClass, e, length,
!                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, m,
!                     msp, offset, s,
                      defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E>
!     void storeIntoMemorySegment(Class<? extends V> vmClass, Class<E> e, int length,
!                                 V v,
!                                 MemorySegmentProxy msp, long offset,
!                                 VectorSupport.StoreVectorOperation<MemorySegmentProxy, V> defaultImpl) {
+         // @@@ Smarter alignment checking if accessing heap segment backing non-byte[] array
+         if (msp.maxAlignMask() > 1) {
+             throw new IllegalArgumentException();
+         }
+ 
          try {
!             storeIntoMemorySegmentScopedInternal(
!                     msp.scope(),
                      vmClass, e, length,
                      v,
!                     msp, offset,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
      }
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E>
!     void storeIntoMemorySegmentScopedInternal(ScopedMemoryAccess.Scope scope,
!                                               Class<? extends V> vmClass, Class<E> e, int length,
!                                               V v,
!                                               MemorySegmentProxy msp, long offset,
!                                               VectorSupport.StoreVectorOperation<MemorySegmentProxy, V> defaultImpl) {
          try {
!             scope.checkValidState();
  
              VectorSupport.store(vmClass, e, length,
!                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset,
!                     v,
!                     msp, offset,
!                     defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
  
      @ForceInline
      public static
      <V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
!     void storeIntoMemorySegmentMasked(Class<? extends V> vmClass, Class<M> maskClass, Class<E> e,
!                                       int length, V v, M m,
!                                       MemorySegmentProxy msp, long offset,
!                                       VectorSupport.StoreVectorMaskedOperation<MemorySegmentProxy, V, M> defaultImpl) {
+         // @@@ Smarter alignment checking if accessing heap segment backing non-byte[] array
+         if (msp.maxAlignMask() > 1) {
+             throw new IllegalArgumentException();
+         }
+ 
          try {
!             storeIntoMemorySegmentMaskedScopedInternal(
!                     msp.scope(),
                      vmClass, maskClass, e, length,
                      v, m,
!                     msp, offset,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }
      }
  
      @Scoped
      @ForceInline
      private static
      <V extends VectorSupport.Vector<E>, E, M extends VectorSupport.VectorMask<E>>
!     void storeIntoMemorySegmentMaskedScopedInternal(ScopedMemoryAccess.Scope scope,
!                                                     Class<? extends V> vmClass, Class<M> maskClass,
!                                                     Class<E> e, int length, V v, M m,
!                                                     MemorySegmentProxy msp, long offset,
!                                                     VectorSupport.StoreVectorMaskedOperation<MemorySegmentProxy, V, M> defaultImpl) {
          try {
!             scope.checkValidState();
  
              VectorSupport.storeMasked(vmClass, maskClass, e, length,
!                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset,
                      v, m,
!                     msp, offset,
                      defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
< prev index next >