Package jnr.ffi
Class Runtime
- java.lang.Object
-
- jnr.ffi.Runtime
-
- Direct Known Subclasses:
AbstractRuntime
,InvalidRuntime
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);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Runtime.SingletonHolder
singleton holder for the default Runtime
-
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.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 this Runtime instance is compatible with another Runtime 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
-
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 this Runtime instance is compatible with another Runtime 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
-
-