Update to JDK 20 foreign-abi preview API.
authorNot Zed <notzed@gmail.com>
Tue, 9 May 2023 02:56:56 +0000 (12:26 +0930)
committerNot Zed <notzed@gmail.com>
Tue, 9 May 2023 02:56:56 +0000 (12:26 +0930)
18 files changed:
src/notzed.nativez/bin/export-api
src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java
src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java
src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java
src/notzed.nativez/classes/au/notzed/nativez/Frame.java
src/notzed.nativez/classes/au/notzed/nativez/HandleArray.java
src/notzed.nativez/classes/au/notzed/nativez/IntArray.java
src/notzed.nativez/classes/au/notzed/nativez/LongArray.java
src/notzed.nativez/classes/au/notzed/nativez/Memory.java
src/notzed.nativez/classes/au/notzed/nativez/Native.java
src/notzed.nativez/classes/au/notzed/nativez/Pointer.java
src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java
src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java
src/notzed.nativez/lib/code.api
src/notzed.nativez/lib/code.pm
src/notzed.nativez/lib/method.pm
src/notzed.nativez/lib/types.api
src/notzed.nativez/native/native.make

index 6ecd12d..ad662a1 100755 (executable)
@@ -121,7 +121,7 @@ my @cmd = (
        "-fplugin-arg-libexport-output=$var->{workdir}/$apibase.pm",
 #      "-fplugin-arg-libexport-verbose=$var->{verbose}",
        '-O0',
-       '-o', '/dev/null',
+       '-o', "$var->{workdir}/$apibase.pch",
        @includes,
        $apih
 );
index 72aa439..2b86b8d 100644 (file)
@@ -20,9 +20,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class ByteArray extends AbstractList<Byte> implements Pointer {
        final MemorySegment segment;
@@ -35,32 +32,34 @@ public class ByteArray extends AbstractList<Byte> implements Pointer {
                return new ByteArray(segment);
        }
 
-       public static ByteArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length, scope));
+       public static ByteArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length, scope));
        }
 
-       public static ByteArray createArray(MemoryAddress address, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+       public static ByteArray create(MemorySegment address, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
        }
 
-       public static ByteArray createArray(long length, SegmentAllocator alloc) {
+       public static ByteArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.BYTE, length));
        }
 
-       public static ByteArray create(String value, SegmentAllocator alloc) {
+       public static ByteArray create(SegmentAllocator alloc, String value) {
                return create(alloc.allocateUtf8String(value));
        }
 
-       public static ByteArray create(String value, MemorySession scope) {
-               return create(scope.allocateUtf8String(value));
-       }
+       //public static ByteArray create(SegmentScope scope, String value) {
+       //      return create(scope.allocateUtf8String(value));
+       //}
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index 9df27ed..72ed513 100644 (file)
@@ -20,9 +20,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class DoubleArray extends AbstractList<Double> implements Pointer {
        final MemorySegment segment;
@@ -35,15 +32,15 @@ public class DoubleArray extends AbstractList<Double> implements Pointer {
                return new DoubleArray(segment);
        }
 
-       public static DoubleArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length * Memory.DOUBLE.byteSize(), scope));
+       public static DoubleArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length * Memory.DOUBLE.byteSize(), scope));
        }
 
-       public static DoubleArray createArray(MemoryAddress address, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+       public static DoubleArray create(MemorySegment address, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
        }
 
-       public static DoubleArray createArray(long length, SegmentAllocator alloc) {
+       public static DoubleArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.DOUBLE, length));
        }
 
@@ -51,12 +48,14 @@ public class DoubleArray extends AbstractList<Double> implements Pointer {
                return create(alloc.allocateArray(Memory.DOUBLE, values));
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index 13a510a..e49b984 100644 (file)
@@ -20,9 +20,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class FloatArray extends AbstractList<Float> implements Pointer {
        final MemorySegment segment;
@@ -35,15 +32,15 @@ public class FloatArray extends AbstractList<Float> implements Pointer {
                return new FloatArray(segment);
        }
 
-       public static FloatArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length * Memory.FLOAT.byteSize(), scope));
+       public static FloatArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length * Memory.FLOAT.byteSize(), scope));
        }
 
