A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/foreign/SymbolLookup.html below:

SymbolLookup (Java SE 21 & JDK 21)

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
SymbolLookup is a preview API of the Java platform.

A

symbol lookup

retrieves the address of a symbol in one or more libraries. A symbol is a named entity, such as a function or a global variable.

A symbol lookup is created with respect to a particular library (or libraries). Subsequently, the find(String) method takes the name of a symbol and returns the address of the symbol in that library.

The address of a symbol is modelled as a zero-length memory segmentPREVIEW. The segment can be used in different ways:

Obtaining a symbol lookup

The factory methods

libraryLookup(String, Arena)

and

libraryLookup(Path, Arena)

create a symbol lookup for a library known to the operating system. The library is specified by either its name or a path. The library is loaded if not already loaded. The symbol lookup, which is known as a

library lookup

, and its lifetime is controlled by an

arenaPREVIEW

. For instance, if the provided arena is a confined arena, the library associated with the symbol lookup is unloaded when the confined arena is

closedPREVIEW

:

 try (Arena arena = Arena.ofConfined()) {
     SymbolLookup libGL = SymbolLookup.libraryLookup("libGL.so", arena); // libGL.so loaded here
     MemorySegment glGetString = libGL.find("glGetString").orElseThrow();
     ...
 } //  libGL.so unloaded here

If a library was previously loaded through JNI, i.e., by System.load(String) or System.loadLibrary(String), then the library was also associated with a particular class loader. The factory method loaderLookup() creates a symbol lookup for all the libraries associated with the caller's class loader:

System.loadLibrary("GL"); // libGL.so loaded here
...
SymbolLookup libGL = SymbolLookup.loaderLookup();
MemorySegment glGetString = libGL.find("glGetString").orElseThrow();

This symbol lookup, which is known as a

loader lookup

, is dynamic with respect to the libraries associated with the class loader. If other libraries are subsequently loaded through JNI and associated with the class loader, then the loader lookup will expose their symbols automatically.

Note that a loader lookup only exposes symbols in libraries that were previously loaded through JNI, i.e., by System.load(String) or System.loadLibrary(String). A loader lookup does not expose symbols in libraries that were loaded in the course of creating a library lookup:

 libraryLookup("libGL.so", arena).find("glGetString").isPresent(); // true
 loaderLookup().find("glGetString").isPresent(); // false

Note also that a library lookup for library

L

exposes symbols in

L

even if

L

was previously loaded through JNI (the association with a class loader is immaterial to the library lookup):

 System.loadLibrary("GL"); // libGL.so loaded here
 libraryLookup("libGL.so", arena).find("glGetString").isPresent(); // true

Finally, each LinkerPREVIEW provides a symbol lookup for libraries that are commonly used on the OS and processor combination supported by that LinkerPREVIEW. This symbol lookup, which is known as a default lookup, helps clients to quickly find addresses of well-known symbols. For example, a LinkerPREVIEW for Linux/x64 might choose to expose symbols in libc through the default lookup:

 Linker nativeLinker = Linker.nativeLinker();
 SymbolLookup stdlib = nativeLinker.defaultLookup();
 MemorySegment malloc = stdlib.find("malloc").orElseThrow();

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4