< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/MemoryAddressImpl.java

Print this page
*** 23,130 ***
   *  questions.
   *
   */
  package jdk.internal.foreign;
  
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemorySegment;
  import jdk.internal.reflect.CallerSensitive;
  import jdk.internal.reflect.Reflection;
  
! import jdk.incubator.foreign.ResourceScope;
- import java.util.Objects;
  
  /**
   * This class provides an immutable implementation for the {@code MemoryAddress} interface. This class contains information
   * about the segment this address is associated with, as well as an offset into such segment.
   */
! public final class MemoryAddressImpl implements MemoryAddress {
  
-     private final AbstractMemorySegmentImpl segment;
      private final long offset;
  
!     public MemoryAddressImpl(AbstractMemorySegmentImpl segment, long offset) {
-         this.segment = segment;
          this.offset = offset;
      }
  
!     Object base() {
!         return segment != null ? segment.base() : null;
      }
  
!     long offset() {
!         return segment != null ?
!                 segment.min() + offset : offset;
      }
  
!     // MemoryAddress methods
  
      @Override
      public ResourceScope scope() {
!         return segment != null ?
-                 segment.scope() : ResourceScope.globalScope();
      }
  
      @Override
!     public MemoryAddress addOffset(long offset) {
!         return new MemoryAddressImpl(segment, this.offset + offset);
      }
  
      @Override
!     public long segmentOffset(MemorySegment segment) {
!         Objects.requireNonNull(segment);
!         AbstractMemorySegmentImpl segmentImpl = (AbstractMemorySegmentImpl)segment;
!         if (segmentImpl.base() != base()) {
!             throw new IllegalArgumentException("Incompatible segment: " + segment);
!         }
-         return offset() - segmentImpl.min();
      }
  
      @Override
!     public boolean isNative() {
!         return base() == null;
      }
  
      @Override
!     public long toRawLongValue() {
!         if (segment != null) {
!             if (segment.base() != null) {
!                 throw new UnsupportedOperationException("Not a native address");
!             }
-             segment.checkValidState();
-         }
-         return offset();
      }
  
!     // Object methods
  
      @Override
!     public int hashCode() {
!         return Objects.hash(base(), offset());
      }
  
      @Override
!     public boolean equals(Object that) {
!         if (that instanceof MemoryAddressImpl) {
!             MemoryAddressImpl addr = (MemoryAddressImpl)that;
!             return Objects.equals(base(), addr.base()) &&
!                     offset() == addr.offset();
-         } else {
-             return false;
-         }
      }
  
      @Override
!     public String toString() {
!         return "MemoryAddress{ base: " + base() + " offset=0x" + Long.toHexString(offset()) + " }";
      }
  
      @Override
      @CallerSensitive
!     public final MemorySegment asSegment(long bytesSize, ResourceScope scope) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         return asSegment(bytesSize, null, scope);
      }
  
      @Override
      @CallerSensitive
!     public final MemorySegment asSegment(long bytesSize, Runnable cleanupAction, ResourceScope scope) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         Objects.requireNonNull(scope);
-         if (bytesSize <= 0) {
-             throw new IllegalArgumentException("Invalid size : " + bytesSize);
-         }
-         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(this, bytesSize,
-                 cleanupAction,
-                 (ResourceScopeImpl) scope);
      }
  
!     public static MemorySegment ofLongUnchecked(long value) {
!         return ofLongUnchecked(value, Long.MAX_VALUE);
      }
  
!     public static MemorySegment ofLongUnchecked(long value, long byteSize, ResourceScopeImpl resourceScope) {
!         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, null, resourceScope);
      }
  
!     public static MemorySegment ofLongUnchecked(long value, long byteSize) {
!         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, null, ResourceScopeImpl.GLOBAL);
      }
  }
--- 23,354 ---
   *  questions.
   *
   */
  package jdk.internal.foreign;
  
+ import jdk.incubator.foreign.Addressable;
  import jdk.incubator.foreign.MemoryAddress;
  import jdk.incubator.foreign.MemorySegment;
+ import jdk.incubator.foreign.ResourceScope;
+ import jdk.incubator.foreign.ValueLayout;
+ import jdk.internal.foreign.abi.SharedUtils;
  import jdk.internal.reflect.CallerSensitive;
  import jdk.internal.reflect.Reflection;
  
! import jdk.internal.vm.annotation.ForceInline;
  
  /**
   * This class provides an immutable implementation for the {@code MemoryAddress} interface. This class contains information
   * about the segment this address is associated with, as well as an offset into such segment.
   */
