Minor changes for dead-end code
authorNot Zed <notzed@gmail.com>
Mon, 28 Jul 2025 00:48:25 +0000 (10:18 +0930)
committerNot Zed <notzed@gmail.com>
Mon, 28 Jul 2025 00:48:25 +0000 (10:18 +0930)
19 files changed:
config.make.in
nbproject/genfiles.properties
nbproject/project.properties
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/types.api

index 32a80d7..e598104 100644 (file)
@@ -8,7 +8,7 @@ JAR ?= $(JAVA_HOME)/bin/jar
 JMOD ?= $(JAVA_HOME)/bin/jmod
 GCCPLUGINDIR:=$(shell gcc -print-file-name=plugin)
 
-JAVACFLAGS += --enable-preview --source 20
+JAVACFLAGS += 
 
 # Linux options
 linux-amd64_CPPFLAGS = \
index 9cb8da4..5356780 100644 (file)
@@ -1,8 +1,8 @@
 build.xml.data.CRC32=d287ed4e
 build.xml.script.CRC32=b55362bc
-build.xml.stylesheet.CRC32=32069288@1.6.1
+build.xml.stylesheet.CRC32=32069288@1.30
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
-nbproject/build-impl.xml.data.CRC32=d287ed4e
-nbproject/build-impl.xml.script.CRC32=727ac37b
-nbproject/build-impl.xml.stylesheet.CRC32=d1ebcf0f@1.14
+nbproject/build-impl.xml.data.CRC32=508a6882
+nbproject/build-impl.xml.script.CRC32=cd15f7cb
+nbproject/build-impl.xml.stylesheet.CRC32=d1ebcf0f@1.30
index e5b298e..796d4da 100644 (file)
@@ -1,4 +1,4 @@
-annotation.processing.enabled=true
+annotation.processing.enabled=false
 annotation.processing.enabled.in.editor=false
 annotation.processing.processors.list=
 annotation.processing.run.all.processors=true
@@ -40,15 +40,15 @@ includes=**
 jar.compress=false
 javac.classpath=
 # Space-separated list of extra javac options
-javac.compilerargs=-Xlint:unchecked --enable-preview
-javac.deprecation=false
+javac.compilerargs=-Xlint:unchecked
+javac.deprecation=true
 javac.external.vm=false
 javac.modulepath=
 javac.processormodulepath=
 javac.processorpath=\
     ${javac.classpath}
-javac.source=19
-javac.target=19
+javac.source=24
+javac.target=24
 javac.test.classpath=\
     ${javac.classpath}
 javac.test.modulepath=\
@@ -74,13 +74,14 @@ jlink.additionalmodules=
 jlink.additionalparam=
 jlink.launcher=true
 jlink.launcher.name=notzed.nativez
+main.class=au.notzed.nativez.tools.Generator
 platform.active=default_platform
 project.license=gpl3-notzed
 run.classpath=
 # Space-separated list of JVM arguments used when running the project.
 # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
 # To set system properties for unit tests define test-sys-prop.name=value:
-run.jvmargs=-Djava.library.path=bin/notzed.nativez/linux-amd64/lib
+run.jvmargs=-ea -Djava.library.path=bin/notzed.nativez/linux-amd64/lib --enable-native-access=notzed.nativez
 run.modulepath=\
     ${javac.modulepath}:\
     ${build.modules.dir}
index ad662a1..75309ac 100755 (executable)
@@ -119,6 +119,7 @@ my @cmd = (
        'gcc',
        "-fplugin=$FindBin::Bin/../lib/libexport.so",
        "-fplugin-arg-libexport-output=$var->{workdir}/$apibase.pm",
+#      "-fplugin-arg-libexport-debug=5",
 #      "-fplugin-arg-libexport-verbose=$var->{verbose}",
        '-O0',
        '-o', "$var->{workdir}/$apibase.pch",
index 2b86b8d..1a92657 100644 (file)
@@ -32,33 +32,29 @@ public class ByteArray extends AbstractList<Byte> implements Pointer {
                return new ByteArray(segment);
        }
 
-       public static ByteArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length, scope));
+       public static ByteArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.BYTE.byteSize(), scope, null));
        }
 
