< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/aarch64/macos/MacOsAArch64VaList.java

Print this page
@@ -24,11 +24,11 @@
   * questions.
   */
  package jdk.internal.foreign.abi.aarch64.macos;
  
  import jdk.incubator.foreign.*;
- import jdk.incubator.foreign.CLinker.VaList;
+ import jdk.internal.foreign.Scoped;
  import jdk.internal.foreign.ResourceScopeImpl;
  import jdk.internal.foreign.abi.SharedUtils;
  import jdk.internal.foreign.abi.SharedUtils.SimpleVaArg;
  import jdk.internal.foreign.abi.aarch64.*;
  

@@ -43,14 +43,14 @@
  /**
   * Simplified va_list implementation used on macOS where all variadic
   * parameters are passed on the stack and the type of va_list decays to
   * char* instead of the structure defined in the AAPCS.
   */
- public non-sealed class MacOsAArch64VaList implements VaList {
+ public non-sealed class MacOsAArch64VaList implements VaList, Scoped {
      public static final Class<?> CARRIER = MemoryAddress.class;
      private static final long VA_SLOT_SIZE_BYTES = 8;
-     private static final VarHandle VH_address = MemoryHandles.asAddressVarHandle(C_POINTER.varHandle(long.class));
+     private static final VarHandle VH_address = C_POINTER.varHandle();
  
      private static final VaList EMPTY = new SharedUtils.EmptyVaList(MemoryAddress.NULL);
  
      private MemorySegment segment;
      private final ResourceScope scope;

@@ -63,78 +63,73 @@
      public static final VaList empty() {
          return EMPTY;
      }
  
      @Override
-     public int vargAsInt(MemoryLayout layout) {
+     public int nextVarg(ValueLayout.OfInt layout) {
          return (int) read(int.class, layout);
      }
  
      @Override
-     public long vargAsLong(MemoryLayout layout) {
+     public long nextVarg(ValueLayout.OfLong layout) {
          return (long) read(long.class, layout);
      }
  
      @Override
-     public double vargAsDouble(MemoryLayout layout) {
+     public double nextVarg(ValueLayout.OfDouble layout) {
          return (double) read(double.class, layout);
      }
  
      @Override
-     public MemoryAddress vargAsAddress(MemoryLayout layout) {
+     public MemoryAddress nextVarg(ValueLayout.OfAddress layout) {
          return (MemoryAddress) read(MemoryAddress.class, layout);
      }
  
      @Override
-     public MemorySegment vargAsSegment(MemoryLayout layout, SegmentAllocator allocator) {
+     public MemorySegment nextVarg(GroupLayout layout, SegmentAllocator allocator) {
          Objects.requireNonNull(allocator);
          return (MemorySegment) read(MemorySegment.class, layout, allocator);
      }
  
-     @Override
-     public MemorySegment vargAsSegment(MemoryLayout layout, ResourceScope scope) {
-         return vargAsSegment(layout, SegmentAllocator.ofScope(scope));
-     }
- 
      private Object read(Class<?> carrier, MemoryLayout layout) {
          return read(carrier, layout, SharedUtils.THROWING_ALLOCATOR);
      }
  
      private Object read(Class<?> carrier, MemoryLayout layout, SegmentAllocator allocator) {
          Objects.requireNonNull(layout);
-         SharedUtils.checkCompatibleType(carrier, layout, MacOsAArch64Linker.ADDRESS_SIZE);
          Object res;
          if (carrier == MemorySegment.class) {
              TypeClass typeClass = TypeClass.classifyLayout(layout);
              res = switch (typeClass) {
                  case STRUCT_REFERENCE -> {
                      MemoryAddress structAddr = (MemoryAddress) VH_address.get(segment);
-                     MemorySegment struct = structAddr.asSegment(layout.byteSize(), scope());
+                     MemorySegment struct = MemorySegment.ofAddressNative(structAddr, layout.byteSize(), scope());
                      MemorySegment seg = allocator.allocate(layout);
                      seg.copyFrom(struct);
                      segment = segment.asSlice(VA_SLOT_SIZE_BYTES);
                      yield seg;
                  }
                  case STRUCT_REGISTER, STRUCT_HFA -> {
-                     MemorySegment struct = allocator.allocate(layout);
-                     struct.copyFrom(segment.asSlice(0L, layout.byteSize()));
+                     MemorySegment struct = allocator.allocate(layout)
+                             .copyFrom(segment.asSlice(0, layout.byteSize()));
                      segment = segment.asSlice(alignUp(layout.byteSize(), VA_SLOT_SIZE_BYTES));
                      yield struct;
                  }
                  default -> throw new IllegalStateException("Unexpected TypeClass: " + typeClass);
              };
          } else {
-             VarHandle reader = SharedUtils.vhPrimitiveOrAddress(carrier, layout);
+             VarHandle reader = layout.varHandle();
              res = reader.get(segment);
              segment = segment.asSlice(VA_SLOT_SIZE_BYTES);
          }
          return res;
      }
  
      @Override
      public void skip(MemoryLayout... layouts) {
          Objects.requireNonNull(layouts);
+         ((ResourceScopeImpl)scope).checkValidStateSlow();
  
          for (MemoryLayout layout : layouts) {
              Objects.requireNonNull(layout);
              segment = segment.asSlice(switch (TypeClass.classifyLayout(layout)) {
                  case STRUCT_REGISTER, STRUCT_HFA -> alignUp(layout.byteSize(), VA_SLOT_SIZE_BYTES);

@@ -142,11 +137,11 @@
              });
          }
      }
  
      static MacOsAArch64VaList ofAddress(MemoryAddress addr, ResourceScope scope) {
-         MemorySegment segment = addr.asSegment(Long.MAX_VALUE, scope);
+         MemorySegment segment = MemorySegment.ofAddressNative(addr, Long.MAX_VALUE, scope);
          return new MacOsAArch64VaList(segment, scope);
      }
  
      static Builder builder(ResourceScope scope) {
          return new Builder(scope);

@@ -179,46 +174,45 @@
          }
  
          private Builder arg(Class<?> carrier, MemoryLayout layout, Object value) {
              Objects.requireNonNull(layout);
              Objects.requireNonNull(value);
-             SharedUtils.checkCompatibleType(carrier, layout, MacOsAArch64Linker.ADDRESS_SIZE);
              args.add(new SimpleVaArg(carrier, layout, value));
              return this;
          }
  
          @Override
-         public Builder vargFromInt(ValueLayout layout, int value) {
+         public Builder addVarg(ValueLayout.OfInt layout, int value) {
              return arg(int.class, layout, value);
          }
  
          @Override
-         public Builder vargFromLong(ValueLayout layout, long value) {
+         public Builder addVarg(ValueLayout.OfLong layout, long value) {
              return arg(long.class, layout, value);
          }
  
          @Override
-         public Builder vargFromDouble(ValueLayout layout, double value) {
+         public Builder addVarg(ValueLayout.OfDouble layout, double value) {
              return arg(double.class, layout, value);
          }
  
          @Override
-         public Builder vargFromAddress(ValueLayout layout, Addressable value) {
+         public Builder addVarg(ValueLayout.OfAddress layout, Addressable value) {
              return arg(MemoryAddress.class, layout, value.address());
          }
  
          @Override
-         public Builder vargFromSegment(GroupLayout layout, MemorySegment value) {
+         public Builder addVarg(GroupLayout layout, MemorySegment value) {
              return arg(MemorySegment.class, layout, value);
          }
  
          public VaList build() {
              if (args.isEmpty()) {
                  return EMPTY;
              }
  
-             SegmentAllocator allocator = SegmentAllocator.arenaAllocator(scope);
+             SegmentAllocator allocator = SegmentAllocator.newNativeArena(scope);
  
              // Each argument may occupy up to four slots
              MemorySegment segment = allocator.allocate(VA_SLOT_SIZE_BYTES * args.size() * 4);
  
              List<MemorySegment> attachedSegments = new ArrayList<>();

@@ -235,14 +229,13 @@
                              copy.copyFrom(msArg); // by-value
                              attachedSegments.add(copy);
                              VH_address.set(cursor, copy.address());
                              cursor = cursor.asSlice(VA_SLOT_SIZE_BYTES);
                          }
-                         case STRUCT_REGISTER, STRUCT_HFA -> {
-                             cursor.copyFrom(msArg.asSlice(0, arg.layout.byteSize()));
-                             cursor = cursor.asSlice(alignUp(arg.layout.byteSize(), VA_SLOT_SIZE_BYTES));
-                         }
+                         case STRUCT_REGISTER, STRUCT_HFA ->
+                             cursor.copyFrom(msArg.asSlice(0, arg.layout.byteSize()))
+                                     .asSlice(alignUp(arg.layout.byteSize(), VA_SLOT_SIZE_BYTES));
                          default -> throw new IllegalStateException("Unexpected TypeClass: " + typeClass);
                      }
                  } else {
                      VarHandle writer = arg.varHandle();
                      writer.set(cursor, arg.value);
< prev index next >