-       public static FloatArray createArray(MemoryAddress address, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+       public static FloatArray create(MemorySegment address, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
        }
 
-       public static FloatArray createArray(long length, SegmentAllocator alloc) {
+       public static FloatArray create(SegmentAllocator alloc, int length) {
                return create(alloc.allocateArray(Memory.FLOAT, length));
        }
 
@@ -51,12 +48,14 @@ public class FloatArray extends AbstractList<Float> implements Pointer {
                return create(alloc.allocateArray(Memory.FLOAT, values));
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index bf65a66..4480287 100644 (file)
@@ -33,20 +33,18 @@ import static java.lang.foreign.ValueLayout.OfAddress;
  */
 public class Frame implements AutoCloseable, SegmentAllocator {
 
-       private final MemorySession scope;
-       private final SegmentAllocator alloc;
+       private final Arena alloc;
 
        Frame() {
-               this.scope = MemorySession.openConfined();
-               this.alloc = SegmentAllocator.newNativeArena(scope);
+               this.alloc = Arena.openConfined();
        }
 
        public static Frame frame() {
                return new Frame();
        }
 
-       public MemorySession scope() {
-               return scope;
+       public SegmentScope scope() {
+               return alloc.scope();
        }
 
        @Override
@@ -56,7 +54,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
 
        @Override
        public void close() {
-               scope.close();
+               alloc.close();
        }
 
        public MemorySegment allocateInt() {
@@ -83,7 +81,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
                return allocateArray(Memory.POINTER, count);
        }
 
-       public MemorySegment allocateArray(OfAddress type, MemoryAddress[] value) {
+       public MemorySegment allocateArray(OfAddress type, MemorySegment[] value) {
                MemorySegment m = allocateArray(type, value.length);
                for (int i=0;i<value.length;i++)
                        m.setAtIndex(type, i, value[i]);
@@ -146,7 +144,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
                return copy(value.address());
        }
 
-       public <T extends Native> MemorySegment copy(MemoryAddress value) {
+       public <T extends Native> MemorySegment copy(MemorySegment value) {
                MemorySegment mem = allocateAddress();
                MemoryAccess.setAddress(mem, value);
                return mem;
@@ -165,6 +163,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
                }
        }
 
+       /*
        public static MemorySegment copy(MemorySegment ctx, String string) {
                if (string != null) {
                        SegmentAllocator alloc = ctx.session();
@@ -188,5 +187,5 @@ public class Frame implements AutoCloseable, SegmentAllocator {
                } else {
                        return Memory.NULL;
                }
-       }
+       }*/
 }
index e1e0ade..ecd9a1c 100644 (file)
@@ -20,9 +20,7 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
 import java.util.function.BiFunction;
-import java.util.List;
 
 /**
  * A HandleArray is a typed PointerArray
@@ -32,36 +30,36 @@ import java.util.List;
  */
 public class HandleArray<T extends Pointer> extends AbstractList<T> implements Pointer {
        public final MemorySegment segment;
-       final MemorySession scope;
-       BiFunction<MemoryAddress,MemorySession,T> create;
+       final SegmentScope scope;
+       BiFunction<MemorySegment,SegmentScope,T> create;
 
-       private HandleArray(MemorySegment segment, BiFunction<MemoryAddress,MemorySession,T> create, MemorySession scope) {
+       private HandleArray(MemorySegment segment, BiFunction<MemorySegment,SegmentScope,T> create, SegmentScope scope) {
                this.segment = segment;
                this.create = create;
                this.scope = scope;
        }
 
-       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemoryAddress,MemorySession,T> create) {
-               return new HandleArray<>(segment, create, segment.session());
+       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemorySegment,SegmentScope,T> create) {
+               return new HandleArray<>(segment, create, segment.scope());
        }
 
-       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemoryAddress,MemorySession,T> create, MemorySession scope) {
+       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemorySegment,SegmentScope,T> create, SegmentScope scope) {
                return new HandleArray<>(segment, create, scope);
        }
 
-       public static <T extends Pointer> HandleArray<T> createArray(long size, SegmentAllocator alloc, BiFunction<MemoryAddress,MemorySession,T> create) {
+       public static <T extends Pointer> HandleArray<T> create(long size, BiFunction<MemorySegment,SegmentScope,T> create, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.POINTER, size), create);
        }
 
-       public static <T extends Pointer> HandleArray<T> createArray(long size, SegmentAllocator alloc, BiFunction<MemoryAddress,MemorySession,T> create, MemorySession scope) {
+       public static <T extends Pointer> HandleArray<T> create(long size, BiFunction<MemorySegment,SegmentScope,T> create, SegmentAllocator alloc, SegmentScope scope) {
                return create(alloc.allocateArray(Memory.POINTER, size), create, scope);
        }
 
-       public static <T extends Pointer> HandleArray<T> createArray(MemoryAddress address, long size, BiFunction<MemoryAddress,MemorySession,T> create, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, size * Memory.POINTER.byteSize(), scope), create);
+       public static <T extends Pointer> HandleArray<T> create(MemorySegment address, long size, BiFunction<MemorySegment,SegmentScope,T> create, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), size * Memory.POINTER.byteSize(), scope), create);
        }
 
-       public static <T extends Pointer> HandleArray<T> create(SegmentAllocator alloc, BiFunction<MemoryAddress,MemorySession,T> create, T... values) {
+       public static <T extends Pointer> HandleArray<T> create(BiFunction<MemorySegment,SegmentScope,T> create, SegmentAllocator alloc, T... values) {
                HandleArray<T> array = create(alloc.allocateArray(Memory.POINTER, values.length), create);
                for (int i=0;i<values.length;i++)
                        array.setAtIndex(i, values[i]);
@@ -77,12 +75,13 @@ public class HandleArray<T extends Pointer> extends AbstractList<T> implements P
        }
 
        @Override
-       public final MemoryAddress address() {
-               return segment.address();
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
@@ -107,11 +106,11 @@ public class HandleArray<T extends Pointer> extends AbstractList<T> implements P
        }
 
        public T getAtIndex(long index) {
-               MemoryAddress ptr = segment.getAtIndex(Memory.POINTER, index);
+               MemorySegment ptr = segment.getAtIndex(Memory.POINTER, index);
                return ptr != null ? create.apply(ptr, scope) : null;
        }
 
        public void setAtIndex(long index, T value) {
-               segment.setAtIndex(Memory.POINTER, index, value != null ? value.address() : MemoryAddress.NULL);
+               segment.setAtIndex(Memory.POINTER, index, value != null ? value.address() : MemorySegment.NULL);
        }
 }
index c4378a2..1451c0d 100644 (file)
@@ -19,9 +19,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class IntArray extends AbstractList<Integer> implements Pointer {
 
@@ -35,15 +32,15 @@ public class IntArray extends AbstractList<Integer> implements Pointer {
                return new IntArray(segment);
        }
 
-       public static IntArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length * Memory.INT.byteSize(), scope));
+       public static IntArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length * Memory.INT.byteSize(), scope));
        }
 
-       public static IntArray createArray(MemoryAddress address, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+       public static IntArray create(MemorySegment address, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
        }
 
-       public static IntArray createArray(long length, SegmentAllocator alloc) {
+       public static IntArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.INT, length));
        }
 
@@ -59,16 +56,14 @@ public class IntArray extends AbstractList<Integer> implements Pointer {
                return create(segment.asSlice(offset * Memory.INT.byteSize(), length * Memory.INT.byteSize()));
        }
 
-       public final MemorySegment segment() {
+       @Override
+       public final MemorySegment address() {
                return segment;
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
-       }
-
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index 7ed18f8..0f2b6a0 100644 (file)
@@ -20,9 +20,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class LongArray extends AbstractList<Long> implements Pointer {
        final MemorySegment segment;
@@ -35,11 +32,11 @@ public class LongArray extends AbstractList<Long> implements Pointer {
                return new LongArray(segment);
        }
 
-       public static LongArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length * Memory.LONG.byteSize(), scope));
+       public static LongArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length * Memory.LONG.byteSize(), scope));
        }
 
-       public static LongArray createArray(long length, SegmentAllocator alloc) {
+       public static LongArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.LONG, length));
        }
 