-       public static ByteArray create(MemorySegment address, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
+       public static ByteArray create(MemorySegment address, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(Long.MAX_VALUE, scope, null));
        }
 
        public static ByteArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.BYTE, length));
+               return create(alloc.allocate(Memory.BYTE, length));
        }
 
        public static ByteArray create(SegmentAllocator alloc, String value) {
-               return create(alloc.allocateUtf8String(value));
+               return create(alloc.allocateFrom(value));
        }
 
-       //public static ByteArray create(SegmentScope scope, String value) {
-       //      return create(scope.allocateUtf8String(value));
-       //}
-
        @Override
        public final MemorySegment address() {
                return segment;
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
@@ -92,6 +88,6 @@ public class ByteArray extends AbstractList<Byte> implements Pointer {
        }
 
        public String toUtf8String() {
-               return segment.getUtf8String(0L);
+               return segment.getString(0L);
        }
 }
index 72ed513..c1a84d5 100644 (file)
@@ -32,20 +32,20 @@ public class DoubleArray extends AbstractList<Double> implements Pointer {
                return new DoubleArray(segment);
        }
 
-       public static DoubleArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length * Memory.DOUBLE.byteSize(), scope));
+       public static DoubleArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.DOUBLE.byteSize(), scope, null));
        }
 
-       public static DoubleArray create(MemorySegment address, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
+       public static DoubleArray create(MemorySegment address, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(Long.MAX_VALUE, scope, null));
        }
 
        public static DoubleArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.DOUBLE, length));
+               return create(alloc.allocate(Memory.DOUBLE, length));
        }
 
        public static DoubleArray create(SegmentAllocator alloc, double... values) {
-               return create(alloc.allocateArray(Memory.DOUBLE, values));
+               return create(alloc.allocateFrom(Memory.DOUBLE, values));
        }
 
        @Override
@@ -54,7 +54,7 @@ public class DoubleArray extends AbstractList<Double> implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index e49b984..70d5175 100644 (file)
@@ -32,20 +32,20 @@ public class FloatArray extends AbstractList<Float> implements Pointer {
                return new FloatArray(segment);
        }
 
-       public static FloatArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length * Memory.FLOAT.byteSize(), scope));
+       public static FloatArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.FLOAT.byteSize(), scope, null));
        }
 
