@@ -190,6 +190,34 @@ public MethodHandle getFunctionPtr(String name, FunctionDescriptor fd) {
190190 }
191191 }
192192
193+ /** Get native MethodHandle for specified function name with FunctionDescriptor where the symbol is a function pointer. */
194+ public MethodHandle getFunctionPtrCritical (String name , FunctionDescriptor fd ) {
195+ if (debug ) JFLog .log ("FFM:getFunctionPtr:" + name );
196+ try {
197+ MemorySegment addr = lookup .findOrThrow (name );
198+ if (addr == null || addr .address () == 0 ) {
199+ JFLog .log ("FFM:FunctionPtr not found(1):" + name + "=" + addr );
200+ return null ;
201+ }
202+ //symbols have zero length, need to reinterpret as a pointer
203+ addr = addr .reinterpret (ADDRESS_SIZE );
204+ if (addr == null || addr .address () == 0 ) {
205+ JFLog .log ("FFM:FunctionPtr not found(2):" + name + "=" + addr );
206+ return null ;
207+ }
208+ //now get the contents of the pointer
209+ addr = addr .get (ADDRESS , 0 );
210+ if (addr == null || addr .address () == 0 ) {
211+ JFLog .log ("FFM:FunctionPtr not found(3):" + name + "=" + addr );
212+ return null ;
213+ }
214+ return linker .downcallHandle (addr , fd , Linker .Option .critical (true ));
215+ } catch (Exception e ) {
216+ JFLog .logTrace ("FFM:FunctionPtr not found(e):" + name );
217+ return null ;
218+ }
219+ }
220+
193221 //upcall helpers
194222
195223 private static ValueLayout classToValueLayout (Class <?> cls ) {
@@ -333,6 +361,44 @@ public static MemorySegment toMemory(Arena arena, MemorySegment[] ptrs) {
333361 return array ;
334362 }
335363
364+ //array helpers (critical)
365+
366+ /** Create native array from float[] */
367+ public static MemorySegment toMemory (float [] m ) {
368+ if (m == null ) return MemorySegment .NULL ;
369+ return MemorySegment .ofArray (m );
370+ }
371+
372+ /** Create native array from double[] */
373+ public static MemorySegment toMemory (double [] m ) {
374+ if (m == null ) return MemorySegment .NULL ;
375+ return MemorySegment .ofArray (m );
376+ }
377+
378+ /** Create native array from long[] */
379+ public static MemorySegment toMemory (long [] m ) {
380+ if (m == null ) return MemorySegment .NULL ;
381+ return MemorySegment .ofArray (m );
382+ }
383+
384+ /** Create native array from int[] */
385+ public static MemorySegment toMemory (int [] m ) {
386+ if (m == null ) return MemorySegment .NULL ;
387+ return MemorySegment .ofArray (m );
388+ }
389+
390+ /** Create native array from short[] */
391+ public static MemorySegment toMemory (short [] m ) {
392+ if (m == null ) return MemorySegment .NULL ;
393+ return MemorySegment .ofArray (m );
394+ }
395+
396+ /** Create native array from byte[] */
397+ public static MemorySegment toMemory (byte [] m ) {
398+ if (m == null ) return MemorySegment .NULL ;
399+ return MemorySegment .ofArray (m );
400+ }
401+
336402 private static final long JAVA_LONG_SIZE = JAVA_LONG .byteSize ();
337403 private static final long JAVA_INT_SIZE = JAVA_INT .byteSize ();
338404 private static final long JAVA_SHORT_SIZE = JAVA_SHORT .byteSize ();
0 commit comments