1
- package org .purejava .linux ;
2
1
// Generated by jextract
2
+ package org .purejava .linux ;
3
3
4
4
import org .slf4j .Logger ;
5
5
import org .slf4j .LoggerFactory ;
13
13
14
14
final class RuntimeHelper {
15
15
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" ); } ;
21
21
private static boolean isLoaded = false ;
22
22
private static final Logger LOG = LoggerFactory .getLogger (RuntimeHelper .class );
23
23
24
24
final static SegmentAllocator CONSTANT_ALLOCATOR =
25
- (size , align ) -> MemorySegment .allocateNative (size , align , MemorySession . openImplicit ());
25
+ (size , align ) -> MemorySegment .allocateNative (size , align , SegmentScope . auto ());
26
26
27
27
static {
28
28
try {
@@ -40,55 +40,63 @@ private RuntimeHelper() {}
40
40
}
41
41
}
42
42
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 ));
44
44
}
45
45
46
+ // Suppresses default constructor, ensuring non-instantiability.
47
+ private RuntimeHelper () {}
48
+
46
49
static <T > T requireNonNull (T obj , String symbolName ) {
47
50
if (obj == null ) {
48
51
throw new UnsatisfiedLinkError ("unresolved symbol: " + symbolName );
49
52
}
50
53
return obj ;
51
54
}
52
55
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 );
57
58
}
58
59
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 ).
61
62
map (addr -> LINKER .downcallHandle (addr , fdesc )).
62
63
orElse (null );
63
64
}
64
65
65
- static final MethodHandle downcallHandle (FunctionDescriptor fdesc ) {
66
+ static MethodHandle downcallHandle (FunctionDescriptor fdesc ) {
66
67
return LINKER .downcallHandle (fdesc );
67
68
}
68
69
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 ).
71
72
map (addr -> VarargsInvoker .make (addr , fdesc )).
72
73
orElse (null );
73
74
}
74
75
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 ) {
76
77
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 ());
80
79
} catch (Throwable ex ) {
81
80
throw new AssertionError (ex );
82
81
}
83
82
}
84
83
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 );
87
95
}
88
96
89
97
// Internals only below this point
90
98
91
- private static class VarargsInvoker {
99
+ private static final class VarargsInvoker {
92
100
private static final MethodHandle INVOKE_MH ;
93
101
private final MemorySegment symbol ;
94
102
private final FunctionDescriptor function ;
@@ -114,7 +122,9 @@ static MethodHandle make(MemorySegment symbol, FunctionDescriptor function) {
114
122
mtype = mtype .appendParameterTypes (carrier (layout , false ));
115
123
}
116
124
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 ) {
118
128
mtype = mtype .insertParameterTypes (0 , SegmentAllocator .class );
119
129
} else {
120
130
handle = MethodHandles .insertArguments (handle , 0 , THROWING_ALLOCATOR );
@@ -124,8 +134,7 @@ static MethodHandle make(MemorySegment symbol, FunctionDescriptor function) {
124
134
125
135
static Class <?> carrier (MemoryLayout layout , boolean ret ) {
126
136
if (layout instanceof ValueLayout valueLayout ) {
127
- return (ret || valueLayout .carrier () != MemoryAddress .class ) ?
128
- valueLayout .carrier () : Addressable .class ;
137
+ return valueLayout .carrier ();
129
138
} else if (layout instanceof GroupLayout ) {
130
139
return MemorySegment .class ;
131
140
} else {
@@ -160,7 +169,9 @@ private Object invoke(SegmentAllocator allocator, Object[] args) throws Throwabl
160
169
FunctionDescriptor .ofVoid (argLayouts ) :
161
170
FunctionDescriptor .of (function .returnLayout ().get (), argLayouts );
162
171
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 ) {
164
175
mh = mh .bindTo (allocator );
165
176
}
166
177
// flatten argument list so that it can be passed to an asSpreader MH
@@ -210,10 +221,7 @@ private Class<?> normalize(Class<?> c) {
210
221
if (c .isPrimitive ()) {
211
222
return promote (c );
212
223
}
213
- if (MemoryAddress .class .isAssignableFrom (c )) {
214
- return MemoryAddress .class ;
215
- }
216
- if (MemorySegment .class .isAssignableFrom (c )) {
224
+ if (c == MemorySegment .class ) {
217
225
return MemorySegment .class ;
218
226
}
219
227
throw new IllegalArgumentException ("Invalid type for ABI: " + c .getTypeName ());
@@ -224,7 +232,7 @@ private MemoryLayout variadicLayout(Class<?> c) {
224
232
return JAVA_LONG ;
225
233
} else if (c == double .class ) {
226
234
return JAVA_DOUBLE ;
227
- } else if (MemoryAddress .class . isAssignableFrom ( c ) ) {
235
+ } else if (c == MemorySegment .class ) {
228
236
return ADDRESS ;
229
237
} else {
230
238
throw new IllegalArgumentException ("Unhandled variadic argument class: " + c );
0 commit comments