-       public static FloatArray create(MemorySegment address, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
+       public static FloatArray create(MemorySegment address, Arena scope) {
+               return create(address, Long.MAX_VALUE, scope);
        }
 
        public static FloatArray create(SegmentAllocator alloc, int length) {
-               return create(alloc.allocateArray(Memory.FLOAT, length));
+               return create(alloc.allocate(Memory.FLOAT, length));
        }
 
        public static FloatArray create(SegmentAllocator alloc, float... values) {
-               return create(alloc.allocateArray(Memory.FLOAT, values));
+               return create(alloc.allocateFrom(Memory.FLOAT, values));
        }
 
        @Override
@@ -54,7 +54,7 @@ public class FloatArray extends AbstractList<Float> implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index 4480287..d816e70 100644 (file)
 package au.notzed.nativez;
 
 import java.lang.foreign.*;
-import static java.lang.foreign.ValueLayout.OfAddress;
 
 /**
- * This is a per-thread stack-based allocator.
- * <pre>
- * try (Frame f = Memory.createFrame()) {
- *             MemorySegment a = f.allocate(size);
- * }
- * </pre>
- * Any memory allocated is freed when the frame is closed.
+ * Local allocator.
  * <p>
- * This is quite a bit faster than using an arena allocator ... but it also
- * removes most scope guarantees.
+ * This was a helper for a pre-release version of java.lang.foreign.
+ * Just use an Arena.
  */
+@Deprecated
 public class Frame implements AutoCloseable, SegmentAllocator {
 
        private final Arena alloc;
 
        Frame() {
-               this.alloc = Arena.openConfined();
+               this.alloc = Arena.ofConfined();
        }
 
        public static Frame frame() {
                return new Frame();
        }
 
-       public SegmentScope scope() {
+       public MemorySegment.Scope scope() {
                return alloc.scope();
        }
 
@@ -70,7 +64,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
        }
 
        public MemorySegment allocateLong(int count) {
-               return allocateArray(Memory.LONG, count);
+               return allocate(Memory.LONG, count);
        }
 
        public MemorySegment allocatePointer() {
@@ -78,58 +72,69 @@ public class Frame implements AutoCloseable, SegmentAllocator {
        }
 
        public MemorySegment allocatePointer(int count) {
-               return allocateArray(Memory.POINTER, count);
+               return allocate(Memory.POINTER, count);
        }
 
-       public MemorySegment allocateArray(OfAddress type, MemorySegment[] value) {
-               MemorySegment m = allocateArray(type, value.length);
-               for (int i=0;i<value.length;i++)
+       public MemorySegment allocateArray(AddressLayout type, MemorySegment[] value) {
+               MemorySegment m = allocate(type, value.length);
+               for (int i = 0; i < value.length; i++)
                        m.setAtIndex(type, i, value[i]);
                return m;
        }
 
+       @Deprecated
        public MemorySegment copy(byte value) {
                return allocate(Memory.BYTE, value);
        }
 
+       @Deprecated
        public MemorySegment copy(short value) {
                return allocate(Memory.SHORT, value);
        }
 
+       @Deprecated
        public MemorySegment copy(int value) {
                return allocate(Memory.INT, value);
        }
 
+       @Deprecated
        public MemorySegment copy(long value) {
                return allocate(Memory.LONG, value);
        }
 
+       @Deprecated
        public MemorySegment copy(float value) {
-               return allocate(Memory.FLOAT, value);
+               return allocateFrom(Memory.FLOAT, value);
        }
 
+       @Deprecated
        public MemorySegment copy(double value) {
-               return allocate(Memory.DOUBLE, value);
+               return allocateFrom(Memory.DOUBLE, value);
        }
 
+       @Deprecated
        public MemorySegment copy(byte[] value) {
-               return allocateArray(Memory.BYTE, value);
+               return allocateFrom(Memory.BYTE, value);
        }
 
+       @Deprecated
        public MemorySegment copy(int[] value) {
-               return allocateArray(Memory.INT, value);
+               return allocateFrom(Memory.INT, value);
        }
 
+       @Deprecated
        public MemorySegment copy(long[] value) {
-               return allocateArray(Memory.LONG, value);
+               return allocateFrom(Memory.LONG, value);
        }
 
+       @Deprecated
        public MemorySegment copy(float[] value) {
-               return allocateArray(Memory.FLOAT, value);
+               return allocateFrom(Memory.FLOAT, value);
        }
 
+       @Deprecated
        public MemorySegment copy(String value) {
-               return allocateUtf8String(value);
+               return allocateFrom(value);
        }
 
        /*
@@ -159,7 +164,7 @@ public class Frame implements AutoCloseable, SegmentAllocator {
                        }
                        return list;
                } else {
-                       return Memory.NULL;
+                       return MemorySegment.NULL;
                }
        }
 
index ecd9a1c..86d2e5b 100644 (file)
@@ -30,37 +30,37 @@ import java.util.function.BiFunction;
  */
 public class HandleArray<T extends Pointer> extends AbstractList<T> implements Pointer {
        public final MemorySegment segment;
-       final SegmentScope scope;
-       BiFunction<MemorySegment,SegmentScope,T> create;
+       final Arena scope;
+       BiFunction<MemorySegment,Arena,T> create;
 
-       private HandleArray(MemorySegment segment, BiFunction<MemorySegment,SegmentScope,T> create, SegmentScope scope) {
+       private HandleArray(MemorySegment segment, BiFunction<MemorySegment,Arena,T> create, Arena scope) {
                this.segment = segment;
                this.create = create;
                this.scope = scope;
        }
 
-       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<MemorySegment,Arena,T> create) {
+               return new HandleArray<>(segment, create, null);
        }
 
-       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemorySegment,SegmentScope,T> create, SegmentScope scope) {
+       public static <T extends Pointer> HandleArray<T> create(MemorySegment segment, BiFunction<MemorySegment,Arena,T> create, Arena scope) {
                return new HandleArray<>(segment, create, scope);
        }
 
-       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> create(long size, BiFunction<MemorySegment,Arena,T> create, SegmentAllocator alloc) {
+               return create(alloc.allocate(Memory.POINTER, size), create);
        }
 
-       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> create(long size, BiFunction<MemorySegment,Arena,T> create, SegmentAllocator alloc, Arena scope) {
+               return create(alloc.allocate(Memory.POINTER, size), create, scope);
        }
 
-       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(MemorySegment address, long size, BiFunction<MemorySegment,Arena,T> create, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(size * Memory.POINTER.byteSize(), scope, null), create);
        }
 
-       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);
+       public static <T extends Pointer> HandleArray<T> create(BiFunction<MemorySegment,Arena,T> create, SegmentAllocator alloc, T... values) {
+               HandleArray<T> array = create(alloc.allocate(Memory.POINTER, values.length), create);
                for (int i=0;i<values.length;i++)
                        array.setAtIndex(i, values[i]);
                return array;
@@ -80,7 +80,7 @@ public class HandleArray<T extends Pointer> extends AbstractList<T> implements P
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index 1451c0d..b936770 100644 (file)
@@ -32,20 +32,20 @@ public class IntArray extends AbstractList<Integer> implements Pointer {
                return new IntArray(segment);
        }
 
-       public static IntArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length * Memory.INT.byteSize(), scope));
+       public static IntArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.INT.byteSize(), scope, null));
        }
 
