Class ForeignFunctions
java.lang.Object
oshi.ffm.ForeignFunctions
- Direct Known Subclasses:
CupsFunctions, IUnknownFFM, MacForeignFunctions, WindowsForeignFunctions
Base class providing utility methods for working with the Java Foreign Function and Memory (FFM) API.
Subclasses use these helpers to load native libraries and frameworks, create downcall handles, and read data from native memory segments.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Linker.OptionLinker option to captureerrnoafter a native call.static final StructLayoutLayout of the captured call state segment, containingerrno.protected static final VarHandleHandle to read theerrnofield from a captured call state segment.protected static final ArenaA shared auto arena used for library symbol lookups.protected static final LinkerThe native linker for the current platform.protected static final SymbolLookupSymbol lookup for libraries already loaded into the current process. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandledowncall(SymbolLookup lib, String symbol, MemoryLayout resLayout, MemoryLayout... argLayouts) Create a downcall handle for a symbol in a library.static byte[]getByteArrayFromNativePointer(MemorySegment pointer, long length, Arena arena) Copylengthbytes from a raw native pointer into a Java byte array.static intgetErrno(MemorySegment callState) Read theerrnovalue from a captured call state segment.static StringgetStringFromNativePointer(MemorySegment pointer, Arena arena) Read a null-terminated UTF-8 string from a raw native pointer.static MemorySegmentgetStructFromNativePointer(MemorySegment pointer, StructLayout layout, Arena arena) Reinterpret a raw native pointer as a struct of the given layout, scoped to the provided arena.static SymbolLookupLookup a library by name in the global arena.static SymbolLookuplibraryLookup(String libraryName) Lookup a native library by simple name, mapping it to the platform-specific filename (e.g.
-
Field Details
-
LINKER
The native linker for the current platform. -
LIBRARY_ARENA
A shared auto arena used for library symbol lookups. -
SYMBOL_LOOKUP
Symbol lookup for libraries already loaded into the current process. -
CAPTURE_CALL_STATE
Linker option to captureerrnoafter a native call. -
CAPTURED_STATE_LAYOUT
Layout of the captured call state segment, containingerrno. -
ERRNO_HANDLE
Handle to read theerrnofield from a captured call state segment.
-
-
Constructor Details
-
ForeignFunctions
protected ForeignFunctions()Not intended for instantiation.
-
-
Method Details
-
libraryLookup
Lookup a native library by simple name, mapping it to the platform-specific filename (e.g."c"→"libc.so"on Linux).- Parameters:
libraryName- the platform-independent library name- Returns:
- the symbol lookup for the library
-
getStructFromNativePointer
public static MemorySegment getStructFromNativePointer(MemorySegment pointer, StructLayout layout, Arena arena) Reinterpret a raw native pointer as a struct of the given layout, scoped to the provided arena.- Parameters:
pointer- the native pointerlayout- the struct layoutarena- the arena to scope the resulting segment to- Returns:
- a memory segment over the struct, or
nullif the pointer is null orMemorySegment.NULL
-
getStringFromNativePointer
Read a null-terminated UTF-8 string from a raw native pointer.- Parameters:
pointer- the native pointerarena- the arena to scope the reinterpreted segment to- Returns:
- the Java string, or
nullif the pointer is null orMemorySegment.NULL
-
getByteArrayFromNativePointer
Copylengthbytes from a raw native pointer into a Java byte array.- Parameters:
pointer- the native pointerlength- the number of bytes to copyarena- the arena to scope the reinterpreted segment to- Returns:
- the byte array, or
nullif the pointer is null orMemorySegment.NULL
-
lib
Lookup a library by name in the global arena.- Parameters:
name- the library name- Returns:
- the symbol lookup for the library
-
downcall
public static MethodHandle downcall(SymbolLookup lib, String symbol, MemoryLayout resLayout, MemoryLayout... argLayouts) Create a downcall handle for a symbol in a library.- Parameters:
lib- the symbol lookupsymbol- the symbol nameresLayout- the return layoutargLayouts- the argument layouts- Returns:
- the method handle
-
getErrno
Read theerrnovalue from a captured call state segment.- Parameters:
callState- the memory segment returned by a call made withCAPTURE_CALL_STATE- Returns:
- the
errnovalue
-