< prev index next >

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

Print this page
*** 26,12 ***
  package jdk.internal.foreign;
  
  import jdk.incubator.foreign.MemoryHandles;
  import jdk.incubator.foreign.MemoryLayout;
  import jdk.incubator.foreign.MemorySegment;
- import jdk.internal.access.JavaLangInvokeAccess;
- import jdk.internal.access.SharedSecrets;
  import jdk.internal.access.foreign.MemorySegmentProxy;
  
  import jdk.incubator.foreign.GroupLayout;
  import jdk.incubator.foreign.SequenceLayout;
  import jdk.incubator.foreign.ValueLayout;
--- 26,10 ---

*** 51,16 ***
   * This class provide support for constructing layout paths; that is, starting from a root path (see {@link #rootPath(MemoryLayout, ToLongFunction)},
   * a path can be constructed by selecting layout elements using the selector methods provided by this class
   * (see {@link #sequenceElement()}, {@link #sequenceElement(long)}, {@link #sequenceElement(long, long)}, {@link #groupElement(String)}).
   * Once a path has been fully constructed, clients can ask for the offset associated with the layout element selected
   * by the path (see {@link #offset}), or obtain a memory access var handle to access the selected layout element
!  * given an address pointing to a segment associated with the root layout (see {@link #dereferenceHandle(Class)}).
   */
  public class LayoutPath {
  
-     private static final JavaLangInvokeAccess JLI = SharedSecrets.getJavaLangInvokeAccess();
- 
      private static final MethodHandle ADD_STRIDE;
      private static final MethodHandle MH_ADD_SCALED_OFFSET;
      private static final MethodHandle MH_SLICE;
  
      private static final int UNSPECIFIED_ELEM_INDEX = -1;
--- 49,14 ---
   * This class provide support for constructing layout paths; that is, starting from a root path (see {@link #rootPath(MemoryLayout, ToLongFunction)},
   * a path can be constructed by selecting layout elements using the selector methods provided by this class
   * (see {@link #sequenceElement()}, {@link #sequenceElement(long)}, {@link #sequenceElement(long, long)}, {@link #groupElement(String)}).
   * Once a path has been fully constructed, clients can ask for the offset associated with the layout element selected
   * by the path (see {@link #offset}), or obtain a memory access var handle to access the selected layout element
!  * given an address pointing to a segment associated with the root layout (see {@link #dereferenceHandle()}).
   */
  public class LayoutPath {
  
      private static final MethodHandle ADD_STRIDE;
      private static final MethodHandle MH_ADD_SCALED_OFFSET;
      private static final MethodHandle MH_SLICE;
  
      private static final int UNSPECIFIED_ELEM_INDEX = -1;

*** 154,21 ***
  
      public long offset() {
          return offset;
      }
  
!     public VarHandle dereferenceHandle(Class<?> carrier) {
!         Utils.checkPrimitiveCarrierCompat(carrier, layout);
          checkAlignment(this);
  
          List<Class<?>> expectedCoordinates = new ArrayList<>();
          Deque<Integer> perms = new ArrayDeque<>();
          perms.addFirst(0);
          expectedCoordinates.add(MemorySegment.class);
  
!         VarHandle handle = Utils.fixUpVarHandle(JLI.memoryAccessVarHandle(carrier, true, layout.byteAlignment() - 1,
!                 ((ValueLayout)layout).order()));
  
          for (int i = 0 ; i < strides.length ; i++) {
              expectedCoordinates.add(long.class);
              perms.addFirst(0);
              perms.addLast(i + 1);
--- 150,25 ---
  
      public long offset() {
          return offset;
      }
  
!     public VarHandle dereferenceHandle() {
!         if (!(layout instanceof ValueLayout)) {
+             throw new IllegalArgumentException("Path does not select a value layout");
+         }
          checkAlignment(this);
  
+         Class<?> carrier = ((ValueLayout)layout).carrier();
+ 
          List<Class<?>> expectedCoordinates = new ArrayList<>();
          Deque<Integer> perms = new ArrayDeque<>();
          perms.addFirst(0);
          expectedCoordinates.add(MemorySegment.class);
  
!         VarHandle handle = Utils.makeMemoryAccessVarHandle(carrier, true, layout.byteAlignment() - 1,
!                 ((ValueLayout)layout).order());
  
          for (int i = 0 ; i < strides.length ; i++) {
              expectedCoordinates.add(long.class);
              perms.addFirst(0);
              perms.addLast(i + 1);

*** 224,19 ***
  
      public MemoryLayout map(UnaryOperator<MemoryLayout> op) {
          MemoryLayout newLayout = op.apply(layout);
          if (enclosing == null) {
              return newLayout;
!         } else if (enclosing.layout instanceof SequenceLayout) {
-             SequenceLayout seq = (SequenceLayout)enclosing.layout;
              if (seq.elementCount().isPresent()) {
                  return enclosing.map(l -> dup(l, MemoryLayout.sequenceLayout(seq.elementCount().getAsLong(), newLayout)));
              } else {
                  return enclosing.map(l -> dup(l, MemoryLayout.sequenceLayout(newLayout)));
              }
!         } else if (enclosing.layout instanceof GroupLayout) {
-             GroupLayout g = (GroupLayout)enclosing.layout;
              List<MemoryLayout> newElements = new ArrayList<>(g.memberLayouts());
              //if we selected a layout in a group we must have a valid index
              newElements.set((int)elementIndex, newLayout);
              if (g.isUnion()) {
                  return enclosing.map(l -> dup(l, MemoryLayout.unionLayout(newElements.toArray(new MemoryLayout[0]))));
--- 224,17 ---
  
      public MemoryLayout map(UnaryOperator<MemoryLayout> op) {
          MemoryLayout newLayout = op.apply(layout);
          if (enclosing == null) {
              return newLayout;
!         } else if (enclosing.layout instanceof SequenceLayout seq) {
              if (seq.elementCount().isPresent()) {
                  return enclosing.map(l -> dup(l, MemoryLayout.sequenceLayout(seq.elementCount().getAsLong(), newLayout)));
              } else {
                  return enclosing.map(l -> dup(l, MemoryLayout.sequenceLayout(newLayout)));
              }
!         } else if (enclosing.layout instanceof GroupLayout g) {
              List<MemoryLayout> newElements = new ArrayList<>(g.memberLayouts());
              //if we selected a layout in a group we must have a valid index
              newElements.set((int)elementIndex, newLayout);
              if (g.isUnion()) {
                  return enclosing.map(l -> dup(l, MemoryLayout.unionLayout(newElements.toArray(new MemoryLayout[0]))));
< prev index next >