From: Not Zed Date: Sat, 25 Jan 2020 01:55:01 +0000 (+1030) Subject: Add CLObject.release() static methods to avoid Native leakage. X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=92c77d4cb78003ae5eb6360abc67a4f066a64b2e;p=zcl Add CLObject.release() static methods to avoid Native leakage. --- diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java index 1a8fc21..ba54975 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java @@ -31,7 +31,7 @@ import api.Memory; *

* Each object has a single memory pointer which is stored in the p field. * For most objects this is the only value retained and all get methods defer - * to the OpenCL runtime. OpenCL objects with the same pointer are guarnateed + * to the OpenCL runtime. OpenCL objects with the same pointer are guarnateed * to map to the same Java instance. Objects are considered equal if their p * value is identical. *

@@ -41,7 +41,7 @@ import api.Memory; * function used to retrieve the information. *

* Memory management is automatic but can also be explicit at application - * discretion. Use the release() methods for explicit deallocation. + * discretion. Use the release() methods for explicit deallocation. */ public abstract class CLObject extends Native { @@ -49,6 +49,17 @@ public abstract class CLObject extends Native { super(p); } + // for now ... mirror the native ones so native isn't exposed? + public static void release(CLObject list) { + if (list != null) + list.release(); + } + + public static void release(CLObject... list) { + for (CLObject o: list) + release(o); + } + // new 5-param version // this one is static so it can be accessed at creation time protected static MemoryAddress getInfo(MemoryAddress self, int id, MethodHandle getInfo, Allocator frame, long size) throws CLRuntimeException { @@ -134,7 +145,7 @@ public abstract class CLObject extends Native { } } - protected T[] getInfoPropertyV(int id, MethodHandle getInfo, BiFunction create, IntFunction createArray) { + protected T[] getInfoPropertyV(int id, MethodHandle getInfo, BiFunction create, IntFunction createArray) { try (Allocator a = Memory.stack()) { return CLProperty.fromNative(getInfoAny(id, getInfo, a), create, createArray); } @@ -146,7 +157,7 @@ public abstract class CLObject extends Native { } } - protected T[] getInfoAnyV(int id, MethodHandle getInfo, Function create, IntFunction createArray) { + protected T[] getInfoAnyV(int id, MethodHandle getInfo, Function create, IntFunction createArray) { try (Allocator a = Memory.stack()) { return Native.toObjectV(getInfoAny(id, getInfo, a), create, createArray); } catch (RuntimeException | Error t) { @@ -227,14 +238,14 @@ public abstract class CLObject extends Native { static String infoToString(MemorySegment seg) { if (false) { MemoryAddress valp = seg.baseAddress(); - byte[] val = new byte[(int)(seg.byteSize()-1)]; + byte[] val = new byte[(int)(seg.byteSize() - 1)]; - for (int i=0;i>> 3); long[] list = new long[len]; - for (int i=0;i