1 // Generated by jextract
2
3 import jdk.incubator.foreign.FunctionDescriptor;
4 import jdk.incubator.foreign.LibraryLookup;
5 import jdk.incubator.foreign.MemoryAddress;
6 import jdk.incubator.foreign.MemoryLayout;
7 import jdk.incubator.foreign.MemorySegment;
8 import jdk.incubator.foreign.SystemABI;
9
10 import java.lang.invoke.MethodHandle;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.MethodType;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.util.Arrays;
17 import java.util.Optional;
18
19 import static jdk.incubator.foreign.MemoryLayouts.*;
20
21 public class RuntimeHelper {
22
23 private final static SystemABI ABI = SystemABI.getInstance();
24
25 private final static ClassLoader LOADER = RuntimeHelper.class.getClassLoader();
26
27 private final static MethodHandles.Lookup MH_LOOKUP = MethodHandles.lookup();
28
29 static final LibraryLookup[] libraries(String[] libNames, String[] libPaths) {
30 if (libNames.length == 0) {
31 return new LibraryLookup[]{LibraryLookup.ofDefault()};
32 } else {
33 Path[] paths = Arrays.stream(libPaths).map(Paths::get).toArray(Path[]::new);
34 return Arrays.stream(libNames).map(libName -> {
35 Optional<Path> absPath = findLibraryPath(paths, libName);
36 return absPath.isPresent() ?
37 LibraryLookup.ofPath(MH_LOOKUP, absPath.get().toString()) :
38 LibraryLookup.ofLibrary(MH_LOOKUP, libName);
39 }).toArray(LibraryLookup[]::new);
178 }
179
180 private Class<?> normalize(Class<?> c) {
181 c = unboxIfNeeded(c);
182 if (c.isPrimitive()) {
183 return c;
184 }
185 if (MemoryAddress.class.isAssignableFrom(c)) {
186 return MemoryAddress.class;
187 }
188 if (MemorySegment.class.isAssignableFrom(c)) {
189 return MemorySegment.class;
190 }
191 throw new IllegalArgumentException("Invalid type for ABI: " + c.getTypeName());
192 }
193
194 private MemoryLayout variadicLayout(Class<?> c) {
195 if (c == char.class || c == byte.class || c == short.class || c == int.class || c == long.class) {
196 //it is ok to approximate with a machine word here; numerics arguments in a prototype-less
197 //function call are always rounded up to a register size anyway.
198 return C_LONG;
199 } else if (c == float.class || c == double.class) {
200 return C_DOUBLE;
201 } else if (MemoryAddress.class.isAssignableFrom(c)) {
202 return C_POINTER;
203 } else {
204 throw new IllegalArgumentException("Unhandled variadic argument class: " + c);
205 }
206 }
207 }
208 }
|
1 // Generated by jextract
2
3 import jdk.incubator.foreign.FunctionDescriptor;
4 import jdk.incubator.foreign.LibraryLookup;
5 import jdk.incubator.foreign.MemoryAddress;
6 import jdk.incubator.foreign.MemoryLayout;
7 import jdk.incubator.foreign.MemorySegment;
8 import jdk.incubator.foreign.SystemABI;
9
10 import java.lang.invoke.MethodHandle;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.MethodType;
13 import java.nio.file.Files;
14 import java.nio.file.Path;
15 import java.nio.file.Paths;
16 import java.util.Arrays;
17 import java.util.Optional;
18
19 import static ${C_LANG}.*;
20
21 public class RuntimeHelper {
22
23 private final static SystemABI ABI = SystemABI.getInstance();
24
25 private final static ClassLoader LOADER = RuntimeHelper.class.getClassLoader();
26
27 private final static MethodHandles.Lookup MH_LOOKUP = MethodHandles.lookup();
28
29 static final LibraryLookup[] libraries(String[] libNames, String[] libPaths) {
30 if (libNames.length == 0) {
31 return new LibraryLookup[]{LibraryLookup.ofDefault()};
32 } else {
33 Path[] paths = Arrays.stream(libPaths).map(Paths::get).toArray(Path[]::new);
34 return Arrays.stream(libNames).map(libName -> {
35 Optional<Path> absPath = findLibraryPath(paths, libName);
36 return absPath.isPresent() ?
37 LibraryLookup.ofPath(MH_LOOKUP, absPath.get().toString()) :
38 LibraryLookup.ofLibrary(MH_LOOKUP, libName);
39 }).toArray(LibraryLookup[]::new);
178 }
179
180 private Class<?> normalize(Class<?> c) {
181 c = unboxIfNeeded(c);
182 if (c.isPrimitive()) {
183 return c;
184 }
185 if (MemoryAddress.class.isAssignableFrom(c)) {
186 return MemoryAddress.class;
187 }
188 if (MemorySegment.class.isAssignableFrom(c)) {
189 return MemorySegment.class;
190 }
191 throw new IllegalArgumentException("Invalid type for ABI: " + c.getTypeName());
192 }
193
194 private MemoryLayout variadicLayout(Class<?> c) {
195 if (c == char.class || c == byte.class || c == short.class || c == int.class || c == long.class) {
196 //it is ok to approximate with a machine word here; numerics arguments in a prototype-less
197 //function call are always rounded up to a register size anyway.
198 return C_LONGLONG;
199 } else if (c == float.class || c == double.class) {
200 return C_DOUBLE;
201 } else if (MemoryAddress.class.isAssignableFrom(c)) {
202 return C_POINTER;
203 } else {
204 throw new IllegalArgumentException("Unhandled variadic argument class: " + c);
205 }
206 }
207 }
208 }
|