@@ -47,12 +44,14 @@ public class LongArray extends AbstractList<Long> implements Pointer {
                return create(alloc.allocateArray(Memory.LONG, values));
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index 841284e..908df69 100644 (file)
@@ -34,11 +34,12 @@ public class Memory {
        public static final OfDouble DOUBLE = JAVA_DOUBLE.withBitAlignment(64);
        public static final OfAddress POINTER = ADDRESS.withBitAlignment(64);
 
-       static final MemorySession sharedScope = MemorySession.openShared(); // cleaner?
+       static final SegmentScope sharedScope = SegmentScope.auto(); // cleaner?
        // Note: currently can't create zero-length segments
-       static final MemorySegment NULL = MemorySegment.ofAddress(MemoryAddress.NULL, 1, MemorySession.global());
+       @Deprecated
+       static final MemorySegment NULL = MemorySegment.NULL;
 
-       public static MemorySession sharedScope() {
+       public static SegmentScope sharedScope() {
                return sharedScope;
        }
 
@@ -47,31 +48,31 @@ public class Memory {
        }
 
        public static MethodHandle downcall(String name, FunctionDescriptor desc) {
-               return SymbolLookup.loaderLookup().lookup(name)
+               return SymbolLookup.loaderLookup().find(name)
                        .map(sym -> Linker.nativeLinker().downcallHandle(sym, desc))
                        .orElse(null);
        }
 
-       public static MethodHandle downcall(Addressable sym, FunctionDescriptor desc) {
+       public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc) {
                return Linker.nativeLinker().downcallHandle(sym, desc);
        }
-
-       public static MethodHandle downcall(String name, MemoryAddress sym, FunctionDescriptor desc, MemorySession scope) {
-               return sym != MemoryAddress.NULL
+/*
+       public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc, SegmentScope scope) {
+               return sym != MemorySegment.NULL
                        ? Linker.nativeLinker().downcallHandle(MemorySegment.ofAddress(sym, 0, scope), desc)
                        : null;
        }
 
-       public static MethodHandle downcall(String name, FunctionDescriptor desc, Function<String, MemoryAddress> resolve, MemorySession scope) {
-               MemoryAddress sym = resolve.apply(name);
-               return sym != MemoryAddress.NULL
+       public static MethodHandle downcall(String name, FunctionDescriptor desc, Function<String, MemorySegment> resolve, SegmentScope scope) {
+               MemorySegment sym = resolve.apply(name);
+               return sym != MemorySegment.NULL
                        ? Linker.nativeLinker().downcallHandle(MemorySegment.ofAddress(sym, 0, scope), desc)
                        : null;
        }
-
+*/
        static final MethodHandles.Lookup lookup = MethodHandles.lookup();
 
-       public static MemorySegment upcall(Object instance, FunctionDescriptor desc, MemorySession scope) {
+       public static MemorySegment upcall(Object instance, FunctionDescriptor desc, SegmentScope scope) {
                try {
                        java.lang.reflect.Method m = instance.getClass().getMethods()[0];
                        MethodHandle handle = lookup.findVirtual(instance.getClass(), "call", MethodType.methodType(m.getReturnType(), m.getParameterTypes()))
@@ -82,7 +83,7 @@ public class Memory {
                }
        }
 
-       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, MemorySession scope) {
+       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, SegmentScope scope) {
                try {
                        java.lang.reflect.Method m = instance.getClass().getMethods()[0];
                        MethodHandle handle = lookup.findVirtual(instance.getClass(), m.getName(), MethodType.fromMethodDescriptorString(signature, Memory.class.getClassLoader()))
@@ -93,7 +94,7 @@ public class Memory {
                }
        }
 
-       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, MemorySession scope) {
+       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, SegmentScope scope) {
                try {
                        MethodHandle handle = lookup.findVirtual(instance.getClass(), method, MethodType.fromMethodDescriptorString(signature, Memory.class.getClassLoader()))
                                .bindTo(instance);
@@ -103,16 +104,30 @@ public class Memory {
                }
        }
 
-       public static Addressable address(Addressable v) {
-               return v != null ? v.address() : MemoryAddress.NULL;
+       // TODO: the null check has overhead so only use where necessary
+/*
+       public static MemorySegment address(MemorySegment v) {
+               return v;
+       }
+
+       public static MemorySegment address(Pointer v) {
+               return v.address();
+       }
+
+       public static <T> MemorySegment address(FunctionPointer<T> v) {
+               return v.symbol();
+       }
+       */
+       public static MemorySegment address(MemorySegment v) {
+               return v != null ? v : MemorySegment.NULL;
        }
 
-       public static Addressable address(Pointer v) {
-               return v != null ? v.address() : MemoryAddress.NULL;
+       public static MemorySegment address(Pointer v) {
+               return v != null ? v.address() : MemorySegment.NULL;
        }
 
-       public static <T> Addressable address(FunctionPointer<T> v) {
-               return v != null ? v.symbol().address() : MemoryAddress.NULL;
+       public static <T> MemorySegment address(FunctionPointer<T> v) {
+               return v != null ? v.symbol() : MemorySegment.NULL;
        }
 
        //public static long length(Pointer v) {
@@ -146,20 +161,20 @@ public class Memory {
                        }
        }
 
-       public static String copyString(MemoryAddress string) {
-               return string != MemoryAddress.NULL ? string.getUtf8String(0) : null;
+       public static String copyString(MemorySegment string) {
+               return string != MemorySegment.NULL ? string.getUtf8String(0) : null;
        }
 
        public static MemorySegment copyString(String string, SegmentAllocator alloc) {
                return (string != null) ? alloc.allocateUtf8String(string) : Memory.NULL;
        }
 
-       public static String[] copyStringArray(MemoryAddress list, long len) {
-               if (list != MemoryAddress.NULL) {
+       public static String[] copyStringArray(MemorySegment list, long len) {
+               if (list != MemorySegment.NULL) {
                        String[] array = new String[(int)len];
                        for (int i = 0; i < array.length; i++) {
-                               MemoryAddress fu = list.getAtIndex(Memory.POINTER, i);
-                               array[i] = fu != MemoryAddress.NULL ? fu.getUtf8String(0) : null;
+                               MemorySegment fu = list.getAtIndex(Memory.POINTER, i);
+                               array[i] = fu != MemorySegment.NULL ? fu.getUtf8String(0) : null;
                        }
                        return array;
                } else {
@@ -171,7 +186,7 @@ public class Memory {
                if (array != null) {
                        MemorySegment list = alloc.allocateArray(Memory.POINTER, array.length);
                        for (int i = 0; i < array.length; i++) {
-                               list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateUtf8String(array[i]) : MemoryAddress.NULL);
+                               list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateUtf8String(array[i]) : MemorySegment.NULL);
                        }
                        return list;
                } else {
index cf1dbff..421d427 100644 (file)
  */
 package au.notzed.nativez;
 
-import java.io.StringReader;
-import java.lang.invoke.*;
 import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
 import java.util.function.Function;
-import java.util.function.IntFunction;
 import java.lang.foreign.*;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
@@ -36,7 +30,7 @@ import java.lang.System.Logger.Level;
  * <p>
  * Handles instantiation and provides helper functions for native access.
  * <p>
- * Work in progress.
+ * Work in progress / obsolete?
  * <p>
  * For better safety the 'p' field should be the CHandle, and addr() would
  * call get(). Otherwise one must not release ANY object which might ever
@@ -47,11 +41,11 @@ import java.lang.System.Logger.Level;
  */
 public class Native implements Pointer {
 
-       private final MemoryAddress p;
+       private final MemorySegment p;
 
        private final static boolean dolog = true;
 
-       protected Native(MemoryAddress p) {
+       protected Native(MemorySegment p) {
                this.p = p;
        }
 
@@ -59,12 +53,14 @@ public class Native implements Pointer {
                return System.getLogger("notzed.native");
        }
 
-       public MemoryAddress address() {
+       @Override
+       public MemorySegment address() {
                return p;
        }
 
-       public MemorySession scope() {
-               return MemorySession.global();
+       @Override
+       public SegmentScope scope() {
+               return p.scope();
        }
 
        /* ********************************************************************** */
@@ -80,10 +76,10 @@ public class Native implements Pointer {
         */
        static private final ReferenceQueue<Native> references = new ReferenceQueue<>();
 
-       private static <T extends Native> T createInstance(Class<T> jtype, MemoryAddress p) {
+       private static <T extends Native> T createInstance(Class<T> jtype, MemorySegment p) {
                cleanerStep();
                try {
-                       Class[] params = {MemoryAddress.class};
+                       Class[] params = {MemorySegment.class};
                        Constructor<T> cc = jtype.getDeclaredConstructor(params);
 
                        cc.setAccessible(true);
@@ -96,7 +92,7 @@ public class Native implements Pointer {
        }
 
        /*
-       public static <T extends Native> T resolve(Class<T> jtype, MemoryAddress p) {
+       public static <T extends Native> T resolve(Class<T> jtype, MemorySegment p) {
                T o;
 
                //if (dolog)
@@ -114,13 +110,13 @@ public class Native implements Pointer {
                }
                return o;
                }*/
-       public static <T extends Native> T resolve(Class<T> jtype, MemoryAddress p, Function<MemoryAddress, T> create) {
+       public static <T extends Native> T resolve(Class<T> jtype, MemorySegment p, Function<MemorySegment, T> create) {
                T o;
                boolean step = false;
 
                //if (dolog)
                //      log().log(Level.DEBUG, () -> String.format("  resolv $%016x %s", Memory.toLong(p), create));
-               if (p.toRawLongValue() == 0)
+               if (p.address() == 0)
                        return null;
 
                // Instantiation needs to be synchronized for obvious reasons.
@@ -142,7 +138,7 @@ public class Native implements Pointer {
                        }
                        {
                                T x = o;
-                               log().log(Level.DEBUG, () -> String.format(fmt, p.toRawLongValue(), x.getClass().getName()));
+                               log().log(Level.DEBUG, () -> String.format(fmt, p.address(), x.getClass().getName()));
                        }
                }
 
@@ -181,7 +177,7 @@ public class Native implements Pointer {
 
                if (ref != null) {
                        if (dolog)
-                               log().log(Level.DEBUG, () -> String.format("  force  $%016x %s", p.toRawLongValue(), getClass().getName()));
+                               log().log(Level.DEBUG, () -> String.format("  force  $%016x %s", p.address(), getClass().getName()));
 
                        ref.enqueue();
                }
@@ -246,11 +242,11 @@ public class Native implements Pointer {
 
        private static class CHandle extends WeakReference<Native> {
 
-               protected MemoryAddress p;
+               protected MemorySegment p;
                final Class<? extends Native> jtype;
                CHandle next;
 
-               CHandle(Native referent, ReferenceQueue<Native> references, MemoryAddress p) {
+               CHandle(Native referent, ReferenceQueue<Native> references, MemorySegment p) {
                        super(referent, references);
                        this.p = p;
                        this.jtype = referent.getClass();
@@ -260,9 +256,9 @@ public class Native implements Pointer {
                        try {
                                if (p != null) {
                                        if (dolog)
-                                               log().log(Level.DEBUG, () -> String.format("  releas $%016x %s", p.toRawLongValue(), jtype.getName()));
+                                               log().log(Level.DEBUG, () -> String.format("  releas $%016x %s", p.address(), jtype.getName()));
 
-                                       Method mm = jtype.getDeclaredMethod("release", MemoryAddress.class);
+                                       Method mm = jtype.getDeclaredMethod("release", MemorySegment.class);
                                        mm.setAccessible(true);
                                        mm.invoke(null, p);
                                }
@@ -301,7 +297,7 @@ public class Native implements Pointer {
                /**
                 * Sigh, memoryaddress has a miserable hashCode(), it's even worse than Long.hashCode()
                 */
-               public static final int hashCode(MemoryAddress p) {
+               public static final int hashCode(MemorySegment p) {
                        return p.hashCode() >>> 5;
                }
        }
@@ -334,7 +330,7 @@ public class Native implements Pointer {
                                while (h != null) {
                                        Native o = h.get();
                                        System.out.printf(" $%016x: %s %-40s %s\n",
-                                               h.p.toRawLongValue(),
+                                               h.p.address(),
                                                o == null ? "dead" : "live",
                                                h.jtype.getName(),
                                                o);
@@ -404,7 +400,7 @@ public class Native implements Pointer {
                        size += 1;
                }
 
-               public CHandle get(MemoryAddress p) {
+               public CHandle get(MemorySegment p) {
                        int i = CHandle.hashCode(p) & mask;
                        CHandle h = table[i];
 
@@ -413,7 +409,7 @@ public class Native implements Pointer {
                        return h;
                }
 
-               public CHandle remove(MemoryAddress p) {
+               public CHandle remove(MemorySegment p) {
                        int i = CHandle.hashCode(p) & mask;
                        CHandle h = table[i];
                        CHandle a = null;
index 8837fcc..c9b8503 100644 (file)
@@ -7,11 +7,8 @@ import java.lang.foreign.*;
  * Because you can't implement foriegn.Addressable for some silly reason
  */
 public interface Pointer {
-       MemoryAddress address();
-       default MemorySession scope() {
-               return MemorySession.global();
+       MemorySegment address();
+       default SegmentScope scope() {
+               return SegmentScope.global();
        }
-       //default long length() {
-       //      return 1;
-       //}
 }
index 7d45387..b706e66 100644 (file)
@@ -20,11 +20,8 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
-public class PointerArray extends AbstractList<MemoryAddress> implements Pointer {
+public class PointerArray extends AbstractList<MemorySegment> implements Pointer {
        final MemorySegment segment;
 
        private PointerArray(MemorySegment segment) {
@@ -35,24 +32,29 @@ public class PointerArray extends AbstractList<MemoryAddress> implements Pointer
                return new PointerArray(segment);
        }
 
-       public static PointerArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length, scope));
+       public static PointerArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length, scope));
        }
 
-       public static PointerArray createArray(long length, SegmentAllocator alloc) {
+       public static PointerArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.POINTER, length));
        }
 
-       public static PointerArray create(Frame alloc, MemoryAddress... values) {
-               return create(alloc.allocateArray(Memory.POINTER, values));
+       public static PointerArray create(SegmentAllocator alloc, MemorySegment... values) {
+               MemorySegment m = alloc.allocateArray(Memory.POINTER, values.length);
+               for (int i=0;i<values.length;i++)
+                       m.setAtIndex(Memory.POINTER, i, values[i]);
+               return create(m);
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
@@ -61,13 +63,13 @@ public class PointerArray extends AbstractList<MemoryAddress> implements Pointer
        }
 
        @Override
-       public MemoryAddress get(int index) {
+       public MemorySegment get(int index) {
                return getAtIndex(index);
        }
 
        @Override
-       public MemoryAddress set(int index, MemoryAddress value) {
-               MemoryAddress old = getAtIndex(index);
+       public MemorySegment set(int index, MemorySegment value) {
+               MemorySegment old = getAtIndex(index);
                setAtIndex(index, value);
                return old;
        }
@@ -76,11 +78,11 @@ public class PointerArray extends AbstractList<MemoryAddress> implements Pointer
                return segment.byteSize() / Memory.POINTER.byteSize();
        }
 
-       public MemoryAddress getAtIndex(long index) {
+       public MemorySegment getAtIndex(long index) {
                return segment.getAtIndex(Memory.POINTER, index);
        }
 
-       public void setAtIndex(long index, MemoryAddress value) {
+       public void setAtIndex(long index, MemorySegment value) {
                segment.setAtIndex(Memory.POINTER, index, value);
        }
 }
index 57fb90d..dc751d4 100644 (file)
@@ -20,9 +20,6 @@ package au.notzed.nativez;
 import java.lang.foreign.*;
 
 import java.util.AbstractList;
-import java.util.function.Function;
-import java.util.function.BiFunction;
-import java.util.List;
 
 public class ShortArray extends AbstractList<Short> implements Pointer {
        final MemorySegment segment;
@@ -35,15 +32,15 @@ public class ShortArray extends AbstractList<Short> implements Pointer {
                return new ShortArray(segment);
        }
 
-       public static ShortArray createArray(MemoryAddress address, long length, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, length * Memory.SHORT.byteSize(), scope));
+       public static ShortArray create(MemorySegment address, long length, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), length * Memory.SHORT.byteSize(), scope));
        }
 
-       public static ShortArray createArray(MemoryAddress address, MemorySession scope) {
-               return create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope));
+       public static ShortArray create(MemorySegment address, SegmentScope scope) {
+               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
        }
 
-       public static ShortArray createArray(long length, SegmentAllocator alloc) {
+       public static ShortArray create(long length, SegmentAllocator alloc) {
                return create(alloc.allocateArray(Memory.SHORT, length));
        }
 
@@ -51,12 +48,14 @@ public class ShortArray extends AbstractList<Short> implements Pointer {
                return create(alloc.allocateArray(Memory.SHORT, values));
        }
 
-       public final MemoryAddress address() {
-               return segment.address();
+       @Override
+       public final MemorySegment address() {
+               return segment;
        }
 
-       public final MemorySession scope() {
-               return segment.session();
+       @Override
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
index f735f0f..6741734 100644 (file)
@@ -53,8 +53,8 @@ code method {
 
        # callback function/types
        downcall {{
-       public static FunctionPointer<{rename}> downcall(MemoryAddress addr$, MemorySession scope$) {
-               MemorySegment symbol$ = MemorySegment.ofAddress(addr$, 0, scope$);
+       public static FunctionPointer<{rename}> downcall(MemorySegment addr$, SegmentScope scope$) {
+               MemorySegment symbol$ = MemorySegment.ofAddress(addr$.address(), 0, scope$);
                MethodHandle {rename}$FH = Memory.downcall(symbol$, descriptor());
                return new FunctionPointer<{rename}>(
                        symbol$,
@@ -78,13 +78,13 @@ code method {
        }}
 
        upcall {{
-       public static FunctionPointer<{rename}> upcall({rename} target$, MemorySession scope$) {
+       public static FunctionPointer<{rename}> upcall({rename} target$, SegmentScope scope$) {
                interface Trampoline {
                        {java-result} call({native-arguments});
                }
                Trampoline trampoline = ({native-arguments}) -> {
                        // frame?  scope?
-                       try (MemorySession upcallScope$ = MemorySession.openConfined()) {
+                       try (Arena upcallScope$ = Arena.openConfined()) {
                                {trampoline-result-define}target$.call({java-call});
                                {trampoline-result-return}
                        }
@@ -132,10 +132,10 @@ import au.notzed.nativez.*;
 {imports}
 
 public class {name} {
-       {name}(Function<String,MemoryAddress> resolve, MemorySession scope) {
+       {name}(Function<String,MemorySegment> resolve, SegmentScope scope) {
 {init}
        }
-       public static {name} create(Function<String,MemoryAddress> resolve, MemorySession scope) {
+       public static {name} create(Function<String,MemorySegment> resolve, SegmentScope scope) {
                return new {name}(resolve, scope);
        }
 {defines}
@@ -171,8 +171,8 @@ public class {rename} implements Pointer {
                return new {rename}(segment);
        }
 
-       public static {rename} create(MemoryAddress address, MemorySession scope) {
-               return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize(), scope)) : null;
+       public static {rename} create(MemorySegment address, SegmentScope scope) {
+               return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize(), scope)) : null;
        }
 
        public static {rename} create(SegmentAllocator frame) {
@@ -180,17 +180,13 @@ public class {rename} implements Pointer {
        }
 
        @Override
-       public final MemoryAddress address() {
-               return segment.address();
+       public final MemorySegment address() {
+               return segment;
        }
 
        @Override
-       public final MemorySession scope() {
-               return segment.session();
-       }
-
-       public final MemorySegment segment() {
-               return segment;
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
 {defines}
@@ -221,20 +217,20 @@ public class {rename} implements Pointer, Array<{rename}> {
                return new {rename}(segment);
        }
 
-       public static {rename} create(MemoryAddress address, MemorySession scope) {
-               return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize(), scope)) : null;
+       public static {rename} create(MemorySegment address, SegmentScope scope) {
+               return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize(), scope)) : null;
        }
 
        public static {rename} create(SegmentAllocator frame) {
                return create(frame.allocate(LAYOUT));
        }
 
-       public static {rename} createArray(MemoryAddress address, MemorySession scope) {
-               return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, Long.MAX_VALUE, scope)) : null;
+       public static {rename} createArray(MemorySegment address, SegmentScope scope) {
+               return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)) : null;
        }
 
-       public static {rename} createArray(MemoryAddress address, long length, MemorySession scope) {
-               return MemoryAddress.NULL != address ? create(MemorySegment.ofAddress(address, LAYOUT.byteSize() * length, scope)) : null;
+       public static {rename} createArray(MemorySegment address, long length, SegmentScope scope) {
+               return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize() * length, scope)) : null;
        }
 
        public static {rename} createArray(long length, SegmentAllocator alloc) {
@@ -242,17 +238,13 @@ public class {rename} implements Pointer, Array<{rename}> {
        }
 
        @Override
-       public final MemoryAddress address() {
-               return segment.address();
+       public final MemorySegment address() {
+               return segment;
        }
 
        @Override
-       public final MemorySession scope() {
-               return segment.session();
-       }
-
-       public final MemorySegment segment() {
-               return segment;
+       public final SegmentScope scope() {
+               return segment.scope();
        }
 
        @Override
@@ -291,30 +283,30 @@ import au.notzed.nativez.*;
 
 public class {rename} implements Pointer {
 
-       MemoryAddress address;
-       MemorySession scope;
+       MemorySegment address;
+       SegmentScope scope;
 
-       private {rename}(MemoryAddress address, MemorySession scope) {
+       private {rename}(MemorySegment address, SegmentScope scope) {
                this.address = address;
                this.scope = scope;
 {init}
        }
 
-       public static {rename} create(MemoryAddress address, MemorySession scope) {
-               return MemoryAddress.NULL != address ? new {rename}(address, scope) : null;
+       public static {rename} create(MemorySegment address, SegmentScope scope) {
+               return MemorySegment.NULL != address ? new {rename}(address, scope) : null;
        }
 
-       public static HandleArray<{rename}> createArray(long count, SegmentAllocator alloc) {
-               return HandleArray.createArray(count, alloc, {rename}::create);
+       public static HandleArray<{rename}> createArray(SegmentAllocator alloc, long count) {
+               return HandleArray.create(count, {rename}::create, alloc);
        }
 
        @Override
-       public MemoryAddress address() {
+       public MemorySegment address() {
                return address;
        }
 
        @Override
-       public MemorySession scope() {
+       public SegmentScope scope() {
                return scope;
        }
 
@@ -355,7 +347,7 @@ code getset {
        public {type} get{rename}AtIndex(long index) {
                // option a: resolve an offset segment and asme as above with set=segment=segment
                // option b: an indexed varhandle
-               MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
+               MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
                return {tojava};
        }
        }}
@@ -366,7 +358,7 @@ code getset {
        }}
        seti set=value=value set=segment=segment {{
        public void set{rename}AtIndex(long index, {type} value) {
-               MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
+               MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
                {setnative};
        }
        }}
@@ -383,7 +375,7 @@ code getset-frame {
        public {type} get{rename}AtIndex(long index) {
                // option a: resolve an offset segment and asme as above with set=segment=segment
                // option b: an indexed varhandle
-               MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
+               MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
                return {tojava};
        }
        }}
@@ -394,7 +386,7 @@ code getset-frame {
        }}
        seti set=value=value set=segment=segment {{
        public void set{rename}AtIndex(Frame frame$, long index, {type} value) {
-               MemorySegment segment = segment().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
+               MemorySegment segment = address().asSlice(index * LAYOUT.byteSize(), LAYOUT.byteSize());
                {setnative};
        }
        }}
index 57af37c..65e8a9e 100644 (file)
@@ -96,7 +96,7 @@ sub scanFields {
 
                $match->{name} = $m->{name};
                $match->{rename} = $m->{rename};
-               $match->{segment} = 'segment()';
+               $match->{segment} = 'address()';
                $match->{scope} = 'scope()';
 
                push @members, { field=>$m, type=>$type, match=>$match };
index 263b632..1b4cced 100644 (file)
@@ -14,7 +14,7 @@ sub fieldScope {
        } elsif ($m->{scope} eq 'instance') {
                'scope()';
        } else {
-               'MemorySession.global()';
+               'SegmentScope.global()';
        }
 }
 sub fieldScopeAction {
@@ -56,7 +56,7 @@ sub new {
        $info->{rename} = $c->{rename};
 
        my @list =  map { apply('{type} {name}', $_) } grep { $_->{field}->{output} } @members;
-       push @list, 'MemorySession scope$' if ($c->{scope} =~ m/explicit/);
+       push @list, 'SegmentScope scope$' if ($c->{scope} =~ m/explicit/);
        $info->{'java-arguments'} = join ', ', @list;
        $info->{'native-arguments'} = join ', ', map { apply('{carrier} {name}', $_) } @members;
 
@@ -67,7 +67,7 @@ sub new {
                my $m = $_->{field};
 
                if ($m->{instance}) {
-                       "(java.lang.foreign.Addressable)address()";
+                       "address()";
                } elsif ($m->{implied}) {
                        $m->{implied};
                } else {
@@ -92,7 +92,7 @@ sub new {
        }
 
        # for java upcalls, they have an explicit scope always
-       $info->{'java-call'} = join ', ', map { code::formatTemplate('{tojava}', { %{$_->{match}}, value=>'{name}', scope=>'upcallScope$' }) } grep { $_->{field}->{output} } @members;
+       $info->{'java-call'} = join ', ', map { code::formatTemplate('{tojava}', { %{$_->{match}}, value=>'{name}', scope=>'upcallScope$.scope()' }) } grep { $_->{field}->{output} } @members;
 
        $info->{'java-signature'} = code::formatFunctionSignature($api, [$result, @members]);
        $info->{'function-descriptor'} = code::formatFunctionDescriptor($api, [$result, @members]);
@@ -107,7 +107,7 @@ sub new {
        } @members;
 
        $info->{'native-output-define'} = join "\n\t\t",        map { apply('{carrieri} {name};', $_)                                                           } @output;
-       $info->{'native-output-init'} = join ";\n\t\t\t",       map { apply('{type} {name}$h = {type}.createArray(1, frame$);', $_)     } @output;
+       $info->{'native-output-init'} = join ";\n\t\t\t",       map { apply('{type} {name}$h = {type}.create(1, frame$);', $_)  } @output;
        $info->{'native-output-copy'} = join ";\n\t\t\t",       map { apply('{name} = {name}$h.get(0);', $_)                                            } @output;
 
        # also required for some tonative types?
index 351e045..26728d3 100644 (file)
@@ -50,10 +50,10 @@ type /^\[(?<length>\d+)u64:(?<ctype>[uif]\d+)\]$/
 
        type            {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
        layout          {{ 'MemoryLayout.sequenceLayout({length}, Memory.POINTER)' }}
-       tojava          {{ !$m->{array} ? 'PointerArray.createArray({value}, {length}, {scope})' : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})' }}
+       tojava          {{ !$m->{array} ? 'PointerArray.create({value}, {length}, {scope})' : 'HandleArray.create({value}, {length}, {typei}::create, {scope})' }}
 
        getsegment      {{ '(MemorySegment){name}$SH.invokeExact({segment})' }}
-       getnative       {{ !$m->{array} ? 'PointerArray.create({getsegment})' : 'HandleArray.create({getsegment}, (a, s) -> {typei}.createArray(a, Long.MAX_VALUE, s))' }}
+       getnative       {{ !$m->{array} ? 'PointerArray.create({getsegment})' : 'HandleArray.create({getsegment}, (a, s) -> {typei}.create(a, Long.MAX_VALUE, s))' }}
        setnative;
        varhandle       {{
                'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n".
@@ -61,8 +61,8 @@ type /^\[(?<length>\d+)u64:(?<ctype>[uif]\d+)\]$/
        }}
 
        typei           {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }}
-       getnativei      {{ '{typei}.createArray((MemoryAddress){name}$EH.get({segment}, {index}), Long.MAX_VALUE, {scope})' }}
-       setnativei      {{ '{name}$EH.set({segment}, {index}, (Addressable)Memory.address({value}))' }}
+       getnativei      {{ '{typei}.create((MemorySegment){name}$EH.get({segment}, {index}), Long.MAX_VALUE, {scope})' }}
+       setnativei      {{ '{name}$EH.set({segment}, {index}, Memory.address({value}))' }}
 
 }
 
@@ -92,8 +92,8 @@ type /^\[(?<length>\d+)(?<ctype>[uif]\d+)\]$/
                copy=<pointer> template=code:getbyvalue,code:getsetelement {
        layout          {{ "MemoryLayout.sequenceLayout({length}, Memory.".uc($typeSizes{$match->{ctype}}).")" }}
        type            {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
-       tojava          {{ "{type}.createArray({value}, $match->{length}, {scope})" }}
-       tonative        {{ "(Addressable)Memory.address({value})" }}
+       tojava          {{ "{type}.create({value}, $match->{length}, {scope})" }}
+       tonative        {{ "Memory.address({value})" }}
        lea                     {{ "(MemorySegment)$m->{name}\$SH.invokeExact({segment})" }}
        getnative       {{ "{type}.create({lea})" }}
        setnative;
@@ -113,7 +113,7 @@ type /^\[(?<length0>\d+)\[(?<length1>\d+)(?<ctype>[uif]\d+)\]\]$/
        layout     {{ "MemoryLayout.sequenceLayout({length0}, MemoryLayout.sequenceLayout({length1}, Memory.".uc($typeSizes{$match->{ctype}})."))" }}
        type       {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
        tojava     {{ "{type}.create({value})" }}
-       tonative   {{ "(Addressable)Memory.address({value})" }}
+       tonative   {{ "Memory.address({value})" }}
 
        getnative  {{ "{type}.create((MemorySegment)$m->{name}\$SH.invokeExact({segment}))" }}
        setnative;
@@ -134,7 +134,7 @@ type /^u64:/ select=raw copy=<pointer> {
 
 # pointer is an 'in' only pointer to pointer
 type /^u64:u64:/ select=raw-in copy=<pointer> {
-       tonative        {{ '(Addressable)frame$.allocate(Memory.POINTER, {value})' }}
+       tonative        {{ 'frame$.allocate(Memory.POINTER, {value})' }}
 }
 
 # function pointer
@@ -149,8 +149,8 @@ type /^u64:u64:(?<ctype>[uif]\d+)$/ copy=<pointer> {
        type    {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
        tojava  {{
                !$m->{array}
-               ? 'PointerArray.createArray({value}, {length}, {scope})'
-               : 'HandleArray.createArray({value}, {length}, (a, s) -> {typei}.createArray(a, {length}, s), {scope})'
+               ? 'PointerArray.create({value}, {length}, {scope})'
+               : 'HandleArray.create({value}, {length}, (a, s) -> {typei}.create(a, {length}, s), {scope})'
        }}
        typei   {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
 }
@@ -161,11 +161,11 @@ type /^u64:u64:\$\{(\w+)\}$/ copy=<pointer> {
        type            {{ !$m->{array} ? 'PointerArray' : 'HandleArray<{typei}>' }}
        tojava          {{
                !$m->{array}
-               ? 'PointerArray.createArray({value}, {length}, {scope})'
-               : 'HandleArray.createArray({value}, {length}, {typei}::create, {scope})'
+               ? 'PointerArray.create({value}, {length}, {scope})'
+               : 'HandleArray.create({value}, {length}, {typei}::create, {scope})'
        }}
        # tojavai ... ?
-       carrieri        {{ "MemoryAddress" }}
+       carrieri        {{ "MemorySegment" }}
        typei           {{ "$data->{$m->{type}}->{rename}" }}
        tojavai         {{ "{typei}.create({value}, {scope})" }}
 }
@@ -176,8 +176,8 @@ type /^u64:u64:v$/ copy=<pointer> {
        type            {{ !$m->{array} ? 'PointerArray' : 'HandleArray<PointerArray>' }}
        tojava          {{
                !$m->{array}
-               ? 'PointerArray.createArray({value}, {length}, {scope})'
-               : 'HandleArray.createArray({value}, {length}, PointerArray::create, {scope})'
+               ? 'PointerArray.create({value}, {length}, {scope})'
+               : 'HandleArray.create({value}, {length}, PointerArray::create, {scope})'
        }}
 }
 
@@ -185,7 +185,7 @@ type /^u64:u64:v$/ copy=<pointer> {
 # TODO: length, multiple flags?
 type /^u64:(?<ctype>[ui]8)$/ select=array copy=<pointer> {
        type            {{ ucfirst($typeSizes{$match->{ctype}}).'Array' }}
-       tojava          {{ '{type}.createArray({value}, Long.MAX_VALUE, {scope})' }}
+       tojava          {{ '{type}.create({value}, Long.MAX_VALUE, {scope})' }}
        carrieri        {{ "{typei}" }}
        typei           {{ $typeSizes{$match->{ctype}} }}
        tojavai         {{ "({typei}){value}" }}
@@ -203,10 +203,10 @@ type /^u64:(?<ctype>[ui]8)$/ select=segment copy=<pointer> {
 # *i8 with no flag = String
 type /^u64:(?<ctype>i8)$/ copy=<pointer> {
        type            {{ 'String' }}
-       tonative        {{ '(Addressable)({value} != null ? frame$.copy({value}) : MemoryAddress.NULL)' }}
+       tonative        {{ '(MemorySegment)({value} != null ? frame$.copy({value}) : MemorySegment.NULL)' }}
 
        setnative       {{ '{name}$VH.set({segment}, {copynative})' }}
-       copynative      {{ 'segment.session().allocateUtf8String({value})' }}
+       copynative      {{ 'SegmentAllocator.nativeAllocator(segment.scope()).allocateUtf8String({value})' }}
 
        tojava          {{ '({value}).getUtf8String(0L)' }}
 }
@@ -214,8 +214,8 @@ type /^u64:(?<ctype>i8)$/ copy=<pointer> {
 # *Primitive fallback
 type /^u64:(?<ctype>[uif]\d+)$/ copy=<pointer> {
        type            {{ ucfirst($typeSizes{$match->{ctype}})."Array" }}
-       tonative        {{ '(Addressable)Memory.address({value})' }}
-       tojava          {{ ucfirst($typeSizes{$match->{ctype}})."Array.createArray({value}, Long.MAX_VALUE, {scope})" }}
+       tonative        {{ 'Memory.address({value})' }}
+       tojava          {{ ucfirst($typeSizes{$match->{ctype}})."Array.create({value}, Long.MAX_VALUE, {scope})" }}
        carrieri        {{ "{typei}" }}
        typei           {{ $typeSizes{$match->{ctype}} }}
        tojavai         {{ "({typei}){value}" }}
@@ -226,7 +226,7 @@ type /^u64:(?<ctype>[uif]\d+)$/ copy=<pointer> {
 type /^u64:\$\{(\w+)\}$/ select=array-size copy=<pointer> {
        type            {{ "$data->{$m->{type}}->{rename}" }}
        length          {{ 'get'.($m->{'array-size'}->{rename}).'()' }}
-       tojava          {{ "$data->{$m->{type}}->{rename}.createArray({value}, {length}, {scope})" }}
+       tojava          {{ "$data->{$m->{type}}->{rename}.create({value}, {length}, {scope})" }}
 }
 
 # *type  struct?  handle?
@@ -261,7 +261,7 @@ type /^\$\{(\w+)\}$/ byvalue template=code:getbyvalue {
        carrier         {{ 'MemorySegment' }}
        type            {{ "$data->{$m->{type}}->{rename}" }}
        tojava          {{ '{type}.create({value})' }}
-       tonative        {{ '({value}).segment()' }}
+       tonative        {{ '({value}).address()' }}
        getnative       {{ '{type}.create((MemorySegment){name}$SH.invokeExact({segment}))' }}
        setnative;
        varhandle       {{ 'final static MethodHandle {name}$SH = LAYOUT.sliceHandle(MemoryLayout.PathElement.groupElement("{name}"));'."\n" }}
@@ -277,7 +277,7 @@ type <common> template=code:getset {
 
 type <pointer> copy=<common> {
        layout          {{ 'Memory.POINTER' }}
-       carrier         {{ 'MemoryAddress' }}
-       type            {{ 'MemoryAddress' }}
-       tonative        {{ '(Addressable)Memory.address({value})' }}
+       carrier         {{ 'MemorySegment' }}
+       type            {{ 'MemorySegment' }}
+       tonative        {{ 'Memory.address({value})' }}
 }
index 48d90af..14cbfb9 100644 (file)
@@ -1,8 +1,9 @@
 
 notzed.nativez_NATIVE_LIBRARIES = export
 
-notzed.nativez_COMMANDS=export-api export-defines generate-api
-notzed.nativez_LIBRARIES=api.pm        code.api code.pm config.pm method.pm tokenise.pm types.api
+notzed.nativez_COMMANDS=export-api export-defines generate-api generate-api2
+notzed.nativez_LIBRARIES=api.pm        code.api code.pm config.pm method.pm tokenise.pm types.api \
+       Format.pm API.pm
 
 # JMOD target
 $(notzed.nativez_libdir)/%: src/notzed.nativez/lib/%