Package jnr.ffi

Class 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 Detail

      • Runtime

        public Runtime()
    • 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 from LibraryLoader.load()
        Returns:
        The runtime that loaded the library
      • getLoadedLibraries

        public static java.util.List<NativeLibrary.LoadedLibraryData> getLoadedLibraries()
        Gets a list of NativeLibrary.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 to dlopen() 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
        When a library is unloaded (all references to your interface mapping have been GC'd), calling this method again will reflect the unload and you will no longer see the unloaded library.
        Returns:
        the list of NativeLibrary.LoadedLibraryData which represents all currently loaded libraries
        See Also:
        NativeLibrary.LoadedLibraryData
      • 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 new ObjectReferenceManager
        Type Parameters:
        T - the type parameter of the ObjectReferenceManager.
        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