From: Not Zed Date: Mon, 28 Jul 2025 00:48:25 +0000 (+0930) Subject: Minor changes for dead-end code X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=c8c6d9ccc7569b00b83788dfbb4fddaf85db4f95;p=nativez Minor changes for dead-end code --- diff --git a/config.make.in b/config.make.in index 32a80d7..e598104 100644 --- a/config.make.in +++ b/config.make.in @@ -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 = \ diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 9cb8da4..5356780 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -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 diff --git a/nbproject/project.properties b/nbproject/project.properties index e5b298e..796d4da 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -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} diff --git a/src/notzed.nativez/bin/export-api b/src/notzed.nativez/bin/export-api index ad662a1..75309ac 100755 --- a/src/notzed.nativez/bin/export-api +++ b/src/notzed.nativez/bin/export-api @@ -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", diff --git a/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java b/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java index 2b86b8d..1a92657 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/ByteArray.java @@ -32,33 +32,29 @@ public class ByteArray extends AbstractList 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 implements Pointer { } public String toUtf8String() { - return segment.getUtf8String(0L); + return segment.getString(0L); } } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java b/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java index 72ed513..c1a84d5 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/DoubleArray.java @@ -32,20 +32,20 @@ public class DoubleArray extends AbstractList 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 implements Pointer { } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java b/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java index e49b984..70d5175 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/FloatArray.java @@ -32,20 +32,20 @@ public class FloatArray extends AbstractList 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 implements Pointer { } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Frame.java b/src/notzed.nativez/classes/au/notzed/nativez/Frame.java index 4480287..d816e70 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Frame.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Frame.java @@ -17,33 +17,27 @@ package au.notzed.nativez; import java.lang.foreign.*; -import static java.lang.foreign.ValueLayout.OfAddress; /** - * This is a per-thread stack-based allocator. - *
- * try (Frame f = Memory.createFrame()) {
- *		MemorySegment a = f.allocate(size);
- * }
- * 
- * Any memory allocated is freed when the frame is closed. + * Local allocator. *

- * 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 extends AbstractList implements Pointer { public final MemorySegment segment; - final SegmentScope scope; - BiFunction create; + final Arena scope; + BiFunction create; - private HandleArray(MemorySegment segment, BiFunction create, SegmentScope scope) { + private HandleArray(MemorySegment segment, BiFunction create, Arena scope) { this.segment = segment; this.create = create; this.scope = scope; } - public static HandleArray create(MemorySegment segment, BiFunction create) { - return new HandleArray<>(segment, create, segment.scope()); + public static HandleArray create(MemorySegment segment, BiFunction create) { + return new HandleArray<>(segment, create, null); } - public static HandleArray create(MemorySegment segment, BiFunction create, SegmentScope scope) { + public static HandleArray create(MemorySegment segment, BiFunction create, Arena scope) { return new HandleArray<>(segment, create, scope); } - public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc) { - return create(alloc.allocateArray(Memory.POINTER, size), create); + public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc) { + return create(alloc.allocate(Memory.POINTER, size), create); } - public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc, SegmentScope scope) { - return create(alloc.allocateArray(Memory.POINTER, size), create, scope); + public static HandleArray create(long size, BiFunction create, SegmentAllocator alloc, Arena scope) { + return create(alloc.allocate(Memory.POINTER, size), create, scope); } - public static HandleArray create(MemorySegment address, long size, BiFunction create, SegmentScope scope) { - return create(MemorySegment.ofAddress(address.address(), size * Memory.POINTER.byteSize(), scope), create); + public static HandleArray create(MemorySegment address, long size, BiFunction create, Arena scope) { + return create(MemorySegment.ofAddress(address.address()).reinterpret(size * Memory.POINTER.byteSize(), scope, null), create); } - public static HandleArray create(BiFunction create, SegmentAllocator alloc, T... values) { - HandleArray array = create(alloc.allocateArray(Memory.POINTER, values.length), create); + public static HandleArray create(BiFunction create, SegmentAllocator alloc, T... values) { + HandleArray array = create(alloc.allocate(Memory.POINTER, values.length), create); for (int i=0;i extends AbstractList implements P } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java b/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java index 1451c0d..b936770 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/IntArray.java @@ -32,20 +32,20 @@ public class IntArray extends AbstractList 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 implements Pointer { } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java b/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java index 0f2b6a0..f8d909e 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/LongArray.java @@ -14,7 +14,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package au.notzed.nativez; import java.lang.foreign.*; @@ -32,16 +31,20 @@ public class LongArray extends AbstractList 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 implements Pointer { } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Memory.java b/src/notzed.nativez/classes/au/notzed/nativez/Memory.java index 908df69..a319f6f 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Memory.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Memory.java @@ -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 MemorySegment address(FunctionPointer 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; } } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Native.java b/src/notzed.nativez/classes/au/notzed/nativez/Native.java index 421d427..a9d4420 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Native.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Native.java @@ -59,7 +59,7 @@ public class Native implements Pointer { } @Override - public SegmentScope scope() { + public MemorySegment.Scope scope() { return p.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java b/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java index c9b8503..eaa0f30 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/Pointer.java @@ -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(); } } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java b/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java index b706e66..396e2d5 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/PointerArray.java @@ -14,7 +14,6 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package au.notzed.nativez; import java.lang.foreign.*; @@ -32,17 +31,21 @@ public class PointerArray extends AbstractList 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 implements Pointer } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java b/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java index dc751d4..a4746d7 100644 --- a/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java +++ b/src/notzed.nativez/classes/au/notzed/nativez/ShortArray.java @@ -32,20 +32,22 @@ public class ShortArray extends AbstractList 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 implements Pointer { } @Override - public final SegmentScope scope() { + public final MemorySegment.Scope scope() { return segment.scope(); } diff --git a/src/notzed.nativez/lib/code.api b/src/notzed.nativez/lib/code.api index 6741734..947d2cd 100644 --- a/src/notzed.nativez/lib/code.api +++ b/src/notzed.nativez/lib/code.api @@ -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 resolve, SegmentScope scope) { + {name}(Function resolve, Arena scope) { {init} } - public static {name} create(Function resolve, SegmentScope scope) { + public static {name} create(Function 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; } diff --git a/src/notzed.nativez/lib/code.pm b/src/notzed.nativez/lib/code.pm index 65e8a9e..592d8af 100644 --- a/src/notzed.nativez/lib/code.pm +++ b/src/notzed.nativez/lib/code.pm @@ -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; } diff --git a/src/notzed.nativez/lib/types.api b/src/notzed.nativez/lib/types.api index 26728d3..18312bc 100644 --- a/src/notzed.nativez/lib/types.api +++ b/src/notzed.nativez/lib/types.api @@ -226,7 +226,7 @@ type /^u64:(?[uif]\d+)$/ copy= { type /^u64:\$\{(\w+)\}$/ select=array-size copy= { 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?