"-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
);
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;
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
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;
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));
}
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
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;
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));
}
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
*/
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
@Override
public void close() {
- scope.close();
+ alloc.close();
}
public MemorySegment allocateInt() {
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]);
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;
}
}
+ /*
public static MemorySegment copy(MemorySegment ctx, String string) {
if (string != null) {
SegmentAllocator alloc = ctx.session();
} else {
return Memory.NULL;
}
- }
+ }*/
}
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
*/
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]);
}
@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
}
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);
}
}
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 {
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));
}
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
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;
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));
}
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
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;
}
}
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()))
}
}
- 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()))
}
}
- 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);
}
}
- 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) {
}
}
- 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 {
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 {
*/
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;
* <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
*/
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;
}
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();
}
/* ********************************************************************** */
*/
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);
}
/*
- 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)
}
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.
}
{
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()));
}
}
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();
}
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();
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);
}
/**
* 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;
}
}
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);
size += 1;
}
- public CHandle get(MemoryAddress p) {
+ public CHandle get(MemorySegment p) {
int i = CHandle.hashCode(p) & mask;
CHandle h = table[i];
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;
* 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;
- //}
}
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) {
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
}
@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;
}
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);
}
}
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;
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));
}
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
# 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$,
}}
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}
}
{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}
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) {
}
@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}
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) {
}
@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
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;
}
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};
}
}}
}}
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};
}
}}
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};
}
}}
}}
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};
}
}}
$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 };
} elsif ($m->{scope} eq 'instance') {
'scope()';
} else {
- 'MemorySession.global()';
+ 'SegmentScope.global()';
}
}
sub fieldScopeAction {
$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;
my $m = $_->{field};
if ($m->{instance}) {
- "(java.lang.foreign.Addressable)address()";
+ "address()";
} elsif ($m->{implied}) {
$m->{implied};
} else {
}
# 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]);
} @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?
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".
}}
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}))' }}
}
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;
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;
# 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
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" }}
}
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})" }}
}
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})'
}}
}
# 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}" }}
# *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)' }}
}
# *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}" }}
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?
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" }}
type <pointer> copy=<common> {
layout {{ 'Memory.POINTER' }}
- carrier {{ 'MemoryAddress' }}
- type {{ 'MemoryAddress' }}
- tonative {{ '(Addressable)Memory.address({value})' }}
+ carrier {{ 'MemorySegment' }}
+ type {{ 'MemorySegment' }}
+ tonative {{ 'Memory.address({value})' }}
}
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/%