1- package org .purejava .linux ;
21// Generated by jextract
2+ package org .purejava .linux ;
33
44import org .slf4j .Logger ;
55import org .slf4j .LoggerFactory ;
1313
1414final class RuntimeHelper {
1515
16- private RuntimeHelper () {}
17- private final static Linker LINKER = Linker . nativeLinker ();
18- private final static ClassLoader LOADER = RuntimeHelper . class . getClassLoader ();
19- private final static MethodHandles . Lookup MH_LOOKUP = MethodHandles . lookup () ;
20- private final static SymbolLookup SYMBOL_LOOKUP ;
16+ private static final Linker LINKER = Linker . nativeLinker ();
17+ private static final ClassLoader LOADER = RuntimeHelper . class . getClassLoader ();
18+ private static final MethodHandles . Lookup MH_LOOKUP = MethodHandles . lookup ();
19+ private static final SymbolLookup SYMBOL_LOOKUP ;
20+ private static final SegmentAllocator THROWING_ALLOCATOR = ( x , y ) -> { throw new AssertionError ( "should not reach here" ); } ;
2121 private static boolean isLoaded = false ;
2222 private static final Logger LOG = LoggerFactory .getLogger (RuntimeHelper .class );
2323
2424 final static SegmentAllocator CONSTANT_ALLOCATOR =
25- (size , align ) -> MemorySegment .allocateNative (size , align , MemorySession . openImplicit ());
25+ (size , align ) -> MemorySegment .allocateNative (size , align , SegmentScope . auto ());
2626
2727 static {
2828 try {
@@ -40,55 +40,63 @@ private RuntimeHelper() {}
4040 }
4141 }
4242 SymbolLookup loaderLookup = SymbolLookup .loaderLookup ();
43- SYMBOL_LOOKUP = name -> loaderLookup .lookup (name ).or (() -> LINKER .defaultLookup ().lookup (name ));
43+ SYMBOL_LOOKUP = name -> loaderLookup .find (name ).or (() -> LINKER .defaultLookup ().find (name ));
4444 }
4545
46+ // Suppresses default constructor, ensuring non-instantiability.
47+ private RuntimeHelper () {}
48+
4649 static <T > T requireNonNull (T obj , String symbolName ) {
4750 if (obj == null ) {
4851 throw new UnsatisfiedLinkError ("unresolved symbol: " + symbolName );
4952 }
5053 return obj ;
5154 }
5255
53- private final static SegmentAllocator THROWING_ALLOCATOR = (x , y ) -> { throw new AssertionError ("should not reach here" ); };
54-
55- static final MemorySegment lookupGlobalVariable (String name , MemoryLayout layout ) {
56- return SYMBOL_LOOKUP .lookup (name ).map (symbol -> MemorySegment .ofAddress (symbol .address (), layout .byteSize (), MemorySession .openShared ())).orElse (null );
56+ static MemorySegment lookupGlobalVariable (String name , MemoryLayout layout ) {
57+ return SYMBOL_LOOKUP .find (name ).map (symbol -> MemorySegment .ofAddress (symbol .address (), layout .byteSize (), symbol .scope ())).orElse (null );
5758 }
5859
59- static final MethodHandle downcallHandle (String name , FunctionDescriptor fdesc ) {
60- return SYMBOL_LOOKUP .lookup (name ).
60+ static MethodHandle downcallHandle (String name , FunctionDescriptor fdesc ) {
61+ return SYMBOL_LOOKUP .find (name ).
6162 map (addr -> LINKER .downcallHandle (addr , fdesc )).
6263 orElse (null );
6364 }
6465
65- static final MethodHandle downcallHandle (FunctionDescriptor fdesc ) {
66+ static MethodHandle downcallHandle (FunctionDescriptor fdesc ) {
6667 return LINKER .downcallHandle (fdesc );
6768 }
6869
69- static final MethodHandle downcallHandleVariadic (String name , FunctionDescriptor fdesc ) {
70- return SYMBOL_LOOKUP .lookup (name ).
70+ static MethodHandle downcallHandleVariadic (String name , FunctionDescriptor fdesc ) {
71+ return SYMBOL_LOOKUP .find (name ).
7172 map (addr -> VarargsInvoker .make (addr , fdesc )).
7273 orElse (null );
7374 }
7475
75- static final < Z > MemorySegment upcallStub (Class <Z > fi , Z z , FunctionDescriptor fdesc , MemorySession session ) {
76+ static MethodHandle upcallHandle (Class <? > fi , String name , FunctionDescriptor fdesc ) {
7677 try {
77- MethodHandle handle = MH_LOOKUP .findVirtual (fi , "apply" , Linker .upcallType (fdesc ));
78- handle = handle .bindTo (z );
79- return LINKER .upcallStub (handle , fdesc , session );
78+ return MH_LOOKUP .findVirtual (fi , name , fdesc .toMethodType ());
8079 } catch (Throwable ex ) {
8180 throw new AssertionError (ex );
8281 }
8382 }
8483
85- static MemorySegment asArray (MemoryAddress addr , MemoryLayout layout , int numElements , MemorySession session ) {
86- return MemorySegment .ofAddress (addr , numElements * layout .byteSize (), session );
84+ static <Z > MemorySegment upcallStub (MethodHandle fiHandle , Z z , FunctionDescriptor fdesc , SegmentScope scope ) {
85+ try {
86+ fiHandle = fiHandle .bindTo (z );
87+ return LINKER .upcallStub (fiHandle , fdesc , scope );
88+ } catch (Throwable ex ) {
89+ throw new AssertionError (ex );
90+ }
91+ }
92+
93+ static MemorySegment asArray (MemorySegment addr , MemoryLayout layout , int numElements , SegmentScope scope ) {
94+ return MemorySegment .ofAddress (addr .address (), numElements * layout .byteSize (), scope );
8795 }
8896
8997 // Internals only below this point
9098
91- private static class VarargsInvoker {
99+ private static final class VarargsInvoker {
92100 private static final MethodHandle INVOKE_MH ;
93101 private final MemorySegment symbol ;
94102 private final FunctionDescriptor function ;
@@ -114,7 +122,9 @@ static MethodHandle make(MemorySegment symbol, FunctionDescriptor function) {
114122 mtype = mtype .appendParameterTypes (carrier (layout , false ));
115123 }
116124 mtype = mtype .appendParameterTypes (Object [].class );
117- if (mtype .returnType ().equals (MemorySegment .class )) {
125+ boolean needsAllocator = function .returnLayout ().isPresent () &&
126+ function .returnLayout ().get () instanceof GroupLayout ;
127+ if (needsAllocator ) {
118128 mtype = mtype .insertParameterTypes (0 , SegmentAllocator .class );
119129 } else {
120130 handle = MethodHandles .insertArguments (handle , 0 , THROWING_ALLOCATOR );
@@ -124,8 +134,7 @@ static MethodHandle make(MemorySegment symbol, FunctionDescriptor function) {
124134
125135 static Class <?> carrier (MemoryLayout layout , boolean ret ) {
126136 if (layout instanceof ValueLayout valueLayout ) {
127- return (ret || valueLayout .carrier () != MemoryAddress .class ) ?
128- valueLayout .carrier () : Addressable .class ;
137+ return valueLayout .carrier ();
129138 } else if (layout instanceof GroupLayout ) {
130139 return MemorySegment .class ;
131140 } else {
@@ -160,7 +169,9 @@ private Object invoke(SegmentAllocator allocator, Object[] args) throws Throwabl
160169 FunctionDescriptor .ofVoid (argLayouts ) :
161170 FunctionDescriptor .of (function .returnLayout ().get (), argLayouts );
162171 MethodHandle mh = LINKER .downcallHandle (symbol , f );
163- if (mh .type ().returnType () == MemorySegment .class ) {
172+ boolean needsAllocator = function .returnLayout ().isPresent () &&
173+ function .returnLayout ().get () instanceof GroupLayout ;
174+ if (needsAllocator ) {
164175 mh = mh .bindTo (allocator );
165176 }
166177 // flatten argument list so that it can be passed to an asSpreader MH
@@ -210,10 +221,7 @@ private Class<?> normalize(Class<?> c) {
210221 if (c .isPrimitive ()) {
211222 return promote (c );
212223 }
213- if (MemoryAddress .class .isAssignableFrom (c )) {
214- return MemoryAddress .class ;
215- }
216- if (MemorySegment .class .isAssignableFrom (c )) {
224+ if (c == MemorySegment .class ) {
217225 return MemorySegment .class ;
218226 }
219227 throw new IllegalArgumentException ("Invalid type for ABI: " + c .getTypeName ());
@@ -224,7 +232,7 @@ private MemoryLayout variadicLayout(Class<?> c) {
224232 return JAVA_LONG ;
225233 } else if (c == double .class ) {
226234 return JAVA_DOUBLE ;
227- } else if (MemoryAddress .class . isAssignableFrom ( c ) ) {
235+ } else if (c == MemorySegment .class ) {
228236 return ADDRESS ;
229237 } else {
230238 throw new IllegalArgumentException ("Unhandled variadic argument class: " + c );
0 commit comments