8314592: Add shortcut to SymbolLookup::find

Reviewed-by: jvernee, prr
This commit is contained in:
Per Minborg 2024-04-24 11:56:44 +00:00
parent 15190816f7
commit e923dfe4c5
22 changed files with 151 additions and 63 deletions

View file

@ -88,7 +88,7 @@ public class AllocTest extends CLayouts {
static final MethodHandle CALLOC = Linker.nativeLinker()
.downcallHandle(
Linker.nativeLinker().defaultLookup().find("calloc").get(),
Linker.nativeLinker().defaultLookup().findOrThrow("calloc"),
FunctionDescriptor.of(ValueLayout.ADDRESS, ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG));
static MemorySegment calloc(long size) {

View file

@ -33,7 +33,7 @@ import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
public class CLayouts {
private static Linker LINKER = Linker.nativeLinker();
private static final Linker LINKER = Linker.nativeLinker();
// the constants below are useful aliases for C types. The type/carrier association is only valid for 64-bit platforms.
@ -73,10 +73,10 @@ public class CLayouts {
.withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, C_CHAR));
private static final MethodHandle FREE = LINKER.downcallHandle(
LINKER.defaultLookup().find("free").get(), FunctionDescriptor.ofVoid(C_POINTER));
LINKER.defaultLookup().findOrThrow("free"), FunctionDescriptor.ofVoid(C_POINTER));
private static final MethodHandle MALLOC = LINKER.downcallHandle(
LINKER.defaultLookup().find("malloc").get(), FunctionDescriptor.of(C_POINTER, ValueLayout.JAVA_LONG));
LINKER.defaultLookup().findOrThrow("malloc"), FunctionDescriptor.of(C_POINTER, ValueLayout.JAVA_LONG));
public static void freeMemory(MemorySegment address) {
try {

View file

@ -109,7 +109,7 @@ public class CallOverheadHelper extends CLayouts {
System.loadLibrary("CallOverhead");
SymbolLookup loaderLibs = SymbolLookup.loaderLookup();
{
func_addr = loaderLibs.find("func").orElseThrow();
func_addr = loaderLibs.findOrThrow("func");
MethodType mt = MethodType.methodType(void.class);
FunctionDescriptor fd = FunctionDescriptor.ofVoid();
func_v = abi.downcallHandle(fd);
@ -118,59 +118,59 @@ public class CallOverheadHelper extends CLayouts {
func_critical = insertArguments(func_critical_v, 0, func_addr);
}
{
identity_addr = loaderLibs.find("identity").orElseThrow();
identity_addr = loaderLibs.findOrThrow("identity");
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT);
identity_v = abi.downcallHandle(fd);
identity_critical_v = abi.downcallHandle(fd, Linker.Option.critical(false));
identity = insertArguments(identity_v, 0, identity_addr);
identity_critical = insertArguments(identity_critical_v, 0, identity_addr);
}
identity_struct_addr = loaderLibs.find("identity_struct").orElseThrow();
identity_struct_addr = loaderLibs.findOrThrow("identity_struct");
identity_struct_v = abi.downcallHandle(
FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT));
identity_struct = insertArguments(identity_struct_v, 0, identity_struct_addr);
identity_struct_3_addr = loaderLibs.find("identity_struct_3").orElseThrow();
identity_struct_3_addr = loaderLibs.findOrThrow("identity_struct_3");
identity_struct_3_v = abi.downcallHandle(
FunctionDescriptor.of(POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT, POINT_LAYOUT));
identity_struct_3 = insertArguments(identity_struct_3_v, 0, identity_struct_3_addr);
identity_memory_address_addr = loaderLibs.find("identity_memory_address").orElseThrow();
identity_memory_address_addr = loaderLibs.findOrThrow("identity_memory_address");
identity_memory_address_v = abi.downcallHandle(
FunctionDescriptor.of(C_POINTER, C_POINTER));
identity_memory_address = insertArguments(identity_memory_address_v, 0, identity_memory_address_addr);
identity_memory_address_3_addr = loaderLibs.find("identity_memory_address_3").orElseThrow();
identity_memory_address_3_addr = loaderLibs.findOrThrow("identity_memory_address_3");
identity_memory_address_3_v = abi.downcallHandle(
FunctionDescriptor.of(C_POINTER, C_POINTER, C_POINTER, C_POINTER));
identity_memory_address_3 = insertArguments(identity_memory_address_3_v, 0, identity_memory_address_3_addr);
args1_addr = loaderLibs.find("args1").orElseThrow();
args1_addr = loaderLibs.findOrThrow("args1");
args1_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG));
args1 = insertArguments(args1_v, 0, args1_addr);
args2_addr = loaderLibs.find("args2").orElseThrow();
args2_addr = loaderLibs.findOrThrow("args2");
args2_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE));
args2 = insertArguments(args2_v, 0, args2_addr);
args3_addr = loaderLibs.find("args3").orElseThrow();
args3_addr = loaderLibs.findOrThrow("args3");
args3_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
args3 = insertArguments(args3_v, 0, args3_addr);
args4_addr = loaderLibs.find("args4").orElseThrow();
args4_addr = loaderLibs.findOrThrow("args4");
args4_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));
args4 = insertArguments(args4_v, 0, args4_addr);
args5_addr = loaderLibs.find("args5").orElseThrow();
args5_addr = loaderLibs.findOrThrow("args5");
args5_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG));
args5 = insertArguments(args5_v, 0, args5_addr);
args10_addr = loaderLibs.find("args10").orElseThrow();
args10_addr = loaderLibs.findOrThrow("args10");
args10_v = abi.downcallHandle(
FunctionDescriptor.ofVoid(C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG,
C_DOUBLE, C_LONG_LONG, C_DOUBLE, C_LONG_LONG, C_DOUBLE));