-       public static IntArray create(MemorySegment address, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
+       public static IntArray create(MemorySegment address, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(Long.MAX_VALUE, scope, null));
        }
 
        public static IntArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.INT, length));
+               return create(alloc.allocate(Memory.INT, length));
        }
 
        public static IntArray create(SegmentAllocator alloc, int... values) {
-               return create(alloc.allocateArray(Memory.INT, values));
+               return create(alloc.allocateFrom(Memory.INT, values));
        }
 
        public IntArray asSlice(long offset) {
@@ -62,7 +62,7 @@ public class IntArray extends AbstractList<Integer> implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index 0f2b6a0..f8d909e 100644 (file)
@@ -14,7 +14,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-
 package au.notzed.nativez;
 
 import java.lang.foreign.*;
@@ -32,16 +31,20 @@ public class LongArray extends AbstractList<Long> implements Pointer {
                return new LongArray(segment);
        }
 
-       public static LongArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length * Memory.LONG.byteSize(), scope));
+       public static LongArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.LONG.byteSize(), scope, null));
+       }
+
+       public static LongArray create(MemorySegment address, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(Long.MAX_VALUE, scope, null));
        }
 
        public static LongArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.LONG, length));
+               return create(alloc.allocate(Memory.LONG, length));
        }
 
        public static LongArray create(SegmentAllocator alloc, long... values) {
-               return create(alloc.allocateArray(Memory.LONG, values));
+               return create(alloc.allocateFrom(Memory.LONG, values));
        }
 
        @Override
