< prev index next >

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

Print this page
@@ -29,12 +29,10 @@
  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;

@@ -332,63 +330,29 @@
          } 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);
-     }
+     // MemorySegment vector access ops
  
      @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) {
+     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 loadFromByteBufferScoped(
-                     BufferAccess.scope(bb),
+             return loadFromMemorySegmentScopedInternal(
+                     msp.scope(),
                      vmClass, e, length,
-                     bb, offset,
+                     msp, offset,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }

@@ -396,43 +360,44 @@
  
      @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) {
+     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 {
-             if (scope != null) {
-                 scope.checkValidState();
-             }
- 
-             final byte[] base = (byte[]) BufferAccess.bufferBase(bb);
+             scope.checkValidState();
  
              return VectorSupport.load(vmClass, e, length,
-                       base, BufferAccess.bufferAddress(bb, offset),
-                       bb, offset, s,
-                       defaultImpl);
+                     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 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) {
+     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 loadFromByteBufferMaskedScoped(
-                     BufferAccess.scope(bb),
+             return loadFromMemorySegmentMaskedScopedInternal(
+                     msp.scope(),
                      vmClass, maskClass, e, length,
-                     bb, offset, m,
+                     msp, offset, m,
                      s,
                      defaultImpl);
          } catch (ScopedMemoryAccess.Scope.ScopedAccessError ex) {
              throw new IllegalStateException("This segment is already closed");
          }

@@ -441,111 +406,113 @@
      @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) {
+     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 {
-             if (scope != null) {
-                 scope.checkValidState();
-             }
+             scope.checkValidState();
  
              return VectorSupport.loadMasked(vmClass, maskClass, e, length,
-                     BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset), m,
-                     bb, offset, s,
+                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset, m,
+                     msp, 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) {
+     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 {
-             storeIntoByteBufferScoped(
-                     BufferAccess.scope(bb),
+             storeIntoMemorySegmentScopedInternal(
+                     msp.scope(),
                      vmClass, e, length,
                      v,
-                     bb, offset,
+                     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 storeIntoByteBufferScoped(ScopedMemoryAccess.Scope scope,
-                                    Class<? extends V> vmClass, Class<E> e, int length,
-                                    V v,
-                                    ByteBuffer bb, int offset,
-                                    VectorSupport.StoreVectorOperation<ByteBuffer, V> defaultImpl) {
+     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 {
-             if (scope != null) {
-                 scope.checkValidState();
-             }
- 
-             final byte[] base = (byte[]) BufferAccess.bufferBase(bb);
+             scope.checkValidState();
  
              VectorSupport.store(vmClass, e, length,
-                                 base, BufferAccess.bufferAddress(bb, offset),
-                                 v,
-                                 bb, offset,
-                                 defaultImpl);
+                     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 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) {
+     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 {
-             storeIntoByteBufferMaskedScoped(
-                     BufferAccess.scope(bb),
+             storeIntoMemorySegmentMaskedScopedInternal(
+                     msp.scope(),
                      vmClass, maskClass, e, length,
                      v, m,
-                     bb, offset,
+                     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 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) {
+     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 {
-             if (scope != null) {
-                 scope.checkValidState();
-             }
+             scope.checkValidState();
  
              VectorSupport.storeMasked(vmClass, maskClass, e, length,
-                     BufferAccess.bufferBase(bb), BufferAccess.bufferAddress(bb, offset),
+                     msp.unsafeGetBase(), msp.unsafeGetOffset() + offset,
                      v, m,
-                     bb, offset,
+                     msp, offset,
                      defaultImpl);
          } finally {
              Reference.reachabilityFence(scope);
          }
      }
< prev index next >