View file

@ -61,7 +61,7 @@ public class CriticalCalls {
System.loadLibrary("CriticalCalls");
SymbolLookup lookup = SymbolLookup.loaderLookup();
MemorySegment sumIntsSym = lookup.find("sum_ints").get();
MemorySegment sumIntsSym = lookup.findOrThrow("sum_ints");
FunctionDescriptor sumIntsDesc = FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT);
PINNED = Linker.nativeLinker().downcallHandle(

View file

@ -58,13 +58,13 @@ public class PointerInvoke extends CLayouts {
static {
Linker abi = Linker.nativeLinker();
SymbolLookup loaderLibs = SymbolLookup.loaderLookup();
F_LONG_LONG = abi.downcallHandle(loaderLibs.find("id_long_long").get(),
F_LONG_LONG = abi.downcallHandle(loaderLibs.findOrThrow("id_long_long"),
FunctionDescriptor.of(C_LONG_LONG, C_LONG_LONG));
F_PTR_LONG = abi.downcallHandle(loaderLibs.find("id_ptr_long").get(),
F_PTR_LONG = abi.downcallHandle(loaderLibs.findOrThrow("id_ptr_long"),
FunctionDescriptor.of(C_LONG_LONG, C_POINTER));
F_LONG_PTR = abi.downcallHandle(loaderLibs.find("id_long_ptr").get(),
F_LONG_PTR = abi.downcallHandle(loaderLibs.findOrThrow("id_long_ptr"),
FunctionDescriptor.of(C_POINTER, C_LONG_LONG));
F_PTR_PTR = abi.downcallHandle(loaderLibs.find("id_ptr_ptr").get(),
F_PTR_PTR = abi.downcallHandle(loaderLibs.findOrThrow("id_ptr_ptr"),
FunctionDescriptor.of(C_POINTER, C_POINTER));
}

View file

@ -58,7 +58,7 @@ public class QSort extends CLayouts {
static final int[] INPUT = { 5, 3, 2, 7, 8, 12, 1, 7 };
static final MemorySegment INPUT_SEGMENT;
static MemorySegment qsort_addr = abi.defaultLookup().find("qsort").get();
static MemorySegment qsort_addr = abi.defaultLookup().findOrThrow("qsort");
static {
MemoryLayout layout = MemoryLayout.sequenceLayout(INPUT.length, JAVA_INT);
@ -74,7 +74,7 @@ public class QSort extends CLayouts {
FunctionDescriptor.ofVoid(C_POINTER, C_LONG_LONG, C_LONG_LONG, C_POINTER)
);
System.loadLibrary("QSort");
native_compar = SymbolLookup.loaderLookup().find("compar").orElseThrow();
native_compar = SymbolLookup.loaderLookup().findOrThrow("compar");
panama_upcall_compar = abi.upcallStub(
lookup().findStatic(QSort.class,
"panama_upcall_compar",

View file

@ -69,7 +69,7 @@ public class StrLenTest extends CLayouts {
static {
Linker abi = Linker.nativeLinker();
STRLEN = abi.downcallHandle(abi.defaultLookup().find("strlen").get(),
STRLEN = abi.downcallHandle(abi.defaultLookup().findOrThrow("strlen"),
FunctionDescriptor.of(C_INT, C_POINTER));
}

View file

@ -66,7 +66,7 @@ public class ToCStringTest extends CLayouts {
static {
Linker abi = Linker.nativeLinker();
STRLEN = abi.downcallHandle(abi.defaultLookup().find("strlen").get(),
STRLEN = abi.downcallHandle(abi.defaultLookup().findOrThrow("strlen"),
FunctionDescriptor.of(C_INT, C_POINTER));
}

View file

@ -120,7 +120,7 @@ public class Upcalls extends CLayouts {
static MethodHandle linkFunc(String name, FunctionDescriptor baseDesc) {
return abi.downcallHandle(
SymbolLookup.loaderLookup().find(name).orElseThrow(),
SymbolLookup.loaderLookup().findOrThrow(name),
baseDesc.appendArgumentLayouts(C_POINTER)
);
}

View file

@ -48,11 +48,11 @@ public class PanamaPoint extends CLayouts implements AutoCloseable {
System.loadLibrary("Point");
SymbolLookup loaderLibs = SymbolLookup.loaderLookup();
MH_distance = abi.downcallHandle(
loaderLibs.find("distance").get(),
loaderLibs.findOrThrow("distance"),
FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
);
MH_distance_ptrs = abi.downcallHandle(
loaderLibs.find("distance_ptrs").get(),
loaderLibs.findOrThrow("distance_ptrs"),
FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
);
}

View file

@ -17,7 +17,7 @@ public class GetArrayForeignXorOpCriticalImpl implements XorOp {
Linker linker;
linker = Linker.nativeLinker();
FunctionDescriptor xor_op_func = FunctionDescriptor.ofVoid(C_POINTER, C_POINTER, C_INT);
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().find("xor_op").orElseThrow(), xor_op_func, critical(true));
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().findOrThrow("xor_op"), xor_op_func, critical(true));
}
static final MethodHandle xor_op;

View file

@ -19,7 +19,7 @@ public class GetArrayForeignXorOpImpl implements XorOp {
Linker linker;
linker = Linker.nativeLinker();
FunctionDescriptor xor_op_func = FunctionDescriptor.ofVoid(C_POINTER, C_POINTER, C_INT);
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().find("xor_op").orElseThrow(), xor_op_func, critical(false));
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().findOrThrow("xor_op"), xor_op_func, critical(false));
}
static final MethodHandle xor_op;

View file

@ -20,7 +20,7 @@ public class GetArrayForeignXorOpInitImpl implements XorOp {
Linker linker;
linker = Linker.nativeLinker();
FunctionDescriptor xor_op_func = FunctionDescriptor.ofVoid(C_POINTER, C_POINTER, C_INT);
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().find("xor_op").orElseThrow(), xor_op_func, critical(false));
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().findOrThrow("xor_op"), xor_op_func, critical(false));
}
static final MethodHandle xor_op;

View file

@ -25,7 +25,7 @@ public class GetArrayUnsafeXorOpImpl implements XorOp {
Linker linker;
linker = Linker.nativeLinker();
FunctionDescriptor xor_op_func = FunctionDescriptor.ofVoid(C_POINTER, C_POINTER, C_INT);
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().find("xor_op").orElseThrow(), xor_op_func, critical(false));
xor_op = linker.downcallHandle(SymbolLookup.loaderLookup().findOrThrow("xor_op"), xor_op_func, critical(false));
}
static final MethodHandle xor_op;