@@ -50,7 +53,7 @@ public class LongArray extends AbstractList<Long> implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index 908df69..a319f6f 100644 (file)
@@ -27,19 +27,16 @@ public class Memory {
 
        // probably should be INT8 INT16, etc
        public static final OfByte BYTE = JAVA_BYTE;
-       public static final OfShort SHORT = JAVA_SHORT.withBitAlignment(16);
-       public static final OfInt INT = JAVA_INT.withBitAlignment(32);
-       public static final OfLong LONG = JAVA_LONG.withBitAlignment(64);
-       public static final OfFloat FLOAT = JAVA_FLOAT.withBitAlignment(32);
-       public static final OfDouble DOUBLE = JAVA_DOUBLE.withBitAlignment(64);
-       public static final OfAddress POINTER = ADDRESS.withBitAlignment(64);
-
-       static final SegmentScope sharedScope = SegmentScope.auto(); // cleaner?
-       // Note: currently can't create zero-length segments
-       @Deprecated
-       static final MemorySegment NULL = MemorySegment.NULL;
-
-       public static SegmentScope sharedScope() {
+       public static final OfShort SHORT = JAVA_SHORT.withByteAlignment(2);
+       public static final OfInt INT = JAVA_INT.withByteAlignment(4);
+       public static final OfLong LONG = JAVA_LONG.withByteAlignment(8);
+       public static final OfFloat FLOAT = JAVA_FLOAT.withByteAlignment(4);
+       public static final OfDouble DOUBLE = JAVA_DOUBLE.withByteAlignment(8);
+       public static final AddressLayout POINTER = ADDRESS.withByteAlignment(8);
+
+       static final Arena sharedScope = Arena.ofAuto(); // cleaner?
+
+       public static Arena sharedScope() {
                return sharedScope;
        }
 
@@ -47,6 +44,12 @@ public class Memory {
                return Linker.nativeLinker().downcallHandle(desc);
        }
 
+       public static MethodHandle downcall(SymbolLookup lookup, String name, FunctionDescriptor desc) {
+               return lookup.find(name)
+                       .map(sym -> Linker.nativeLinker().downcallHandle(sym, desc))
+                       .orElse(null);
+       }
+
        public static MethodHandle downcall(String name, FunctionDescriptor desc) {
                return SymbolLookup.loaderLookup().find(name)
                        .map(sym -> Linker.nativeLinker().downcallHandle(sym, desc))
@@ -56,7 +59,7 @@ public class Memory {
        public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc) {
                return Linker.nativeLinker().downcallHandle(sym, desc);
        }
-/*
+       /*
        public static MethodHandle downcall(MemorySegment sym, FunctionDescriptor desc, SegmentScope scope) {
                return sym != MemorySegment.NULL
                        ? Linker.nativeLinker().downcallHandle(MemorySegment.ofAddress(sym, 0, scope), desc)
@@ -69,10 +72,10 @@ public class Memory {
                        ? 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, SegmentScope scope) {
+       public static MemorySegment upcall(Object instance, FunctionDescriptor desc, Arena scope) {
                try {
                        java.lang.reflect.Method m = instance.getClass().getMethods()[0];
                        MethodHandle handle = lookup.findVirtual(instance.getClass(), "call", MethodType.methodType(m.getReturnType(), m.getParameterTypes()))
@@ -83,7 +86,7 @@ public class Memory {
                }
        }
 
-       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, SegmentScope scope) {
+       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String signature, FunctionDescriptor desc, Arena 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()))
@@ -94,7 +97,7 @@ public class Memory {
                }
        }
 
-       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, SegmentScope scope) {
+       public static MemorySegment upcall(MethodHandles.Lookup lookup, Object instance, String method, String signature, FunctionDescriptor desc, Arena scope) {
                try {
                        MethodHandle handle = lookup.findVirtual(instance.getClass(), method, MethodType.fromMethodDescriptorString(signature, Memory.class.getClassLoader()))
                                .bindTo(instance);
@@ -117,7 +120,7 @@ public class Memory {
        public static <T> MemorySegment address(FunctionPointer<T> v) {
                return v.symbol();
        }
-       */
+        */
        public static MemorySegment address(MemorySegment v) {
                return v != null ? v : MemorySegment.NULL;
        }
@@ -154,19 +157,19 @@ public class Memory {
        }
 
        public static MemorySegment slice(MemorySegment segment, MethodHandle sliceHandle) {
-                       try {
-                               return (MemorySegment)sliceHandle.invokeExact(segment);
-                       } catch (Throwable ex) {
-                               throw new RuntimeException(ex);
-                       }
+               try {
+                       return (MemorySegment)sliceHandle.invokeExact(segment);
+               } catch (Throwable ex) {
+                       throw new RuntimeException(ex);
+               }
        }
 
        public static String copyString(MemorySegment string) {
-               return string != MemorySegment.NULL ? string.getUtf8String(0) : null;
+               return string != MemorySegment.NULL ? string.getString(0) : null;
        }
 
        public static MemorySegment copyString(String string, SegmentAllocator alloc) {
-               return (string != null) ? alloc.allocateUtf8String(string) : Memory.NULL;
+               return (string != null) ? alloc.allocateFrom(string) : MemorySegment.NULL;
        }
 
        public static String[] copyStringArray(MemorySegment list, long len) {
@@ -174,7 +177,7 @@ public class Memory {
                        String[] array = new String[(int)len];
                        for (int i = 0; i < array.length; i++) {
                                MemorySegment fu = list.getAtIndex(Memory.POINTER, i);
-                               array[i] = fu != MemorySegment.NULL ? fu.getUtf8String(0) : null;
+                               array[i] = fu != MemorySegment.NULL ? fu.getString(0) : null;
                        }
                        return array;
                } else {
@@ -184,13 +187,13 @@ public class Memory {
 
        public static MemorySegment copyStringArray(String[] array, SegmentAllocator alloc) {
                if (array != null) {
-                       MemorySegment list = alloc.allocateArray(Memory.POINTER, array.length);
+                       MemorySegment list = alloc.allocate(Memory.POINTER, array.length);
                        for (int i = 0; i < array.length; i++) {
-                               list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateUtf8String(array[i]) : MemorySegment.NULL);
+                               list.setAtIndex(Memory.POINTER, i, array[i] != null ? alloc.allocateFrom(array[i]) : MemorySegment.NULL);
                        }
                        return list;
                } else {
-                       return Memory.NULL;
+                       return MemorySegment.NULL;
                }
        }
 
index 421d427..a9d4420 100644 (file)
@@ -59,7 +59,7 @@ public class Native implements Pointer {
        }
 
        @Override
-       public SegmentScope scope() {
+       public MemorySegment.Scope scope() {
                return p.scope();
        }
 
index c9b8503..eaa0f30 100644 (file)
@@ -8,7 +8,7 @@ import java.lang.foreign.*;
  */
 public interface Pointer {
        MemorySegment address();
-       default SegmentScope scope() {
-               return SegmentScope.global();
+       default MemorySegment.Scope scope() {
+               return Arena.global().scope();
        }
 }
index b706e66..396e2d5 100644 (file)
@@ -14,7 +14,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-
 package au.notzed.nativez;
 
 import java.lang.foreign.*;
@@ -32,17 +31,21 @@ public class PointerArray extends AbstractList<MemorySegment> implements Pointer
                return new PointerArray(segment);
        }
 
-       public static PointerArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length, scope));
+       public static PointerArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length, scope, null));
+       }
+
+       public static PointerArray create(MemorySegment address, Arena scope) {
+               return create(address, Long.MAX_VALUE, scope);
        }
 
        public static PointerArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.POINTER, length));
