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 @@ import java.util.stream.Stream;
* {@snippet lang = java:
* Linker linker = Linker.nativeLinker();
* MethodHandle strlen = linker.downcallHandle(
* linker.defaultLookup().find("strlen").orElseThrow(),
* linker.defaultLookup().findOrThrow("strlen"),
* FunctionDescriptor.of(JAVA_LONG, ADDRESS)
* );
* }
@ -306,7 +306,7 @@ import java.util.stream.Stream;
* {@snippet lang = java:
* Linker linker = Linker.nativeLinker();
* MethodHandle qsort = linker.downcallHandle(
* linker.defaultLookup().find("qsort").orElseThrow(),
* linker.defaultLookup().findOrThrow("qsort"),
* FunctionDescriptor.ofVoid(ADDRESS, JAVA_LONG, JAVA_LONG, ADDRESS)
* );
* }
@ -397,12 +397,12 @@ import java.util.stream.Stream;
* Linker linker = Linker.nativeLinker();
*
* MethodHandle malloc = linker.downcallHandle(
* linker.defaultLookup().find("malloc").orElseThrow(),
* linker.defaultLookup().findOrThrow("malloc"),
* FunctionDescriptor.of(ADDRESS, JAVA_LONG)
* );
*
* MethodHandle free = linker.downcallHandle(
* linker.defaultLookup().find("free").orElseThrow(),
* linker.defaultLookup().findOrThrow("free"),
* FunctionDescriptor.ofVoid(ADDRESS)
* );
* }
@ -530,7 +530,7 @@ import java.util.stream.Stream;
* {@snippet lang = java:
* Linker linker = Linker.nativeLinker();
* MethodHandle printf = linker.downcallHandle(
* linker.defaultLookup().find("printf").orElseThrow(),
* linker.defaultLookup().findOrThrow("printf"),
* FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT),
* Linker.Option.firstVariadicArg(1) // first int is variadic
* );

View file