! public final class MemoryAddressImpl implements MemoryAddress, Scoped {
  
      private final long offset;
  
!     public MemoryAddressImpl(long offset) {
          this.offset = offset;
      }
  
!     // MemoryAddress methods
! 
+     @Override
+     public MemoryAddress addOffset(long offset) {
+         return new MemoryAddressImpl(this.offset + offset);
      }
  
!     @Override
!     public long toRawLongValue() {
!         return offset;
      }
  
!     @Override
+     public final MemoryAddress address() {
+         return this;
+     }
+ 
+     // Object methods
+ 
+     @Override
+     public int hashCode() {
+         return (int) toRawLongValue();
+     }
+ 
+     @Override
+     public boolean equals(Object that) {
+         return (that instanceof MemoryAddressImpl addressImpl &&
+             offset == addressImpl.offset);
+     }
+ 
+     @Override
+     public String toString() {
+         return "MemoryAddress{ offset=0x" + Long.toHexString(offset) + " }";
+     }
+ 
+     public static MemorySegment ofLongUnchecked(long value) {
+         return ofLongUnchecked(value, Long.MAX_VALUE);
+     }
+ 
+     public static MemorySegment ofLongUnchecked(long value, long byteSize, ResourceScopeImpl resourceScope) {
+         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, resourceScope);
+     }
+ 
+     public static MemorySegment ofLongUnchecked(long value, long byteSize) {
+         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, ResourceScopeImpl.GLOBAL);
+     }
  
      @Override
      public ResourceScope scope() {
!         return ResourceScopeImpl.GLOBAL;
      }
  
      @Override
!     @CallerSensitive
!     @ForceInline
+     public String getUtf8String(long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         SharedUtils.checkAddress(this);
+         return NativeMemorySegmentImpl.EVERYTHING.getUtf8String(toRawLongValue() + offset);
      }
  
      @Override
!     @CallerSensitive
!     @ForceInline
!     public void setUtf8String(long offset, String str) {
!         Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         SharedUtils.checkAddress(this);
!         NativeMemorySegmentImpl.EVERYTHING.setUtf8String(toRawLongValue() + offset, str);
      }
  
      @Override
!     @ForceInline
!     @CallerSensitive
+     public byte get(ValueLayout.OfByte layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
!     @ForceInline
!     @CallerSensitive
!     public void set(ValueLayout.OfByte layout, long offset, byte value) {
!         Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
!     @Override
+     @ForceInline
+     @CallerSensitive
+     public boolean get(ValueLayout.OfBoolean layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
  
      @Override
!     @ForceInline
!     @CallerSensitive
+     public void set(ValueLayout.OfBoolean layout, long offset, boolean value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
      @Override
!     @ForceInline
!     @CallerSensitive
!     public char get(ValueLayout.OfChar layout, long offset) {
!         Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
!     @ForceInline
!     @CallerSensitive
+     public void set(ValueLayout.OfChar layout, long offset, char value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
      @Override
+     @ForceInline
      @CallerSensitive
!     public short get(ValueLayout.OfShort layout, long offset) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
+     @ForceInline
      @CallerSensitive
!     public void set(ValueLayout.OfShort layout, long offset, short value) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
!         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
!     @Override
!     @ForceInline
+     @CallerSensitive
+     public int get(ValueLayout.OfInt layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
!     @Override
!     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfInt layout, long offset, int value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
!     @Override
!     @ForceInline
+     @CallerSensitive
+     public float get(ValueLayout.OfFloat layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfFloat layout, long offset, float value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public long get(ValueLayout.OfLong layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfLong layout, long offset, long value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public double get(ValueLayout.OfDouble layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfDouble layout, long offset, double value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public MemoryAddress get(ValueLayout.OfAddress layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfAddress layout, long offset, Addressable value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value.address());
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public char getAtIndex(ValueLayout.OfChar layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfChar layout, long index, char value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public short getAtIndex(ValueLayout.OfShort layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfShort layout, long index, short value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public int getAtIndex(ValueLayout.OfInt layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfInt layout, long index, int value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public float getAtIndex(ValueLayout.OfFloat layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfFloat layout, long index, float value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public long getAtIndex(ValueLayout.OfLong layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfLong layout, long index, long value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public double getAtIndex(ValueLayout.OfDouble layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfDouble layout, long index, double value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value);
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public MemoryAddress getAtIndex(ValueLayout.OfAddress layout, long index) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + (index * layout.byteSize()));
+     }
+ 
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void setAtIndex(ValueLayout.OfAddress layout, long index, Addressable value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + (index * layout.byteSize()), value.address());
      }
  }
< prev index next >