+               return create(alloc.allocate(Memory.POINTER, length));
        }
 
        public static PointerArray create(SegmentAllocator alloc, MemorySegment... values) {
-               MemorySegment m = alloc.allocateArray(Memory.POINTER, values.length);
-               for (int i=0;i<values.length;i++)
+               MemorySegment m = alloc.allocate(Memory.POINTER, values.length);
+               for (int i = 0; i < values.length; i++)
                        m.setAtIndex(Memory.POINTER, i, values[i]);
                return create(m);
        }
@@ -53,7 +56,7 @@ public class PointerArray extends AbstractList<MemorySegment> implements Pointer
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index dc751d4..a4746d7 100644 (file)
@@ -32,20 +32,22 @@ public class ShortArray extends AbstractList<Short> implements Pointer {
                return new ShortArray(segment);
        }
 
-       public static ShortArray create(MemorySegment address, long length, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), length * Memory.SHORT.byteSize(), scope));
+       public static ShortArray create(MemorySegment address, long length, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(length * Memory.SHORT.byteSize(), scope, null));
        }
 
-       public static ShortArray create(MemorySegment address, SegmentScope scope) {
-               return create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope));
+       public static ShortArray create(MemorySegment address, Arena scope) {
+               return create(MemorySegment.ofAddress(address.address()).reinterpret(Long.MAX_VALUE, scope, null));
        }
 
+       @Deprecated
        public static ShortArray create(long length, SegmentAllocator alloc) {
-               return create(alloc.allocateArray(Memory.SHORT, length));
+               return create(alloc.allocate(Memory.SHORT, length));
        }
 
+       @Deprecated
        public static ShortArray create(SegmentAllocator alloc, short... values) {
-               return create(alloc.allocateArray(Memory.SHORT, values));
+               return create(alloc.allocateFrom(Memory.SHORT, values));
        }
 
        @Override