@ -39,6 +39,7 @@ import jdk.internal.reflect.Reflection;
import java.lang.invoke.MethodHandles;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
@ -79,7 +80,7 @@ import java.util.function.BiFunction;
* {@snippet lang = java:
* try (Arena arena = Arena.ofConfined()) {
* SymbolLookup libGL = SymbolLookup.libraryLookup("libGL.so", arena); // libGL.so loaded here
* MemorySegment glGetString = libGL.find("glGetString").orElseThrow();
* MemorySegment glGetString = libGL.findOrThrow("glGetString");
* ...
* } // libGL.so unloaded here
*}
@ -93,7 +94,7 @@ import java.util.function.BiFunction;
* System.loadLibrary("GL"); // libGL.so loaded here
* ...
* SymbolLookup libGL = SymbolLookup.loaderLookup();
* MemorySegment glGetString = libGL.find("glGetString").orElseThrow();
* MemorySegment glGetString = libGL.findOrThrow("glGetString");
* }
*
* This symbol lookup, which is known as a <em>loader lookup</em>, is dynamic with
@ -130,7 +131,7 @@ import java.util.function.BiFunction;
* {@snippet lang = java:
* Linker nativeLinker = Linker.nativeLinker();
* SymbolLookup stdlib = nativeLinker.defaultLookup();
* MemorySegment malloc = stdlib.find("malloc").orElseThrow();
* MemorySegment malloc = stdlib.findOrThrow("malloc");
*}
*
* @since 22
@ -144,9 +145,40 @@ public interface SymbolLookup {
* @param name the symbol name
* @return a zero-length memory segment whose address indicates the address of
* the symbol, if found
* @see #findOrThrow(String)
*/
Optional<MemorySegment> find(String name);
/**
* Returns the address of the symbol with the given name or throws an exception.
*<p>
* This is equivalent to the following code, but is more efficient:
* to:
* {@snippet lang= java :
* String name = ...
* MemorySegment address = lookup.find(name)
* .orElseThrow(() -> new NoSuchElementException("Symbol not found: " + name));
* }
*
* @param name the symbol name
* @return a zero-length memory segment whose address indicates the address of
* the symbol
* @throws NoSuchElementException if no symbol address can be found for the
* given name
* @see #find(String)
*
* @since 23
*/
default MemorySegment findOrThrow(String name) {
Objects.requireNonNull(name);
Optional<MemorySegment> address = find(name);
// Avoid lambda capturing
if (address.isPresent()) {
return address.get();
}
throw new NoSuchElementException("Symbol not found: " + name);
}
/**
* {@return a composed symbol lookup that returns the result of finding the symbol
* with this lookup if found, otherwise returns the result of finding

View file

@ -100,7 +100,7 @@
* Linker linker = Linker.nativeLinker();
* SymbolLookup stdlib = linker.defaultLookup();
* MethodHandle strlen = linker.downcallHandle(
* stdlib.find("strlen").orElseThrow(),
* stdlib.findOrThrow("strlen"),
* FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS)
* );
*
@ -111,7 +111,7 @@
*}
*
* Here, we obtain a {@linkplain java.lang.foreign.Linker#nativeLinker() native linker}
* and we use it to {@linkplain java.lang.foreign.SymbolLookup#find(java.lang.String) look up}
* and we use it to {@linkplain java.lang.foreign.SymbolLookup#findOrThrow(java.lang.String) look up}
* the {@code strlen} function in the standard C library; a <em>downcall method handle</em>
* targeting said function is subsequently
* {@linkplain java.lang.foreign.Linker#downcallHandle(FunctionDescriptor, Linker.Option...) obtained}.

View file

@ -163,7 +163,7 @@ class Snippets {
void downcall() throws Throwable {
Linker linker = Linker.nativeLinker();
MethodHandle strlen = linker.downcallHandle(
linker.defaultLookup().find("strlen").orElseThrow(),
linker.defaultLookup().findOrThrow("strlen"),
FunctionDescriptor.of(JAVA_LONG, ADDRESS)
);
@ -177,7 +177,7 @@ class Snippets {
void qsort() throws Throwable {
Linker linker = Linker.nativeLinker();
MethodHandle qsort = linker.downcallHandle(
linker.defaultLookup().find("qsort").orElseThrow(),
linker.defaultLookup().findOrThrow("qsort"),
FunctionDescriptor.ofVoid(ADDRESS, JAVA_LONG, JAVA_LONG, ADDRESS)
);
@ -208,12 +208,12 @@ class Snippets {
Linker linker = Linker.nativeLinker();
MethodHandle malloc = linker.downcallHandle(
linker.defaultLookup().find("malloc").orElseThrow(),
linker.defaultLookup().findOrThrow("malloc"),
FunctionDescriptor.of(ADDRESS, JAVA_LONG)
);
MethodHandle free = linker.downcallHandle(
linker.defaultLookup().find("free").orElseThrow(),
linker.defaultLookup().findOrThrow("free"),
FunctionDescriptor.ofVoid(ADDRESS)
);
@ -282,7 +282,7 @@ class Snippets {
Linker linker = Linker.nativeLinker();
MethodHandle printf = linker.downcallHandle(
linker.defaultLookup().find("printf").orElseThrow(),
linker.defaultLookup().findOrThrow("printf"),
FunctionDescriptor.of(JAVA_INT, ADDRESS, JAVA_INT, JAVA_INT, JAVA_INT),
Linker.Option.firstVariadicArg(1) // first int is variadic
);
@ -568,7 +568,7 @@ class Snippets {
Linker linker = Linker.nativeLinker();
SymbolLookup stdlib = linker.defaultLookup();
MethodHandle strlen = linker.downcallHandle(
stdlib.find("strlen").orElseThrow(),
stdlib.findOrThrow("strlen"),
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS)
);
@ -626,14 +626,14 @@ class Snippets {
void header() {
try (Arena arena = Arena.ofConfined()) {
SymbolLookup libGL = libraryLookup("libGL.so", arena); // libGL.so loaded here
MemorySegment glGetString = libGL.find("glGetString").orElseThrow();
MemorySegment glGetString = libGL.findOrThrow("glGetString");
// ...
} // libGL.so unloaded here
System.loadLibrary("GL"); // libGL.so loaded here
// ...
SymbolLookup libGL = loaderLookup();
MemorySegment glGetString = libGL.find("glGetString").orElseThrow();
MemorySegment glGetString = libGL.findOrThrow("glGetString");
Arena arena = Arena.ofAuto();
@ -647,7 +647,7 @@ class Snippets {
Linker nativeLinker = Linker.nativeLinker();
SymbolLookup stdlib = nativeLinker.defaultLookup();
MemorySegment malloc = stdlib.find("malloc").orElseThrow();
MemorySegment malloc = stdlib.findOrThrow("malloc");
}
}