Package jnr.ffi
Class Runtime
- java.lang.Object
-
- jnr.ffi.Runtime
-
- Direct Known Subclasses:
AbstractRuntime
public abstract class Runtime extends java.lang.Object
Access JNR runtime functionality.This class is needed by many classes to correctly initialize internal data structures, and each library loaded has its own instance of this class.
To obtain an instance of this class, use
getRuntime(Object)
on a loaded library.Example
public interface LibC { public long write(int fd, Pointer data, long len); } LibC library = LibraryLoader.create(LibC.class).load("c"); byte[] bytes = "Hello, World\n".getBytes("UTF-8"); // Use the loaded library's Runtime to allocate memory for the string jnr.ffi.Runtime runtime = jnr.ffi.Runtime.getRuntime(library); Pointer buffer = Memory.allocateDirect(runtime, bytes.length); // Copy the java string data to the native memory, then write the contents to STDOUT buffer.put(0, bytes, 0, bytes.length); library.write(1, buffer, bytes.length);
-
-
Constructor Summary
Constructors Constructor Description Runtime()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract long
addressMask()
Gets the address mask for this runtimeabstract int
addressSize()
Gets the size of an address (e.g.abstract java.nio.ByteOrder
byteOrder()
Gets the native byte order of the runtime.abstract Type
findType(NativeType type)
Looks up the runtime-specific type that corresponds to the pseudo-typeabstract Type
findType(TypeAlias type)
Looks up the runtime-specific type that corresponds to the type aliasabstract ClosureManager
getClosureManager()
Gets the native closure manager for this runtimeabstract int
getLastError()
Gets the last native error code.static java.util.List<NativeLibrary.LoadedLibraryData>
getLoadedLibraries()
Gets a list ofNativeLibrary.LoadedLibraryData
which represents all currently loaded libraries, or an empty list if none are loaded.abstract MemoryManager
getMemoryManager()
Gets the native memory manager for this runtimestatic Runtime
getRuntime(java.lang.Object library)
Returns the runtime associated with the library instance.static Runtime
getSystemRuntime()
Gets the global Runtime for the current FFI providerabstract boolean
isCompatible(Runtime other)
Indicates whether thisRuntime
instance is compatible with anotherRuntime
instance.abstract int
longSize()
Gets the size of a C long integer for this runtimeabstract <T> ObjectReferenceManager<T>
newObjectReferenceManager()
Creates a newObjectReferenceManager
abstract void
setLastError(int error)
Sets the native error code.
-
-
-
Method Detail
-
getSystemRuntime
public static Runtime getSystemRuntime()
Gets the global Runtime for the current FFI provider- Returns:
- The system runtime
-
getRuntime
public static Runtime getRuntime(java.lang.Object library)
Returns the runtime associated with the library instance.- Parameters:
library
- A loaded library instance as returned fromLibraryLoader.load()
- Returns:
- The runtime that loaded the library
-
getLoadedLibraries
public static java.util.List<NativeLibrary.LoadedLibraryData> getLoadedLibraries()
Gets a list ofNativeLibrary.LoadedLibraryData
which represents all currently loaded libraries, or an empty list if none are loaded.
A library is "loaded" if the native library's file (.so, .dylib, .dll etc) was opened and loaded into memory successfully, ie a call todlopen()
was successful. If you don't see a library here then either:- It failed to load and threw an
UnsatisfiedLinkError
- It has not yet loaded because it is being loaded lazily, ie, upon needing to be used, you can
disable this behavior by using
LibraryOption.LoadNow
at load time
- Returns:
- the list of
NativeLibrary.LoadedLibraryData
which represents all currently loaded libraries - See Also:
NativeLibrary.LoadedLibraryData
- It failed to load and threw an
-
findType
public abstract Type findType(NativeType type)
Looks up the runtime-specific type that corresponds to the pseudo-type- Parameters:
type
- The native pseudo-type.- Returns:
- A
Type
instance.
-
findType
public abstract Type findType(TypeAlias type)
Looks up the runtime-specific type that corresponds to the type alias- Parameters:
type
- the type alias.- Returns:
- A
Type
instance
-
getMemoryManager
public abstract MemoryManager getMemoryManager()
Gets the native memory manager for this runtime- Returns:
- The
MemoryManager
of the runtime
-
getClosureManager
public abstract ClosureManager getClosureManager()
Gets the native closure manager for this runtime- Returns:
- The
ClosureManager
of the runtime
-
newObjectReferenceManager
public abstract <T> ObjectReferenceManager<T> newObjectReferenceManager()
Creates a newObjectReferenceManager
- Type Parameters:
T
- the type parameter of theObjectReferenceManager
.- Returns:
- A new
ObjectReferenceManager
-
getLastError
public abstract int getLastError()
Gets the last native error code.This returns the errno value that was set at the time of the last native function call.
- Returns:
- The errno value.
-
setLastError
public abstract void setLastError(int error)
Sets the native error code.- Parameters:
error
- The value to set errno to.
-
addressMask
public abstract long addressMask()
Gets the address mask for this runtime- Returns:
- The address mask for the runtime.
-
addressSize
public abstract int addressSize()
Gets the size of an address (e.g. a pointer) for this runtime- Returns:
- The size of an address in bytes.
-
longSize
public abstract int longSize()
Gets the size of a C long integer for this runtime- Returns:
- The size of a C long integer in bytes.
-
byteOrder
public abstract java.nio.ByteOrder byteOrder()
Gets the native byte order of the runtime.- Returns:
- The byte order of the runtime
-
isCompatible
public abstract boolean isCompatible(Runtime other)
Indicates whether thisRuntime
instance is compatible with anotherRuntime
instance.This is not the same as calling
Object.equals(java.lang.Object)
- this method only indicates whether or not artifacts from the runtime (e.g. memory addresses) are compatible with artifacts from this one.This is mostly for internal use.
- Parameters:
other
- the other runtime to test for compatibility- Returns:
- true if the other runtime is compatible with this one
-
-