@@ -54,7 +56,7 @@ public class ShortArray extends AbstractList<Short> implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
index 6741734..947d2cd 100644 (file)
@@ -53,7 +53,7 @@ code method {
 
        # callback function/types
        downcall {{
-       public static FunctionPointer<{rename}> downcall(MemorySegment addr$, SegmentScope scope$) {
+       public static FunctionPointer<{rename}> downcall(MemorySegment addr$, Arena scope$) {
                MemorySegment symbol$ = MemorySegment.ofAddress(addr$.address(), 0, scope$);
                MethodHandle {rename}$FH = Memory.downcall(symbol$, descriptor());
                return new FunctionPointer<{rename}>(
@@ -78,7 +78,7 @@ code method {
        }}
 
        upcall {{
-       public static FunctionPointer<{rename}> upcall({rename} target$, SegmentScope scope$) {
+       public static FunctionPointer<{rename}> upcall({rename} target$, Arena scope$) {
                interface Trampoline {
                        {java-result} call({native-arguments});
                }
@@ -132,10 +132,10 @@ import au.notzed.nativez.*;
 {imports}
 
 public class {name} {
-       {name}(Function<String,MemorySegment> resolve, SegmentScope scope) {
+       {name}(Function<String,MemorySegment> resolve, Arena scope) {
 {init}
        }
-       public static {name} create(Function<String,MemorySegment> resolve, SegmentScope scope) {
+       public static {name} create(Function<String,MemorySegment> resolve, Arena scope) {
                return new {name}(resolve, scope);
        }
 {defines}
@@ -167,12 +167,14 @@ public class {rename} implements Pointer {
 {init}
        }
 
+
+
        public static {rename} create(MemorySegment segment) {
                return new {rename}(segment);
        }
 
-       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(MemorySegment address, Arena scope) {
+               return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address()).reinterpret(LAYOUT.byteSize(), scope, null)) : null;
        }
 
        public static {rename} create(SegmentAllocator frame) {
@@ -185,7 +187,7 @@ public class {rename} implements Pointer {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
@@ -217,7 +219,7 @@ public class {rename} implements Pointer, Array<{rename}> {
                return new {rename}(segment);
        }
 
-       public static {rename} create(MemorySegment address, SegmentScope scope) {
+       public static {rename} create(MemorySegment address, Arena scope) {
                return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize(), scope)) : null;
        }
 
@@ -225,11 +227,11 @@ public class {rename} implements Pointer, Array<{rename}> {
                return create(frame.allocate(LAYOUT));
        }
 
-       public static {rename} createArray(MemorySegment address, SegmentScope scope) {
+       public static {rename} createArray(MemorySegment address, Arena scope) {
                return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), Long.MAX_VALUE, scope)) : null;
        }
 
-       public static {rename} createArray(MemorySegment address, long length, SegmentScope scope) {
+       public static {rename} createArray(MemorySegment address, long length, Arena scope) {
                return MemorySegment.NULL != address ? create(MemorySegment.ofAddress(address.address(), LAYOUT.byteSize() * length, scope)) : null;
        }
 
@@ -243,7 +245,7 @@ public class {rename} implements Pointer, Array<{rename}> {
        }
 
        @Override
-       public final SegmentScope scope() {
+       public final MemorySegment.Scope scope() {
                return segment.scope();
        }
 
@@ -284,15 +286,15 @@ import au.notzed.nativez.*;
 public class {rename} implements Pointer {
 
        MemorySegment address;
-       SegmentScope scope;
+       Arena scope;
 
-       private {rename}(MemorySegment address, SegmentScope scope) {
+       private {rename}(MemorySegment address, Arena scope) {
                this.address = address;
                this.scope = scope;
 {init}
        }
 
-       public static {rename} create(MemorySegment address, SegmentScope scope) {
+       public static {rename} create(MemorySegment address, Arena scope) {
                return MemorySegment.NULL != address ? new {rename}(address, scope) : null;
        }
 
@@ -306,7 +308,7 @@ public class {rename} implements Pointer {
        }
 
        @Override
-       public SegmentScope scope() {
+       public Arena scope() {
                return scope;
        }
 
index 65e8a9e..592d8af 100644 (file)
@@ -179,7 +179,7 @@ sub formatStructLayout {
                $layout .= 'MemoryLayout.paddingLayout('.($s->{size} - $lastOffset).')';
        }
 
-       $layout .= "\n\t).withBitAlignment($maxSize);\n";
+       $layout .= "\n\t).withByteAlignment(".($maxSize/8).");\n";
        $layout;
 }
 
index 26728d3..18312bc 100644 (file)
@@ -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}.create({value}, {length}, {scope})" }}
+       tojava          {{ "$data->{$m->{type}}->{rename}.createArray({value}, {length}, {scope})" }}
 }
 
 # *type  struct?  handle?