< prev index next >

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

Print this page
@@ -23,130 +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.incubator.foreign.ResourceScope;
- import java.util.Objects;
+ 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 {
+ public final class MemoryAddressImpl implements MemoryAddress, Scoped {
  
-     private final AbstractMemorySegmentImpl segment;
      private final long offset;
  
-     public MemoryAddressImpl(AbstractMemorySegmentImpl segment, long offset) {
-         this.segment = segment;
+     public MemoryAddressImpl(long offset) {
          this.offset = offset;
      }
  
-     Object base() {
-         return segment != null ? segment.base() : null;
+     // MemoryAddress methods
+ 
+     @Override
+     public MemoryAddress addOffset(long offset) {
+         return new MemoryAddressImpl(this.offset + offset);
      }
  
-     long offset() {
-         return segment != null ?
-                 segment.min() + offset : offset;
+     @Override
+     public long toRawLongValue() {
+         return offset;
      }
  
-     // MemoryAddress methods
+     @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 segment != null ?
-                 segment.scope() : ResourceScope.globalScope();
+         return ResourceScopeImpl.GLOBAL;
      }
  
      @Override
-     public MemoryAddress addOffset(long offset) {
-         return new MemoryAddressImpl(segment, this.offset + offset);
+     @CallerSensitive
+     @ForceInline
+     public String getUtf8String(long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         SharedUtils.checkAddress(this);
+         return NativeMemorySegmentImpl.EVERYTHING.getUtf8String(toRawLongValue() + 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();
+     @CallerSensitive
+     @ForceInline
+     public void setUtf8String(long offset, String str) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         SharedUtils.checkAddress(this);
+         NativeMemorySegmentImpl.EVERYTHING.setUtf8String(toRawLongValue() + offset, str);
      }
  
      @Override
-     public boolean isNative() {
-         return base() == null;
+     @ForceInline
+     @CallerSensitive
+     public byte get(ValueLayout.OfByte layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
-     public long toRawLongValue() {
-         if (segment != null) {
-             if (segment.base() != null) {
-                 throw new UnsupportedOperationException("Not a native address");
-             }
-             segment.checkValidState();
-         }
-         return offset();
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfByte layout, long offset, byte value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
-     // Object methods
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public boolean get(ValueLayout.OfBoolean layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
+     }
  
      @Override
-     public int hashCode() {
-         return Objects.hash(base(), offset());
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfBoolean layout, long offset, boolean value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
      @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;
-         }
+     @ForceInline
+     @CallerSensitive
+     public char get(ValueLayout.OfChar layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
-     public String toString() {
-         return "MemoryAddress{ base: " + base() + " offset=0x" + Long.toHexString(offset()) + " }";
+     @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 final MemorySegment asSegment(long bytesSize, ResourceScope scope) {
+     public short get(ValueLayout.OfShort layout, long offset) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
-         return asSegment(bytesSize, null, scope);
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
      @Override
+     @ForceInline
      @CallerSensitive
-     public final MemorySegment asSegment(long bytesSize, Runnable cleanupAction, ResourceScope scope) {
+     public void set(ValueLayout.OfShort layout, long offset, short value) {
          Reflection.ensureNativeAccess(Reflection.getCallerClass());
-         Objects.requireNonNull(scope);
-         if (bytesSize <= 0) {
-             throw new IllegalArgumentException("Invalid size : " + bytesSize);
-         }
-         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(this, bytesSize,
-                 cleanupAction,
-                 (ResourceScopeImpl) scope);
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
-     public static MemorySegment ofLongUnchecked(long value) {
-         return ofLongUnchecked(value, Long.MAX_VALUE);
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public int get(ValueLayout.OfInt layout, long offset) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         return NativeMemorySegmentImpl.EVERYTHING.get(layout, toRawLongValue() + offset);
      }
  
-     public static MemorySegment ofLongUnchecked(long value, long byteSize, ResourceScopeImpl resourceScope) {
-         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, null, resourceScope);
+     @Override
+     @ForceInline
+     @CallerSensitive
+     public void set(ValueLayout.OfInt layout, long offset, int value) {
+         Reflection.ensureNativeAccess(Reflection.getCallerClass());
+         NativeMemorySegmentImpl.EVERYTHING.set(layout, toRawLongValue() + offset, value);
      }
  
-     public static MemorySegment ofLongUnchecked(long value, long byteSize) {
-         return NativeMemorySegmentImpl.makeNativeSegmentUnchecked(MemoryAddress.ofLong(value), byteSize, null, ResourceScopeImpl.GLOBAL);
+     @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 >