notzed.zcl.fxdemo_DEMOS=fxdemo.fract.Mandelbrot fxdemo.fract.Test
-DEMOFLAGS=--enable-native-access=notzed.zcl
+DEMOFLAGS=--enable-native-access=notzed.zcl --enable-preview
# module class basename(class)
define java_demo=
+foreign-abi branch note 20220926
+--------------------------------
+
+I've updated it to OpenJDK 19 and it builds and clinfo runs, but since
+I don't have a working OpenCL on my Renior laptop even after a couple
+of years I'm not inclined to bother and i'm focusing on learning
+vulkan compute instead, albeit slowly.
+
foreign-abi branch note
-----------------------
JAVA_HOME ?= /usr/local/jdk
JAVAFX_HOME ?= /usr/local/javafx-sdk
-JAVACFLAGS += --add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
+JAVACFLAGS += --enable-preview --source 19
JAVA ?= $(JAVA_HOME)/bin/java
JAVAC ?= $(JAVA_HOME)/bin/javac
jar.compress=false
javac.classpath=
# Space-separated list of extra javac options
-javac.compilerargs=-Xlint:unchecked --add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
+javac.compilerargs=-Xlint:unchecked --enable-preview
javac.deprecation=false
javac.external.vm=false
javac.modulepath=\
- ${libs.JavaFX_13.classpath}
+ ${libs.JavaFX.classpath}
javac.processormodulepath=
javac.processorpath=\
${javac.classpath}
-javac.source=17
-javac.target=17
+javac.source=19
+javac.target=19
javac.test.classpath=\
${libs.junit_4.classpath}:\
${javac.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=--enable-native-access=notzed.zcl -Djava.library.path=/usr/lib64
+run.jvmargs=--enable-native-access=notzed.zcl -Djava.library.path=/usr/lib64 --enable-preview
run.modulepath=\
${javac.modulepath}:\
${build.modules.dir}
*/
package au.notzed.zcl.test;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.*;
if (true) {
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment arg0 = MemorySegment.allocateNative(8, 8, scope);
MemorySegment arg1 = MemorySegment.allocateNative(8, 8, scope);
MemorySegment arg2 = MemorySegment.allocateNative(8, 8, scope);
if (false) {
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment arg0 = MemorySegment.allocateNative(8, 8, scope);
MemorySegment arg1 = MemorySegment.allocateNative(8, 8, scope);
}
if (false) {
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
- MemorySegment arg0 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
- MemorySegment arg1 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
- MemorySegment arg2 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
- MemorySegment arg3 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
+ try (MemorySession scope = MemorySession.openConfined()) {
+ MemorySegment arg0 = MemorySegment.allocateNative(Memory.POINTER, scope);
+ MemorySegment arg1 = MemorySegment.allocateNative(Memory.POINTER, scope);
+ MemorySegment arg2 = MemorySegment.allocateNative(Memory.POINTER, scope);
+ MemorySegment arg3 = MemorySegment.allocateNative(Memory.POINTER, scope);
}
}
long u = System.nanoTime() - now;
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
- MemorySegment arg0 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
- MemorySegment arg1 = MemorySegment.allocateNative(CLinker.C_POINTER, scope);
+ try (MemorySession scope = MemorySession.openConfined()) {
+ MemorySegment arg0 = MemorySegment.allocateNative(Memory.POINTER, scope);
+ MemorySegment arg1 = MemorySegment.allocateNative(Memory.POINTER, scope);
}
}
long m = System.nanoTime() - now;
*/
package au.notzed.zcl.test;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.*;
public class TestCopies {
- final static VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
- final static SequenceLayout longVLayout = MemoryLayout.sequenceLayout(CLinker.C_LONG);
- final static VarHandle longVHandle = longVLayout.varHandle(long.class, MemoryLayout.PathElement.sequenceElement());
+ final static VarHandle longHandle = Memory.LONG.varHandle();
+ final static SequenceLayout longVLayout = MemoryLayout.sequenceLayout(-1, Memory.LONG);
+ final static VarHandle longVHandle = Memory.LONG.arrayElementVarHandle();
public static long getLong(MemorySegment p) {
return (long)longHandle.get(p);
if (true) {
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
copyLoop(data, seg);
}
if (true) {
now = System.nanoTime();
for (int l = 0; l < X; l++) {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(data.length * 8, scope);
copyBulk(data, seg);
}
now = System.nanoTime();
for (int l = 0; l < X; l++) {
try (Frame frame = api.Memory.createFrame()) {
- MemorySegment seg = frame.allocate(CLinker.C_POINTER, data.length);
+ MemorySegment seg = frame.allocate(Memory.POINTER, data.length);
copyLoop(data, seg);
}
}
now = System.nanoTime();
for (int l = 0; l < X; l++) {
try (Frame frame = api.Memory.createFrame()) {
- MemorySegment seg = frame.allocate(CLinker.C_POINTER, data.length);
+ MemorySegment seg = frame.allocate(Memory.POINTER, data.length);
copyBulk(data, seg);
}
}
import au.notzed.zcl.CLContext;
import au.notzed.zcl.CLPlatform;
import java.util.stream.Stream;
-import jdk.incubator.foreign.MemoryAddress;
-import jdk.incubator.foreign.MemorySegment;
+import java.lang.foreign.MemoryAddress;
+import java.lang.foreign.MemorySegment;
public class TestExtensions {
*/
package au.notzed.zcl.test;
-import api.Native;
+import api.Memory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.MemoryAddress;
-import jdk.incubator.foreign.MemorySegment;
-import jdk.incubator.foreign.ResourceScope;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.MemorySession;
/**
* Some memory tests.
float sum = 0;
for (int i = 0; i < len; i++)
- sum += MemoryAccess.getFloatAtIndex(seg, i);
- MemoryAccess.setFloat(seg, sum);
+ sum += seg.getAtIndex(Memory.FLOAT, i);
+ seg.set(Memory.FLOAT, 0, sum);
}
static void check(float[] seg) {
for (int i = 0; i < sizes.length; i++) {
arrays[i] = new float[sizes[i]];
- segments[i] = MemorySegment.allocateNative(sizes[i] * 4, 16, ResourceScope.globalScope());
+ segments[i] = MemorySegment.allocateNative(sizes[i] * 4, 16, MemorySession.global());
buffers[i] = ByteBuffer.allocateDirect(sizes[i] * 4).order(ByteOrder.nativeOrder());
}
- for (int c = 0; c < 10; c++) {
+ for (int c = 0; c < 3; c++) {
long now;
now = System.nanoTime();
*/
package au.notzed.zcl.test;
+import api.Memory;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.LongBuffer;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.MemorySegment;
-import jdk.incubator.foreign.ResourceScope;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.MemorySession;
/**
* Some memory tests.
long sum = 0;
for (int i = 0; i < len; i++)
- sum += MemoryAccess.getLongAtIndex(seg, i);
- MemoryAccess.setLong(seg, sum);
+ sum += seg.getAtIndex(Memory.LONG, i);
+ seg.set(Memory.LONG, 0, sum);
}
static void check(long[] seg) {
for (int i = 0; i < sizes.length; i++) {
arrays[i] = new long[sizes[i]];
- segments[i] = MemorySegment.allocateNative(sizes[i] * 8, 16, ResourceScope.globalScope());
+ segments[i] = MemorySegment.allocateNative(sizes[i] * 8, 16, MemorySession.global());
buffers[i] = ByteBuffer.allocateDirect(sizes[i] * 8).order(ByteOrder.nativeOrder());
}
- for (int c = 0; c < 10; c++) {
+ for (int c = 0; c < 3; c++) {
long now;
now = System.nanoTime();
CLDevice dev = CLPlatform.getBestDevice(CL.CL_DEVICE_TYPE_ALL);
cl = CLContext.createContext(null, new CLDevice[]{dev});
- for (int c = 0; c < 10; c++) {
+ for (int c = 0; c < 3; c++) {
long now;
now = System.nanoTime();
*/
module notzed.zcl.demo {
requires notzed.zcl;
- requires jdk.incubator.foreign;
-
+
exports au.notzed.zcl.tools;
}
package api;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.MemoryAddress;
public class Callback<T> extends Native implements AutoCloseable {
T func;
*/
package api;
-import jdk.incubator.foreign.CLinker;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.MemoryAddress;
-import jdk.incubator.foreign.MemoryLayout;
-import jdk.incubator.foreign.MemorySegment;
+import java.lang.foreign.MemoryAddress;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
// TODO: after writing this i discovered SegmentAllocator, perhaps should be one of those.
public interface Frame extends AutoCloseable {
}
default MemorySegment allocateInt() {
- return allocate(CLinker.C_INT);
+ return allocate(Memory.INT);
}
default MemorySegment allocateInt(int count) {
- return allocate(CLinker.C_INT, count);
+ return allocate(Memory.INT, count);
}
default MemorySegment allocateLong() {
- return allocate(CLinker.C_LONG);
+ return allocate(Memory.LONG);
}
default MemorySegment allocateLong(int count) {
- return allocate(CLinker.C_LONG, count);
+ return allocate(Memory.LONG, count);
}
default MemorySegment allocateAddress() {
- return allocate(CLinker.C_POINTER);
+ return allocate(Memory.POINTER);
}
default MemorySegment allocateAddress(int count) {
- return allocate(CLinker.C_POINTER, count);
+ return allocate(Memory.POINTER, count);
}
default MemorySegment copy(byte value) {
- MemorySegment mem = allocate(1);
- MemoryAccess.setByte(mem, value);
+ MemorySegment mem = allocate(Memory.BYTE);
+ mem.set(Memory.BYTE, 0, value);
return mem;
}
default MemorySegment copy(short value) {
- MemorySegment mem = allocate(2, 2);
- MemoryAccess.setShort(mem, value);
+ MemorySegment mem = allocate(Memory.SHORT);
+ mem.set(Memory.SHORT, 0, value);
return mem;
}
default MemorySegment copy(int value) {
- MemorySegment mem = allocate(4, 4);
- MemoryAccess.setInt(mem, value);
+ MemorySegment mem = allocate(Memory.INT);
+ mem.set(Memory.INT, 0, value);
return mem;
}
default MemorySegment copy(long value) {
- MemorySegment mem = allocate(8, 8);
- MemoryAccess.setLong(mem, value);
+ MemorySegment mem = allocate(Memory.LONG);
+ mem.set(Memory.LONG, 0, value);
return mem;
}
default MemorySegment copy(float value) {
- MemorySegment mem = allocate(4, 4);
- MemoryAccess.setFloat(mem, value);
+ MemorySegment mem = allocate(Memory.FLOAT);
+ mem.set(Memory.FLOAT, 0, value);
return mem;
}
default MemorySegment copy(double value) {
- MemorySegment mem = allocate(8, 8);
- MemoryAccess.setDouble(mem, value);
+ MemorySegment mem = allocate(Memory.DOUBLE);
+ mem.set(Memory.DOUBLE, 0, value);
return mem;
}
default <T extends Native> MemorySegment copy(T[] array) {
MemorySegment mem = allocateAddress(array.length);
for (int i = 0; i < array.length; i++)
- MemoryAccess.setAddressAtIndex(mem, i, array[i].address());
+ mem.setAtIndex(Memory.POINTER, i, array[i].address());
+
return mem;
}
default <T extends Native> MemorySegment copy(MemoryAddress value) {
MemorySegment mem = allocateAddress();
- MemoryAccess.setAddress(mem, value);
+ mem.set(Memory.POINTER, 0, value);
return mem;
}
default MemorySegment copy(String[] array) {
MemorySegment list = allocateAddress(array.length);
for (int i = 0; i < array.length; i++) {
- MemoryAccess.setAddressAtIndex(list, i, copy(array[i]));
+ list.setAtIndex(Memory.POINTER, i, copy(array[i]));
}
return list;
}
*/
package api;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.Linker;
+import java.lang.foreign.MemoryAddress;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.MemorySession;
+import java.lang.foreign.ValueLayout;
+import static java.lang.foreign.ValueLayout.ADDRESS;
+import static java.lang.foreign.ValueLayout.JAVA_BYTE;
+import static java.lang.foreign.ValueLayout.JAVA_DOUBLE;
+import static java.lang.foreign.ValueLayout.JAVA_FLOAT;
+import static java.lang.foreign.ValueLayout.JAVA_INT;
+import static java.lang.foreign.ValueLayout.JAVA_LONG;
+import static java.lang.foreign.ValueLayout.JAVA_SHORT;
import java.lang.ref.Cleaner;
/**
*/
public class Memory {
- static final ResourceScope scope = ResourceScope.newSharedScope(Cleaner.create());
+ // probably should be INT8 INT16, etc
+ public static final ValueLayout.OfByte BYTE = JAVA_BYTE;
+ public static final ValueLayout.OfShort SHORT = JAVA_SHORT.withBitAlignment(16);
+ public static final ValueLayout.OfInt INT = JAVA_INT.withBitAlignment(32);
+ public static final ValueLayout.OfLong LONG = JAVA_LONG.withBitAlignment(64);
+ public static final ValueLayout.OfFloat FLOAT = JAVA_FLOAT.withBitAlignment(32);
+ public static final ValueLayout.OfDouble DOUBLE = JAVA_DOUBLE.withBitAlignment(64);
+ public static final ValueLayout.OfAddress POINTER = ADDRESS.withBitAlignment(64);
+
+ static final MemorySession scope = MemorySession.openShared(Cleaner.create());
private static final ThreadLocal<Stack> stacks = ThreadLocal.withInitial(() -> new Stack(scope));
public static Frame createFrame() {
private long sp;
private Thread thread = Thread.currentThread();
- Stack(ResourceScope scope) {
+ Stack(MemorySession scope) {
stack = MemorySegment.allocateNative(4096, 4096, scope);
sp = 4096;
}
return new Frame() {
private final long tos = sp;
private Thread self = thread;
- private ResourceScope scope;
+ private MemorySession scope;
@Override
public MemorySegment allocate(long size, long alignment) {
return stack.asSlice(sp, size);
} else {
if (scope == null)
- scope = ResourceScope.newConfinedScope();
+ scope = MemorySession.openConfined();
return MemorySegment.allocateNative(size, alignment, scope);
}
}
@Override
public MemorySegment copy(String value) {
+ if (scope == null)
+ scope = MemorySession.openConfined();
if (value != null) {
- if (scope == null)
- scope = ResourceScope.newConfinedScope();
- return CLinker.toCString(value, scope);
+ return scope.allocateUtf8String(value);
} else {
- return MemorySegment.globalNativeSegment().asSlice(0, 0);
+ return MemorySegment.ofAddress(MemoryAddress.NULL, 0, scope);
}
}
import java.util.List;
import java.util.function.Function;
import java.util.function.IntFunction;
-import jdk.incubator.foreign.*;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.System.Logger.Level;
+import java.lang.foreign.Addressable;
+import java.lang.foreign.FunctionDescriptor;
+import java.lang.foreign.Linker;
+import java.lang.foreign.MemoryAddress;
+import java.lang.foreign.MemoryLayout;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.MemorySession;
+import java.lang.foreign.SymbolLookup;
/**
* Base class for all native objects.
* <p>
* FIXME: there are MemorySegment based accessors for primitive types now, use those
*/
-public class Native implements Addressable {
+public class Native {
private final MemoryAddress p;
/* helpers - native to java */
public static <T extends Native> T[] toObjectV(MemorySegment list, T[] array, Function<MemoryAddress, T> create) {
for (int i = 0; i < array.length; i++)
- array[i] = Native.resolve(MemoryAccess.getAddressAtIndex(list, i), create);
+ array[i] = Native.resolve(list.getAtIndex(Memory.POINTER, i), create);
return array;
}
public static MethodHandle downcallHandle(SymbolLookup lib, String name, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- CLinker abi = CLinker.getInstance();
+ Linker abi = Linker.nativeLinker();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
MemoryLayout resLayout = sig.layouts[n - 1];
if (resLayout != null) {
- return lib.lookup(name).map(addr -> abi.downcallHandle(addr,
- MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
- FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1))))
+ return lib.lookup(name).map(addr
+ -> abi.downcallHandle(addr, FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1))))
.get();
} else {
- return lib.lookup(name).map(addr -> abi.downcallHandle(addr,
- MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
- FunctionDescriptor.ofVoid(Arrays.copyOf(sig.layouts, n - 1))))
+ return lib.lookup(name).map(addr
+ -> abi.downcallHandle(addr, FunctionDescriptor.ofVoid(Arrays.copyOf(sig.layouts, n - 1))))
.get();
}
}
public static MethodHandle downcallHandle(MemoryAddress addr, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- CLinker abi = CLinker.getInstance();
+ Linker abi = Linker.nativeLinker();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
MemoryLayout resLayout = sig.layouts[n - 1];
return abi.downcallHandle(addr,
- MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n - 1)),
FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1)));
}
// instance must be of a functional interface
- public static MemoryAddress upcallStub(MethodHandles.Lookup lookup, Object instance, String signature) {
+ public static MemorySegment upcallStub(MethodHandles.Lookup lookup, Object instance, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- CLinker abi = CLinker.getInstance();
+ Linker abi = Linker.nativeLinker();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
Method m = instance.getClass().getMethods()[0];
MethodType mt = MethodType.methodType(m.getReturnType(), m.getParameterTypes());
- // FIXME: ResourceScope usage is broken here
+ // FIXME: MemorySession usage is broken here
//System.out.printf("instance %s\n", instance);
//System.out.printf("declaring class %s\n", m.getDeclaringClass());
try {
if (resLayout != null) {
- return abi.upcallStub(lookup.findVirtual(
- m.getDeclaringClass(),
- m.getName(),
- mt)
- .bindTo(instance),
+ return abi.upcallStub(
+ lookup.findVirtual(
+ m.getDeclaringClass(),
+ m.getName(),
+ mt).bindTo(instance),
FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n - 1)),
- ResourceScope.newSharedScope());
+ MemorySession.openShared());
} else {
- return abi.upcallStub(lookup.findVirtual(
- m.getDeclaringClass(),
- m.getName(),
- mt)
- .bindTo(instance),
+ return abi.upcallStub(
+ lookup.findVirtual(
+ m.getDeclaringClass(),
+ m.getName(),
+ mt).bindTo(instance),
FunctionDescriptor.ofVoid(Arrays.copyOf(sig.layouts, n - 1)),
- ResourceScope.newSharedScope());
+ MemorySession.openShared());
}
} catch (NoSuchMethodException | IllegalAccessException x) {
throw new RuntimeException(x);
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(CLinker.C_POINTER);
+ argLayout.add(Memory.POINTER);
} else {
switch (c) {
case 'u':
switch (size) {
case 8:
argClass.add(byte.class);
- argLayout.add(CLinker.C_CHAR);
+ argLayout.add(Memory.BYTE);
break;
case 16:
// char.class?
argClass.add(short.class);
- argLayout.add(CLinker.C_SHORT);
+ argLayout.add(Memory.SHORT);
break;
case 32:
argClass.add(int.class);
- argLayout.add(CLinker.C_INT);
+ argLayout.add(Memory.INT);
break;
case 64:
argClass.add(long.class);
- argLayout.add(CLinker.C_LONG);
+ argLayout.add(Memory.LONG);
break;
}
break;
switch (size) {
case 8:
argClass.add(byte.class);
- argLayout.add(CLinker.C_CHAR);
+ argLayout.add(Memory.BYTE);
break;
case 16:
argClass.add(short.class);
- argLayout.add(CLinker.C_SHORT);
+ argLayout.add(Memory.SHORT);
break;
case 32:
argClass.add(int.class);
- argLayout.add(CLinker.C_INT);
+ argLayout.add(Memory.INT);
break;
case 64:
argClass.add(long.class);
- argLayout.add(CLinker.C_LONG);
+ argLayout.add(Memory.LONG);
break;
}
break;
switch (size) {
case 32:
argClass.add(float.class);
- argLayout.add(CLinker.C_FLOAT);
+ argLayout.add(Memory.FLOAT);
break;
case 64:
argClass.add(double.class);
- argLayout.add(CLinker.C_DOUBLE);
+ argLayout.add(Memory.DOUBLE);
break;
}
break;
case 'x':
switch (size) {
case 8:
- argLayout.add(MemoryLayouts.PAD_8);
+ argLayout.add(MemoryLayout.paddingLayout(8));
break;
case 16:
- argLayout.add(MemoryLayouts.PAD_16);
+ argLayout.add(MemoryLayout.paddingLayout(16));
break;
case 32:
- argLayout.add(MemoryLayouts.PAD_32);
+ argLayout.add(MemoryLayout.paddingLayout(32));
break;
case 64:
- argLayout.add(MemoryLayouts.PAD_64);
+ argLayout.add(MemoryLayout.paddingLayout(64));
break;
}
break;
//System.out.printf("void *=%d\n", pointerDepth);
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(CLinker.C_POINTER);
+ argLayout.add(Memory.POINTER);
} else {
// can only be return value
argClass.add(void.class);
//System.out.printf(" type: *=%d %s\n", pointerDepth, sb);
if (pointerDepth > 0) {
argClass.add(MemoryAddress.class);
- argLayout.add(CLinker.C_POINTER);
+ argLayout.add(Memory.POINTER);
} else {
argClass.add(MemorySegment.class);
try {
}
return o;
}*/
+ public static <T extends Native> T resolve(Addressable p, Function<MemoryAddress, T> create) {
+ return resolve(p.address(), create);
+ }
+
public static <T extends Native> T resolve(MemoryAddress p, Function<MemoryAddress, T> create) {
T o;
boolean step = false;
release(o);
}
- static {
+ static{
Thread cleanup = new Thread(Native::cleaner, "Native cleaner");
cleanup.setPriority(Thread.MAX_PRIORITY);
cleanup.setDaemon(true);
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.*;
import java.nio.ByteBuffer;
int res;
b = clCreateSubBuffer(address(), flags, CL_BUFFER_CREATE_TYPE_REGION, pinfo.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(b, (c) -> new CLBuffer(c, getObjectPlatform()));
package au.notzed.zcl;
import api.Frame;
-import jdk.incubator.foreign.CLinker;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.MemorySegment;
+import api.Memory;
+import java.lang.foreign.MemorySegment;
/**
* Parameters for Buffer.createSubBuffer()
}
MemorySegment toNative(Frame frame) {
- MemorySegment addr = frame.allocate(CLinker.C_LONG, 2); // FIXME: size_t
+ MemorySegment addr = frame.allocate(Memory.LONG, 2); // FIXME: size_t
- MemoryAccess.setLong(addr, origin);
- MemoryAccess.setLongAtIndex(addr, 1, size);
+ addr.setAtIndex(Memory.LONG, 0, origin);
+ addr.setAtIndex(Memory.LONG, 1, size);
return addr;
}
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Memory;
import api.Callback;
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueReadBuffer(mem, blocking,
mem_offset, size,
seg,
byte[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size, scope);
enqueueReadBuffer(mem, true, mem_offset, size, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).get(buffer, buf_offset, size);
short[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 2, scope);
enqueueReadBuffer(mem, true, mem_offset * 2, size * 2, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asShortBuffer().get(buffer, buf_offset, size);
int[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 4, scope);
enqueueReadBuffer(mem, true, mem_offset * 4, size * 4, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asIntBuffer().get(buffer, buf_offset, size);
long[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 8, scope);
enqueueReadBuffer(mem, true, mem_offset * 8, size * 8, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asLongBuffer().get(buffer, buf_offset, size);
float[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 4, scope);
enqueueReadBuffer(mem, true, mem_offset * 4, size * 4, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().get(buffer, buf_offset, size);
double[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 8, scope);
enqueueReadBuffer(mem, true, mem_offset * 8, size * 8, seg, wait, event);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asDoubleBuffer().get(buffer, buf_offset, size);
ByteBuffer buffer,
CLEventList waiters, CLEventList events) throws CLException, UnsupportedOperationException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueReadBufferRect(mem, blocking,
buffer_origin, host_origin, region,
buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
long slice = region[1] * stride;
long size = slice * region[2];
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size, scope);
enqueueReadBufferRect(mem, blocking,
buffer_origin, OFFSET_0x0x0, region,
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueWriteBuffer(mem, blocking,
mem_offset, size,
seg,
byte[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset, size, seg, wait, event);
short[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 2L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asShortBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 2, size * 2L, seg, wait, event);
int[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 4L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asIntBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 4, size * 4L, seg, wait, event);
long[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 8L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asLongBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 8, size * 8L, seg, wait, event);
float[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 4L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asFloatBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 4, size * 4L, seg, wait, event);
double[] buffer, int buf_offset,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(size * 8L, scope);
seg.asByteBuffer().order(ByteOrder.nativeOrder()).asDoubleBuffer().put(buffer, buf_offset, size);
enqueueWriteBuffer(mem, true, mem_offset * 8, size * 8L, seg, wait, event);
long buffer_row_pitch, long buffer_slice_pitch, long host_row_pitch, long host_slice_pitch,
ByteBuffer buffer,
CLEventList waiters, CLEventList events) throws CLException, UnsupportedOperationException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueWriteBufferRect(mem, blocking,
buffer_origin, host_origin, region,
buffer_row_pitch, buffer_slice_pitch, host_row_pitch, host_slice_pitch,
long size,
CLEventList wait,
CLEventList event) throws CLException {
- MemorySegment seg = MemorySegment.ofByteBuffer(pattern);
+ MemorySegment seg = MemorySegment.ofBuffer(pattern);
enqueueFillBuffer(buffer,
pattern,
offset, size,
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(pattern.length, scope);
seg.copyFrom(MemorySegment.ofArray(pattern));
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(pattern.length * 2, scope);
seg.copyFrom(MemorySegment.ofArray(pattern));
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4, scope);
seg.copyFrom(MemorySegment.ofArray(pattern));
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4, scope);
seg.copyFrom(MemorySegment.ofArray(pattern));
long size,
CLEventList wait,
CLEventList event) throws CLException {
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(pattern.length * 4, scope);
seg.copyFrom(MemorySegment.ofArray(pattern));
long xstride = region[0] << dshift;
long xslice = xstride * region[1];
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(alloc, scope);
if (!readMode)
writer.apply(seg).copy((int)region[0], (int)region[1], (int)region[2], (int)stride, (int)slice);
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueReadImage(image, blocking,
origin, region,
row_pitch, slice_pitch,
ByteBuffer buffer,
CLEventList wait,
CLEventList event) throws CLException {
- MemorySegment seg = MemorySegment.ofByteBuffer(buffer);
+ MemorySegment seg = MemorySegment.ofBuffer(buffer);
enqueueWriteImage(image, blocking,
origin, region,
row_pitch, slice_pitch,
flags, offset, size,
info.nwait, info.wait, info.event,
pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
- return cmap.asSegment(size, ResourceScope.globalScope()).asByteBuffer();
+ return MemorySegment.ofAddress(cmap, size, MemorySession.global()).asByteBuffer();
} catch (CLException | RuntimeException | Error t) {
throw t;
} catch (Throwable t) {
cstride.address(), cslice.address(),
info.nwait, info.wait, info.event,
pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
- stride = MemoryAccess.getLong(cstride);
- slice = MemoryAccess.getLong(cslice);
+ stride = cstride.get(Memory.LONG, 0);
+ slice = cslice.get(Memory.LONG, 0);
image_row_pitch[0] = stride;
if (image_slice_pitch != null)
? stride * region[1] + region[0] // 2D
: slice * region[2] + stride * region[1] + region[0]; // 3D
- return cmap.asSegment(size, ResourceScope.globalScope()).asByteBuffer();
+ return MemorySegment.ofAddress(cmap, size, MemorySession.global()).asByteBuffer();
} catch (CLException | RuntimeException | Error t) {
throw t;
} catch (Throwable t) {
public void enqueueUnmapMemObject(CLMemory mem, ByteBuffer mapped,
CLEventList wait,
CLEventList event) throws CLException {
- MemoryAddress cmap = MemorySegment.ofByteBuffer(mapped).address();
+ MemoryAddress cmap = MemorySegment.ofBuffer(mapped).address();
try (Frame frame = Memory.createFrame()) {
EventInfo info = new EventInfo(frame, wait, event);
return frame.copy(ws);
} else {
// or null??
- return MemorySegment.globalNativeSegment();
+ return MemorySegment.ofAddress(MemoryAddress.NULL, 0, MemorySession.global());
}
}
// This means args and mem_list are the same value
try (Frame frame = Memory.createFrame()) {
EventInfo info = new EventInfo(frame, waiters, events);
- MemorySegment memstage = frame.allocate(CLinker.C_POINTER, args.length);
- MemorySegment memptrs = frame.allocate(CLinker.C_POINTER, args.length);
+ MemorySegment memstage = frame.allocate(Memory.POINTER, args.length);
+ MemorySegment memptrs = frame.allocate(Memory.POINTER, args.length);
int nmem = 0;
int res;
for (Object a: args) {
if (a instanceof CLMemory) {
- MemoryAccess.setAddressAtIndex(memstage, nmem, ((CLMemory)a).address());
- MemoryAccess.setAddressAtIndex(memptrs, nmem, memstage.asSlice(nmem * CLinker.C_POINTER.byteSize()));
+ memstage.setAtIndex(Memory.POINTER, nmem, ((CLMemory)a).address());
+ memptrs.setAtIndex(Memory.POINTER, nmem, memstage.asSlice(nmem * Memory.POINTER.byteSize()));
nmem++;
}
}
tmem++;
}
- MemorySegment seg = memargs.asSegment(tmem * CLinker.C_POINTER.byteSize(), ResourceScope.globalScope());
+ MemorySegment seg = MemorySegment.ofAddress(memargs, tmem * Memory.POINTER.byteSize(), MemorySession.global());
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof CLMemory) {
- MemoryAddress mem = MemoryAccess.getAddressAtIndex(seg, xmem);
+ MemoryAddress mem = seg.get(Memory.POINTER, xmem);
long size = ((CLMemory)args[i]).getSize();
- save[i] = mem.asSegment(size, ResourceScope.globalScope()).asByteBuffer().order(ByteOrder.nativeOrder());
+ save[i] = MemorySegment.ofAddress(mem, size, MemorySession.global()).asByteBuffer().order(ByteOrder.nativeOrder());
xmem++;
}
}
}),
(p) -> new Callback<>(p, kernel));
- res = clEnqueueNativeKernel(address(), call.address(), memstage.address(), nmem * CLinker.C_POINTER.byteSize(), nmem, memstage.address(), memptrs.address(), info.nwait, info.wait, info.event);
+ res = clEnqueueNativeKernel(address(), call.address(), memstage.address(), nmem * Memory.POINTER.byteSize(), nmem, memstage.address(), memptrs.address(), info.nwait, info.wait, info.event);
if (res != 0)
throw new CLException(res);
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Memory;
import api.Native;
import api.Callback;
int res;
for (int i = 0; i < devices.length; i++)
- MemoryAccess.setAddressAtIndex(pdevs, i, devices[i].address());
+ pdevs.setAtIndex(Memory.POINTER, i, devices[i].address());
cl = clCreateContext(pprops.address(), devices.length, pdevs.address(), call.address(), MemoryAddress.NULL, pres.address());
- res = MemoryAccess.getInt(pres);
+ res = pres.get(Memory.INT, 0);
if (res != 0)
throw new CLRuntimeException(res);
static CLPlatform findPlatform(MemoryAddress ccl) {
try (Frame frame = Memory.createFrame()) {
MemorySegment devices = getInfoAny(ccl, CL_CONTEXT_DEVICES, clGetContextInfo, frame);
- MemoryAddress dev0 = MemoryAccess.getAddress(devices);
- MemoryAddress plat = MemoryAccess.getAddress(getInfo(dev0, CL_DEVICE_PLATFORM, clGetDeviceInfo, frame, 8));
+ MemoryAddress dev0 = devices.get(Memory.POINTER, 0);
+ MemoryAddress plat = getInfo(dev0, CL_DEVICE_PLATFORM, clGetDeviceInfo, frame, 8).get(Memory.POINTER, 0);
return Native.resolve(plat, CLPlatform::new);
} catch (RuntimeException | Error t) {
int res;
cl = clCreateContextFromType(pprops.address(), device_type, call.address(), MemoryAddress.NULL, pres.address());
- res = MemoryAccess.getInt(pres);
+ res = pres.get(Memory.INT, 0);
if (res != 0)
throw new CLRuntimeException(res);
int res;
q = clCreateCommandQueue(address(), dev.address(), properties, pres.address());
- res = MemoryAccess.getInt(pres);
+ res = pres.get(Memory.INT, 0);
if (res != 0)
throw new CLRuntimeException(res);
int res;
q = clCreateCommandQueueWithProperties(address(), dev.address(), pprops.address(), pres.address());
- res = MemoryAccess.getInt(pres);
+ res = pres.get(Memory.INT, 0);
if (res != 0)
throw new CLRuntimeException(res);
int res;
pbuffer = clCreateBuffer(address(), flags, size, addr(hostseg), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
if (hostseg != null && (flags & CL_MEM_USE_HOST_PTR) != 0)
*/
public CLBuffer createBuffer(long flags, long size, ByteBuffer hostp) throws CLRuntimeException {
if (hostp != null) {
- MemorySegment hostseg = MemorySegment.ofByteBuffer(hostp);
+ MemorySegment hostseg = MemorySegment.ofBuffer(hostp);
try {
CLBuffer buffer = createBuffer(flags, size, hostseg);
}
}
- if ((res = MemoryAccess.getInt(cres)) != 0)
+ if ((res = cres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
if (hostseg != null && (flags & CL_MEM_USE_HOST_PTR) != 0)
public CLImage createImage(long flags, CLImageFormat fmt, CLImageDesc desc, ByteBuffer hostp) throws CLRuntimeException, UnsupportedOperationException {
if (hostp != null) {
- MemorySegment hostseg = MemorySegment.ofByteBuffer(hostp);
+ MemorySegment hostseg = MemorySegment.ofBuffer(hostp);
try {
CLImage image = createImage(flags, fmt, desc, hostseg);
MemoryAddress cp;
cp = clCreatePipe(address(), flags, packetSize, maxPackets, pprops.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return resolve(cp, (x) -> new CLPipe(x, getObjectPlatform()));
if (res != 0)
throw new CLRuntimeException(res);
- num = MemoryAccess.getInt(cnum);
+ num = cnum.get(Memory.INT, 0);
list = frame.allocateAddress(8);
res = clGetSupportedImageFormats(address(), flags, type, num, list.address(), cnum.address());
CLImageFormat[] out = new CLImageFormat[num];
for (int i = 0; i < out.length; i++)
- out[i] = CLImageFormat.fromNative(MemoryAccess.getAddressAtIndex(list, i).asSegment(CLImageFormat.layout().byteSize(), ResourceScope.globalScope()));
+ out[i] = CLImageFormat.fromNative(MemorySegment.ofAddress(list.getAtIndex(Memory.POINTER, i), CLImageFormat.layout().byteSize(), MemorySession.global()));
return out;
} catch (RuntimeException | Error t) {
MemoryAddress cs;
cs = clCreateSampler(address(), norm ? 1 : 0, addr_mode, filter_mode, cres.address());
- if ((res = MemoryAccess.getInt(cres)) != 0)
+ if ((res = cres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return resolve(cs, (x) -> new CLSampler(x, getObjectPlatform()));
MemoryAddress cs;
cs = clCreateSamplerWithProperties(address(), pprops.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return resolve(cs, (x) -> new CLSampler(x, getObjectPlatform()));
int res;
cp = clCreateProgramWithSource(address(), 1, cstring.address(), clength.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
int res;
cp = clCreateProgramWithIL(address(), buffer.address(), il.length, pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
MemorySegment cdevs = frame.copy(devices);
MemorySegment barray = frame.allocateAddress(binaries.length);
MemorySegment larray = frame.allocateLong(binaries.length);
- MemorySegment cstatus = status != null ? frame.allocateInt(status.length) : MemorySegment.globalNativeSegment();
+ MemorySegment cstatus = status != null ? frame.allocateInt(status.length) : MemorySegment.ofAddress(MemoryAddress.NULL, 0, MemorySession.global());
MemorySegment pres = frame.allocateInt();
MemoryAddress cp;
int res;
MemorySegment addr = buffer.asSlice(o);
addr.copyFrom(MemorySegment.ofArray(binaries[i]));
- MemoryAccess.setAddressAtIndex(barray, i, addr);
- MemoryAccess.setLongAtIndex(larray, i, binaries[i].length);
+ barray.setAtIndex(Memory.POINTER, i, addr);
+ larray.setAtIndex(Memory.LONG, i, binaries[i].length);
o += binaries[i].length;
}
cp = clCreateProgramWithBinary(address(), devices.length, cdevs.address(), larray.address(), barray.address(), cstatus.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
if (status != null) {
for (int i = 0; i < status.length; i++)
- status[i] = MemoryAccess.getIntAtIndex(cstatus, i);
+ status[i] = cstatus.getAtIndex(Memory.INT, i);
}
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
int res;
cp = clCreateProgramWithBuiltInKernels(address(), devices.length, cdevs.address(), cnames.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
int res;
cp = clLinkProgram(address(), devices.length, cdevs.address(), coptions.address(), programs.length, cprogs.address(), cnotify.address(), MemoryAddress.NULL, pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform()));
int res;
ce = clCreateUserEvent(address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(ce, (x) -> new CLEvent(x, getObjectPlatform()));
int res;
ce = gl.clCreateFromGLBuffer(address(), flags, bufobj, cres.address());
- if ((res = MemoryAccess.getInt(cres)) != 0)
+ if ((res = cres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(ce, (b) -> new CLBuffer(b, getObjectPlatform()));
} catch (RuntimeException | Error t) {
int res;
ce = gl.clCreateFromGLTexture(address(), flags, target, miplevel, texture, pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(ce, (x) -> new CLImage(x, getObjectPlatform()));
} catch (RuntimeException | Error t) {
int res;
ce = gl.clCreateFromGLRenderbuffer(address(), flags, renderbuffer, pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(ce, (x) -> new CLImage(x, getObjectPlatform()));
} catch (RuntimeException | Error t) {
int res;
ce = gl.clCreateEventFromGLsyncKHR(address(), glsync, pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(ce, CLEvent::new);
package au.notzed.zcl;
import java.nio.ByteBuffer;
-import jdk.incubator.foreign.MemorySegment;
+import java.lang.foreign.MemorySegment;
import api.Native;
import api.Callback;
-import jdk.incubator.foreign.CLinker;
-import jdk.incubator.foreign.ResourceScope;
+import java.lang.foreign.MemorySession;
/**
* Callback for CLContext.createContext*()
if (notify != null) {
return Native.resolve(
Call_pBpvJpv_v.stub((cwhat, cinfo, cinfolen, dummy) -> {
- MemorySegment seg = cinfo.asSegment(cinfolen, ResourceScope.globalScope());
- notify.notify(CLinker.toJavaString(cwhat), seg.asByteBuffer());
+ MemorySegment seg = MemorySegment.ofAddress(cinfo, cinfolen, MemorySession.global());
+ notify.notify(cwhat.getUtf8String(0), seg.asByteBuffer());
}),
(p) -> new Callback<>(p, notify));
} else {
package au.notzed.zcl;
import java.lang.invoke.MethodHandle;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
import java.util.stream.Stream;
*/
package au.notzed.zcl;
-import api.Native;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.MemorySegment;
+import api.Memory;
+import java.lang.foreign.MemorySegment;
/**
* Properties for CLDevice sub-device creation.
@Override
public int toInt(MemorySegment dst, int o) {
- MemoryAccess.setIntAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
+ dst.setAtIndex(Memory.INT, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
for (int c : counts)
- MemoryAccess.setIntAtIndex(dst, o++, c);
- MemoryAccess.setIntAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
+ dst.setAtIndex(Memory.INT, o++, c);
+ dst.setAtIndex(Memory.INT, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
return o;
}
-
+
@Override
public int toLong(MemorySegment dst, int o) {
- MemoryAccess.setLongAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
+ dst.setAtIndex(Memory.LONG, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS);
for (int c : counts)
- MemoryAccess.setLongAtIndex(dst, o++, c);
- MemoryAccess.setLongAtIndex(dst, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
+ dst.setAtIndex(Memory.LONG, o++, c);
+ dst.setAtIndex(Memory.LONG, o++, CL.CL_DEVICE_PARTITION_BY_COUNTS_LIST_END);
return o;
}
}
*/
package au.notzed.zcl;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
import api.*;
*/
static CLPlatform findPlatform(MemoryAddress cevent) {
try (Frame frame = Memory.createFrame()) {
- MemoryAddress ccl = MemoryAccess.getAddress(getInfo(cevent, CL_EVENT_CONTEXT, clGetEventInfo, frame, 8));
+ MemoryAddress ccl = getInfo(cevent, CL_EVENT_CONTEXT, clGetEventInfo, frame, 8).get(Memory.POINTER, 0);
return CLContext.findPlatform(ccl);
} catch (RuntimeException | Error t) {
*/
package au.notzed.zcl;
-import jdk.incubator.foreign.MemoryAddress;
-import jdk.incubator.foreign.MemorySegment;
+import api.Memory;
+import java.lang.foreign.MemoryAddress;
+import java.lang.foreign.MemorySegment;
import api.Native;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.CLinker;
-import jdk.incubator.foreign.MemoryAccess;
-import jdk.incubator.foreign.ResourceScope;
+import java.lang.foreign.MemorySession;
/**
* Manages a list of cl_events.
/**
* Scope is required for native allocation
*/
- final ResourceScope scope = ResourceScope.newConfinedScope();
+ final MemorySession scope = MemorySession.openConfined();
/**
* Raw event values.
*/
*/
public CLEventList(int capacity) {
this.jevents = new CLEvent[capacity];
- this.cevents = MemorySegment.allocateNative(CLinker.C_POINTER.byteSize() * capacity, CLinker.C_POINTER.byteAlignment(), scope);
+ this.cevents = scope.allocateArray(Memory.POINTER, capacity);
}
/**
if (ev == null) {
try {
- clReleaseEvent(MemoryAccess.getAddressAtIndex(cevents, i));
+ clReleaseEvent(cevents.getAtIndex(Memory.POINTER, i));
} catch (Throwable t) {
}
} else {
ev.release();
jevents[i] = null;
}
- MemoryAccess.setAddressAtIndex(cevents, i, MemoryAddress.NULL);
+ cevents.setAtIndex(Memory.POINTER, i, MemoryAddress.NULL);
}
index = 0;
}
MemorySegment addr = cevents.asSlice(index * 8);
// This should already be null, but this performs a range check
- MemoryAccess.setAddress(addr, MemoryAddress.NULL);
+ addr.set(Memory.POINTER, 0, MemoryAddress.NULL);
return addr.address();
} else
throw new ArrayIndexOutOfBoundsException();
if (i < index) {
CLEvent ev = jevents[i];
if (ev == null)
- jevents[i] = ev = Native.resolve(MemoryAccess.getAddressAtIndex(cevents, i), CLEvent::new);
+ jevents[i] = ev = Native.resolve(cevents.getAtIndex(Memory.POINTER, i), CLEvent::new);
return ev;
} else
throw new ArrayIndexOutOfBoundsException();
* @param event
*/
public void add(CLEvent event) {
- MemoryAccess.setAddressAtIndex(cevents, index, event.address());
+ cevents.setAtIndex(Memory.POINTER, index, event.address());
jevents[index++] = event;
}
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.nio.ByteBuffer;
import java.lang.invoke.MethodHandle;
import api.Memory;
*/
package au.notzed.zcl;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Frame;
+import api.Memory;
/**
* Holder for cl_image_desc equivalent.
public static <T extends CLProperty> MemorySegment toNative(Frame frame, CLImageDesc d) {
MemorySegment addr = frame.allocate(LAYOUT);
- MemoryAccess.setInt(addr, d.imageType);
- MemoryAccess.setLongAtIndex(addr, 1, d.imageWidth);
- MemoryAccess.setLongAtIndex(addr, 2, d.imageHeight);
- MemoryAccess.setLongAtIndex(addr, 3, d.imageDepth);
- MemoryAccess.setLongAtIndex(addr, 4, d.imageArraySize);
- MemoryAccess.setLongAtIndex(addr, 5, d.imageRowPitch);
- MemoryAccess.setLongAtIndex(addr, 6, d.imageSlicePitch);
- MemoryAccess.setLongAtIndex(addr, 7, d.numMipLevels);
- MemoryAccess.setLongAtIndex(addr, 8, d.numSamples);
- MemoryAccess.setAddressAtIndex(addr, 9, d.memObject.address());
+ addr.set(Memory.INT, 0, d.imageType);
+ addr.setAtIndex(Memory.LONG, 1, d.imageWidth);
+ addr.setAtIndex(Memory.LONG, 2, d.imageHeight);
+ addr.setAtIndex(Memory.LONG, 3, d.imageDepth);
+ addr.setAtIndex(Memory.LONG, 4, d.imageArraySize);
+ addr.setAtIndex(Memory.LONG, 5, d.imageRowPitch);
+ addr.setAtIndex(Memory.LONG, 6, d.imageSlicePitch);
+ addr.setAtIndex(Memory.LONG, 7, d.numMipLevels);
+ addr.setAtIndex(Memory.LONG, 8, d.numSamples);
+ addr.setAtIndex(Memory.POINTER, 9, d.memObject.address());
return addr;
}
- public static CLImageFormat fromNative(MemorySegment addr) {
- return new CLImageFormat(MemoryAccess.getInt(addr), MemoryAccess.getIntAtIndex(addr, 1));
+ public static CLImageDesc fromNative(MemorySegment addr) {
+ return new CLImageDesc(
+ addr.get(Memory.INT, 0),
+ addr.get(Memory.LONG, 8),
+ addr.get(Memory.LONG, 16),
+ addr.get(Memory.LONG, 24),
+ addr.get(Memory.LONG, 32),
+ addr.get(Memory.LONG, 40),
+ addr.get(Memory.LONG, 48),
+ addr.get(Memory.LONG, 56),
+ addr.get(Memory.LONG, 64),
+ null // no way to know?
+ );
}
static final MemoryLayout LAYOUT = Native.parseStruct("[u32(image_type)x32u64(image_width)u64(image_height)u64(image_depth)u64(image_array_size)u64(image_row_pitch)u64(image_slice_pitch)u32(num_mip_levels)u32(num_samples)u64(buffer):${_cl_mem}u64(mem_object):${_cl_mem}]");
*/
package au.notzed.zcl;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Frame;
+import api.Memory;
/**
* Holder for cl_image_format equivalent.
* since these are just going to be used temporarily
*/
static MemorySegment toNative(Frame frame, CLImageFormat fmt) {
- MemorySegment addr = frame.allocate(CLinker.C_INT, 2);
+ MemorySegment addr = frame.allocate(Memory.INT, 2);
- MemoryAccess.setInt(addr, fmt.channelOrder);
- MemoryAccess.setIntAtIndex(addr, 1, fmt.channelDataType);
+ addr.set(Memory.INT, 0, fmt.channelOrder);
+ addr.set(Memory.INT, 4, fmt.channelDataType);
return addr;
}
static CLImageFormat fromNative(MemorySegment addr) {
- return new CLImageFormat(MemoryAccess.getInt(addr), MemoryAccess.getIntAtIndex(addr, 1));
+ return new CLImageFormat(addr.get(Memory.INT, 0), addr.get(Memory.INT, 4));
}
static final MemoryLayout LAYOUT = Native.parseStruct("[u32(image_channel_order)u32(image_channel_data_type)]");
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Frame;
import api.Memory;
MemorySegment pres = frame.allocateInt();
MemoryAddress ck = clCloneKernel(address(), pres.address());
int res;
-
- if ((res = MemoryAccess.getInt(pres)) != 0)
+
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLRuntimeException(res);
return Native.resolve(ck, (x) -> new CLKernel(x, getObjectPlatform()));
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(o);
- setKernelArg(index, CLinker.C_POINTER.byteSize(), pval.address());
+ setKernelArg(index, Memory.POINTER.byteSize(), pval.address());
}
}
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(val);
- setKernelArg(index, CLinker.C_SHORT.byteSize(), pval.address());
+ setKernelArg(index, Memory.SHORT.byteSize(), pval.address());
}
}
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(val);
- setKernelArg(index, CLinker.C_INT.byteSize(), pval.address());
+ setKernelArg(index, Memory.INT.byteSize(), pval.address());
}
}
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(val);
- setKernelArg(index, CLinker.C_LONG.byteSize(), pval.address());
+ setKernelArg(index, Memory.LONG.byteSize(), pval.address());
}
}
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(val);
- setKernelArg(index, CLinker.C_FLOAT.byteSize(), pval.address());
+ setKernelArg(index, Memory.FLOAT.byteSize(), pval.address());
}
}
try (Frame frame = Memory.createFrame()) {
MemorySegment pval = frame.copy(val);
- setKernelArg(index, CLinker.C_DOUBLE.byteSize(), pval.address());
+ setKernelArg(index, Memory.DOUBLE.byteSize(), pval.address());
}
}
/**
* Set a non-native memory segment argument
* @param index
- * @param val
+ * @param val
*/
private void setArg(int index, MemorySegment val) {
try (Frame frame = Memory.createFrame()) {
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Callback;
import api.Memory;
try (Frame frame = Memory.createFrame()) {
MemorySegment addr = getInfo(p, CL_MEM_TYPE, clGetMemObjectInfo, frame, 4);
- int type = MemoryAccess.getInt(addr);
+ int type = addr.get(Memory.INT, 0);
switch (type) {
case CL_MEM_OBJECT_BUFFER:
res = getGLext().clGetGLObjectInfo(address(), ctype.address(), cname.address());
if (res != 0)
throw new CLRuntimeException(res);
- return new GLObjectInfo(MemoryAccess.getInt(ctype), MemoryAccess.getInt(cname));
+ return new GLObjectInfo(ctype.get(Memory.INT, 0), cname.get(Memory.INT, 0));
} catch (RuntimeException | Error t) {
throw t;
} catch (Throwable t) {
*/
public int getGLTextureInfoInt(int param) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getInt(getInfo(address(), CL_GL_TEXTURE_TARGET, getGLext().clGetGLTextureInfo, frame, 4));
+ return getInfo(address(), CL_GL_TEXTURE_TARGET, getGLext().clGetGLTextureInfo, frame, 4).get(Memory.INT, 0);
}
}
package au.notzed.zcl;
import java.util.function.Function;
-import jdk.incubator.foreign.MemoryAddress;
+import java.lang.foreign.MemoryAddress;
import api.Native;
import api.Callback;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.BiFunction;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
/**
MemorySegment addr = frame.allocate(size, 8);
int res;
- res = (int)getInfo.invokeExact(self, id, size, addr.address(), MemoryAddress.NULL);
+ res = (int)getInfo.invokeExact((Addressable)self, id, size, (Addressable)addr.address(), (Addressable)MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
protected int getInfoInt(int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getInt(getInfo(address(), id, getInfo, frame, 4));
+ return getInfo(address(), id, getInfo, frame, 4).get(Memory.INT, 0);
}
}
protected long getInfoLong(int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getLong(getInfo(address(), id, getInfo, frame, 8));
+ return getInfo(address(), id, getInfo, frame, 8).get(Memory.LONG, 0);
}
}
protected <T extends CLObject> T getInfoAny(int id, MethodHandle getInfo, Function<MemoryAddress, T> create) {
try (Frame frame = Memory.createFrame()) {
- return Native.resolve(MemoryAccess.getAddress(getInfo(address(), id, getInfo, frame, 8)), create);
+ return Native.resolve(getInfo(address(), id, getInfo, frame, 8).get(Memory.POINTER, 0), create);
}
}
long size;
int res;
- res = (int)getInfo.invokeExact(addr, id, 0L, MemoryAddress.NULL, sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)addr, id, 0L, (Addressable)MemoryAddress.NULL, (Addressable)sizep.address());
- size = MemoryAccess.getLong(sizep);
+ size = sizep.get(Memory.LONG, 0);
valp = frame.allocate(size);
- res = (int)getInfo.invokeExact(addr, id, size, valp.address(), sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)addr, id, size, (Addressable)valp.address(), (Addressable)sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
protected byte[] getInfoByteV(int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return getInfoAny(address(), id, getInfo, frame).toByteArray();
+ return getInfoAny(address(), id, getInfo, frame).toArray(Memory.BYTE);
}
}
MemorySegment addr = frame.allocate(size, 16);
int res;
- res = (int)getInfo.invokeExact(address(), ctx.address(), id, size, addr.address(), MemoryAddress.NULL);
+ res = (int)getInfo.invokeExact((Addressable)address(), (Addressable)ctx.address(), id, size, (Addressable)addr.address(), (Addressable)MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
protected <T extends CLObject> int getInfoInt(T ctx, int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getInt(getInfo(ctx, id, getInfo, frame, 4));
+ return getInfo(ctx, id, getInfo, frame, 4).get(Memory.INT, 0);
}
}
protected <T extends CLObject> long getInfoLong(T ctx, int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getLong(getInfo(ctx, id, getInfo, frame, 8));
+ return getInfo(ctx, id, getInfo, frame, 8).get(Memory.LONG, 0);
}
}
long size;
int res;
- res = (int)getInfo.invokeExact(address(), ctx.address(), id, 0L, MemoryAddress.NULL, sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)address(), (Addressable)ctx.address(), id, 0L, (Addressable)MemoryAddress.NULL, (Addressable)sizep.address());
- size = MemoryAccess.getLong(sizep);
+ size = sizep.get(Memory.LONG, 0);
valp = frame.allocate(size, 16);
- res = (int)getInfo.invokeExact(address(), ctx.address(), id, size, valp.address(), sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)address(), (Addressable)ctx.address(), id, size, (Addressable)valp.address(), (Addressable)sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
protected <T extends CLObject> byte[] getInfoByteV(T ctx, int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
MemorySegment seg = getInfoAny(ctx, id, getInfo, frame);
- return seg.toByteArray();
+ return seg.toArray(Memory.BYTE);
}
}
// clGet*Info includes terminating 0
static String infoToString(MemorySegment seg) {
- return CLinker.toJavaString(seg);
+ return seg.getUtf8String(0);
}
protected <T extends CLObject> String getInfoString(T ctx, int id, MethodHandle getInfo) {
try {
// TODO: type for alignment
MemorySegment addr = frame.allocate(size, 16);
- int res = (int)getInfo.invokeExact(address(), index, id, size, addr, MemoryAddress.NULL);
+ int res = (int)getInfo.invokeExact((Addressable)address(), index, id, size, (Addressable)addr, (Addressable)MemoryAddress.NULL);
if (res != 0)
throw new IllegalArgumentException();
protected int getInfoInt(int index, int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getInt(getInfo(index, id, getInfo, frame, 4));
+ return getInfo(index, id, getInfo, frame, 4).get(Memory.INT, 0);
}
}
protected long getInfoLong(int index, int id, MethodHandle getInfo) {
try (Frame frame = Memory.createFrame()) {
- return MemoryAccess.getLong(getInfo(index, id, getInfo, frame, 8));
+ return getInfo(index, id, getInfo, frame, 8).get(Memory.LONG, 0);
}
}
long size;
int res;
- res = (int)getInfo.invokeExact(address(), index, id, 0L, MemoryAddress.NULL, sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)address(), index, id, 0L, (Addressable)MemoryAddress.NULL, (Addressable)sizep.address());
- size = MemoryAccess.getLong(sizep);
+ size = sizep.get(Memory.LONG, 0);
valp = frame.allocate(size, 16);
- res = (int)getInfo.invokeExact(address(), index, id, size, valp.address(), sizep.address());
+ res = (int)getInfo.invokeExact((Addressable)address(), index, id, size, (Addressable)valp.address(), (Addressable)sizep.address());
if (res != 0)
throw new CLRuntimeException(res);
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
/**
import static au.notzed.zcl.CLLib.*;
import java.util.function.ToDoubleFunction;
import java.util.function.Function;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.*;
/**
int len;
int res;
- res = (int)clGetPlatformIDs.invokeExact(0, MemoryAddress.NULL, lenp.address());
+ res = (int)clGetPlatformIDs.invokeExact(0, (Addressable)MemoryAddress.NULL, (Addressable)lenp.address());
if (res != 0)
throw new CLRuntimeException(res);
- len = MemoryAccess.getInt(lenp);
- list = frame.allocate(MemoryLayout.sequenceLayout(len, CLinker.C_POINTER));
+ len = lenp.get(Memory.INT, 0);
+ list = frame.allocate(MemoryLayout.sequenceLayout(len, Memory.POINTER));
- res = (int)clGetPlatformIDs.invokeExact(len, list.address(), lenp.address());
+ res = (int)clGetPlatformIDs.invokeExact(len, (Addressable)list.address(), (Addressable)lenp.address());
if (res != 0)
throw new CLRuntimeException(res);
MemorySegment list;
int res, len;
- res = (int)clGetDeviceIDs.invokeExact(address(), type, 0, MemoryAddress.NULL, lenp.address());
+ res = (int)clGetDeviceIDs.invokeExact((Addressable)address(), type, 0, (Addressable)MemoryAddress.NULL, (Addressable)lenp.address());
if (res == CL_DEVICE_NOT_FOUND)
return new CLDevice[0];
else if (res != 0)
throw new CLRuntimeException(res);
- len = MemoryAccess.getInt(lenp);
- list = frame.allocate(MemoryLayout.sequenceLayout(len, CLinker.C_POINTER));
+ len = lenp.get(Memory.INT, 0);
+ list = frame.allocate(MemoryLayout.sequenceLayout(len, Memory.POINTER));
- res = (int)clGetDeviceIDs.invokeExact(address(), type, len, list.address(), lenp.address());
+ res = (int)clGetDeviceIDs.invokeExact((Addressable)address(), type, len, (Addressable)list.address(), (Addressable)lenp.address());
if (res != 0)
throw new CLRuntimeException(res);
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import api.Memory;
import api.Callback;
MemoryAddress ck;
ck = clCreateKernel(address(), pname.address(), pres.address());
- if ((res = MemoryAccess.getInt(pres)) != 0)
+ if ((res = pres.get(Memory.INT, 0)) != 0)
throw new CLException(res);
return resolve(ck, (x) -> new CLKernel(x, getObjectPlatform()));
} catch (CLException | RuntimeException | Error t) {
if (res != 0)
throw new CLRuntimeException();
- count = MemoryAccess.getInt(csize);
+ count = csize.get(Memory.INT, 0);
return Native.toObjectV(ckern, new CLKernel[count], (x) -> new CLKernel(x, getObjectPlatform()));
} catch (RuntimeException | Error t) {
throw t;
int res;
for (int i = 0; i < sizes.length; i++) {
- MemoryAccess.setAddressAtIndex(cptrs, i, data.addOffset(off));
+ cptrs.setAtIndex(Memory.POINTER, i, data.addOffset(off));
off += sizes[i];
}
- res = clGetProgramInfo(address(), CL_PROGRAM_BINARIES, sizes.length * CLinker.C_POINTER.byteSize(), cptrs.address(), MemoryAddress.NULL);
+ res = clGetProgramInfo(address(), CL_PROGRAM_BINARIES, sizes.length * Memory.POINTER.byteSize(), cptrs.address(), MemoryAddress.NULL);
if (res != 0)
throw new CLRuntimeException(res);
byte[][] out = new byte[sizes.length][];
off = 0;
for (int i = 0; i < sizes.length; i++) {
- out[i] = seg.asSlice(off, sizes[i]).toByteArray();
+ out[i] = seg.asSlice(off, sizes[i]).toArray(Memory.BYTE);
off += sizes[i];
}
import api.Frame;
+import api.Memory;
import static au.notzed.zcl.CL.*;
-import jdk.incubator.foreign.MemorySegment;
+import java.lang.foreign.MemorySegment;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
import java.util.ArrayList;
-import jdk.incubator.foreign.MemoryAccess;
/**
* All property arrays implement this interface.
@Override
public int toInt(MemorySegment dst, int o) {
- MemoryAccess.setIntAtIndex(dst, o++, (int)tag);
- MemoryAccess.setIntAtIndex(dst, o++, (int)value);
+ dst.setAtIndex(Memory.INT, o++, (int)tag);
+ dst.setAtIndex(Memory.INT, o++, (int)value);
return o;
}
@Override
public int toLong(MemorySegment dst, int o) {
- MemoryAccess.setLongAtIndex(dst, o++, tag);
- MemoryAccess.setLongAtIndex(dst, o++, value);
+ dst.setAtIndex(Memory.LONG, o++, tag);
+ dst.setAtIndex(Memory.LONG, o++, value);
return o;
}
for (CLProperty p: properties)
i = p.toLong(addr, i);
- MemoryAccess.setLongAtIndex(addr, i, 0L);
+ addr.setAtIndex(Memory.LONG, i, 0L);
return addr;
} else {
MemorySegment addr = frame.allocateLong();
- MemoryAccess.setLong(addr, 0L);
+ addr.set(Memory.LONG, 0, 0L);
return addr;
}
}
ArrayList<T> list = new ArrayList<>();
long tag;
- for (int i = 0;(tag = MemoryAccess.getLongAtIndex(seg, i++)) != 0;) {
+ for (int i = 0;(tag = seg.getAtIndex(Memory.LONG, i++)) != 0;) {
// HACK: this needs some other mechanism, just hardcode each for now
if (tag == CL_DEVICE_PARTITION_BY_COUNTS) {
throw new UnsupportedOperationException();
} else {
- long value = MemoryAccess.getLongAtIndex(seg, i++);
+ long value = seg.getAtIndex(Memory.LONG, i++);
list.add(create.apply(tag, value));
}
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
/**
package au.notzed.zcl;
import api.Frame;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
/**
* Simplify wait/event handling.
* Implements a java-friendly binding to OpenCL.
*/
module notzed.zcl {
- requires jdk.incubator.foreign;
-
exports au.notzed.zcl;
exports api to notzed.zcl.demo;
print $dst <<END;
import java.foreign.Libraries;
import java.lang.invoke.MethodHandles;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
$importPoineter
END
print $dst "package $package;\n";
}
print $dst <<END;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
$importPoineter
print $dst "package $package;\n";
}
print $dst <<END;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import api.Native;
import java.util.function.Function;
for $pi (@params) {
my %param = %{$pi};
+ if ($param{deref} || ($param{type} =~ m/^struct|union/)) {
+ print $dst "(Addressable)";
+ }
+
print $dst "$param{name}";
print $dst ", " if ($pi != $params[$#params]);
}
print $dst "package $package;\n";
}
print $dst <<END;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
# upcall ##############################################################
# ??
- print $dst "\tstatic MemoryAddress stub($name call) {\n";
+ print $dst "\tstatic MemorySegment stub($name call) {\n";
print $dst "\t\treturn Native.upcallStub(MethodHandles.lookup(), call, \"$signature\");\n";
print $dst "\t}\n";
print $dst "package $package;\n";
}
print $dst <<END;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
for $pi (@params) {
my %param = %{$pi};
+ if ($param{deref} || ($param{type} =~ m/^struct|union/)) {
+ print $dst "(Addressable)";
+ }
print $dst "$param{name}";
if ($param{deref}) {
print $dst ".addr()";
package au.notzed.zcl;
+import api.Memory;
import org.junit.*;
import static org.junit.Assert.*;
import static au.notzed.zcl.CL.*;
-import jdk.incubator.foreign.*;
-import api.Native;
+import java.lang.foreign.*;
import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
/*
TODO: USE_HOST_PTR and what it does
static void fillSegment(MemorySegment seg, byte[] seq) {
for (long i = 0; i < seg.byteSize(); i++) {
- MemoryAccess.setByteAtOffset(seg, i, seq[(int)(i % seq.length)]);
+ seg.set(Memory.BYTE, i, seq[(int)(i % seq.length)]);
}
}
boolean same = true;
for (long i = 0; same && i < a.byteSize(); i++) {
- same &= MemoryAccess.getByteAtOffset(a, i) == MemoryAccess.getByteAtOffset(b, i);
+ same &= a.get(Memory.BYTE, i) == b.get(Memory.BYTE, i);
}
return same;
}
boolean same = true;
for (long i = 0; same && i < a.capacity(); i++) {
- same &= a.get((int)i) == MemoryAccess.getByteAtOffset(b, i);
+ same &= a.get((int)i) == b.get(Memory.BYTE, i);
}
return same;
}
public void testCreate4() throws Exception {
System.out.println("createBuffer COPY_HOST_PTR");
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
MemorySegment segb = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
assertEquals(CLRuntimeException.class, x.getClass());
assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(8, 8, scope);
x = null;
cl.createBuffer(0, 8, seg);
assertEquals(CLRuntimeException.class, x.getClass());
assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(8, 8, scope);
x = null;
cl.createBuffer(0, seg);
Throwable x;
System.out.println("createBuffer sized segment");
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(720, 8, scope);
CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, seg);
@Test
public void testMap1() throws Exception {
System.out.println("mapBuffer");
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
@Test
public void testMap2() throws Exception {
System.out.println("mapBuffer double unmap");
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment sega = MemorySegment.allocateNative(1024, 8, scope);
fillSegment(sega, filla);
byte[] data = new byte[]{1, 2, 3, 4};
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(1024, 8, scope);
CLBuffer b = cl.createBuffer(0, 1024);
boolean same = true;
for (int i = 0; same && i < 1024; i++) {
- same = MemoryAccess.getByteAtOffset(seg, i) == data[i % data.length];
+ same = seg.get(Memory.BYTE, i) == data[i % data.length];
}
assertTrue(same);
import org.junit.*;
import static org.junit.Assert.*;
import static au.notzed.zcl.CL.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.Native;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static org.junit.Assert.*;
import static au.notzed.zcl.CL.*;
import static au.notzed.zcl.CLLib.*;
-import jdk.incubator.foreign.*;
+import java.lang.foreign.*;
import api.*;
/*
CLEvent ev = cl.createUserEvent();
CLBuffer mem = cl.createBuffer(0, 64);
- try (ResourceScope scope = ResourceScope.newConfinedScope();
+ try (MemorySession scope = MemorySession.openConfined();
CLEventList list = new CLEventList(1);
CLEventList wait = new CLEventList(1)) {
MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
System.out.println("event callback, state");
CLBuffer mem = cl.createBuffer(0, 64);
- try (ResourceScope scope = ResourceScope.newConfinedScope();
+ try (MemorySession scope = MemorySession.openConfined();
CLEventList list = new CLEventList(1)) {
MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
CLEvent ev;
try (Frame frame = Memory.createFrame()) {
MemorySegment pres = frame.allocateInt();
clGetEventInfo(x, CL_EVENT_REFERENCE_COUNT, 4, pres.address(), MemoryAddress.NULL);
- res = MemoryAccess.getInt(pres);
+ res = pres.get(Memory.INT, 0);
} catch (Throwable T) {
}
return res;
assertEquals(IllegalStateException.class, x.getClass());
CLBuffer mem = cl.createBuffer(0, 64);
- try (ResourceScope scope = ResourceScope.newConfinedScope()) {
+ try (MemorySession scope = MemorySession.openConfined()) {
MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
x = null;
q.enqueueReadBuffer(mem, false, 0, 64, seg, null, list);
public void testMulti() throws Exception {
System.out.println("multi");
CLBuffer mem = cl.createBuffer(0, 64);
- try (ResourceScope scope = ResourceScope.newConfinedScope();
+ try (MemorySession scope = MemorySession.openConfined();
CLEventList list = new CLEventList(2);
CLEventList last = new CLEventList(1)) {
MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
public void testMultiWait() throws Exception {
System.out.println("multi wait");
CLBuffer mem = cl.createBuffer(0, 64);
- try (ResourceScope scope = ResourceScope.newConfinedScope();
+ try (MemorySession scope = MemorySession.openConfined();
CLEventList list = new CLEventList(3)) {
MemorySegment seg = MemorySegment.allocateNative(64, 8, scope);
import api.Frame;
import api.Memory;
-import jdk.incubator.foreign.MemorySegment;
+import java.lang.foreign.MemorySegment;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;