notzed.zcl.fxdemo_DEMOS=fxdemo.fract.Mandelbrot fxdemo.fract.Test
-DEMOFLAGS=--add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl
+DEMOFLAGS=--add-exports jdk.incubator.foreign/jdk.incubator.foreign.unsafe=notzed.zcl \
+ -Dforeign.restricted=permit
# module class basename(class)
define java_demo=
--- /dev/null
+/*
+ * Copyright (C) 2020 Michael Zucchi
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package au.notzed.zcl.test;
+
+import api.Memory;
+import au.notzed.zcl.CL;
+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;
+
+public class TestExtensions {
+
+ public static void main(String[] args) {
+
+ //CLPlatform plat = CLPlatform.getPlatforms()[0];
+ //CLContext cl = CLContext.createContext(null, plat.getDevices(CL.CL_DEVICE_TYPE_ALL));
+ //Stream.of(plat.getExtensions()).forEach(System.out::println);
+ //GLEvent gle = plat.getExtension(CLPlatform.cl_khr_gl_event, (p) -> new GLEvent(p));
+ //gle.clCreateEventFromGLsync(cl, MemoryAddress.NULL);
+ }
+}
package api;
import jdk.incubator.foreign.*;
-import jdk.incubator.foreign.unsafe.ForeignUnsafe;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
Stack(MemoryAddress p, long size) {
super(p);
- stack = ForeignUnsafe.ofNativeUnchecked(addr(), size);
+ // TODO: use cleanup stuff, perhaps owner thread
+ stack = MemorySegment.ofNativeRestricted(addr(), size, null, null, null);
base = stack.baseAddress();
sp = size;
static {
// So for whatever reason it's been decided that MemorySegment can't be freed normally on another thread
- SystemABI abi = SystemABI.getInstance();
+ SystemABI abi = SystemABI.getSystemABI();
MethodHandles.Lookup lookup = MethodHandles.lookup();
LibraryLookup libc = LibraryLookup.ofDefault();
MethodType.methodType(MemoryAddress.class,
long.class),
FunctionDescriptor.of(MemoryLayouts.SysV.C_POINTER,
- false,
MemoryLayouts.SysV.C_ULONG));
free = abi.downcallHandle(libc.lookup("free"),
MethodType.methodType(void.class,
MemoryAddress.class),
- FunctionDescriptor.ofVoid(false,
- MemoryLayouts.SysV.C_POINTER));
+ FunctionDescriptor.ofVoid(MemoryLayouts.SysV.C_POINTER));
} catch (NoSuchMethodException x) {
throw new RuntimeException(x);
}
* Closing this segment has no effect on the original, and the original must be used for that.
*/
public static MemorySegment ofNative(MemoryAddress addr, long size) {
- return ForeignUnsafe.ofNativeUnchecked(addr, size);
+ return MemorySegment.ofNativeRestricted(addr, size, null, null, null);
}
/**
* Get the physical address. I mean how is this an "offset"?
*/
public static long toLong(MemoryAddress addr) {
- return ForeignUnsafe.getUnsafeOffset(addr);
+ return addr.toRawLongValue();
}
/**
import java.util.function.Function;
import java.util.function.IntFunction;
import jdk.incubator.foreign.*;
-import jdk.incubator.foreign.unsafe.ForeignUnsafe;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;
final static VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
final static VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
final static VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder());
- final static VarHandle addrHandle = MemoryHandles.varHandle(MemoryAddress.class, ByteOrder.nativeOrder());
+ final static VarHandle addrHandle = MemoryHandles.asAddressVarHandle(MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder()));
final static VarHandle byteVHandle = MemoryHandles.withStride(byteHandle, 1);
final static VarHandle shortVHandle = MemoryHandles.withStride(shortHandle, 2);
}
public static String toString(MemoryAddress cstr) {
- MemorySegment seg = ForeignUnsafe.ofNativeUnchecked(cstr, Integer.MAX_VALUE);
+ MemorySegment seg = MemorySegment.ofNativeRestricted(cstr, Integer.MAX_VALUE, null, null, null);
MemoryAddress add = seg.baseAddress();
byte[] data;
int len = 0;
public static MethodHandle downcallHandle(LibraryLookup[] libs, String name, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getInstance();
+ SystemABI abi = SystemABI.getSystemABI();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
try {
return abi.downcallHandle(lib.lookup(name),
MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n-1)),
- FunctionDescriptor.of(resLayout, false, Arrays.copyOf(sig.layouts, n-1)));
+ FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n-1)));
} catch (NoSuchMethodException x) {
}
}
public static MethodHandle downcallHandle(MemoryAddress addr, String signature) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getInstance();
+ SystemABI abi = SystemABI.getSystemABI();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
return abi.downcallHandle(addr,
MethodType.methodType(resClass, Arrays.copyOf(sig.classes, n-1)),
- FunctionDescriptor.of(resLayout, false, Arrays.copyOf(sig.layouts, 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) {
Signature sig = Signature.parse(signature);
int n = sig.classes.length;
- SystemABI abi = SystemABI.getInstance();
+ SystemABI abi = SystemABI.getSystemABI();
if (sig.classes.length != sig.layouts.length)
throw new RuntimeException("layout class mismatch");
m.getName(),
mt)
.bindTo(instance),
- FunctionDescriptor.of(resLayout, false, Arrays.copyOf(sig.layouts, n-1)));
+ FunctionDescriptor.of(resLayout, Arrays.copyOf(sig.layouts, n-1)));
} catch (NoSuchMethodException | IllegalAccessException x) {
throw new RuntimeException(x);
}
}
public static void freeUpcallStub(MemoryAddress addr) {
- SystemABI.getInstance().freeUpcallStub(addr);
+ SystemABI.getSystemABI().freeUpcallStub(addr);
}
public static LibraryLookup[] loadLibraries(String... libraries) {
* @return new property
*/
public static CLContextProperty PLATFORM(CLPlatform platform) {
- return new CLContextProperty.TagValue(CL.CL_CONTEXT_PLATFORM, platform.addr().offset());
+ return new CLContextProperty.TagValue(CL.CL_CONTEXT_PLATFORM, Memory.toLong(platform.addr()));
}
/**
import jdk.incubator.foreign.*;
import java.nio.ByteBuffer;
import java.lang.invoke.MethodHandle;
+import api.Memory;
/**
* Interface for cl_image.
@Override
public String toString() {
- return String.format("[%s: %dx%dx%d 0x%x]", getClass().getSimpleName(), getWidth(), getHeight(), getDepth(), addr().offset());
+ return String.format("[%s: %dx%dx%d 0x%x]", getClass().getSimpleName(), getWidth(), getHeight(), getDepth(), Memory.toLong(addr()));
}
/**
*/
@Deprecated
private static CLMemory create(MemoryAddress p, CLPlatform plat) {
- if (p.offset() == 0)
+ if (Memory.toLong(p) == 0)
return null;