Package org.javagi.interop
Class Interop
java.lang.Object
org.javagi.interop.Interop
The Interop class contains functionality for interoperability with native
code.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegment
allocateNativeArray
(boolean[] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(int[], boolean, SegmentAllocator)
.static MemorySegment
allocateNativeArray
(byte[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of bytes.static MemorySegment
allocateNativeArray
(char[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of chars.static MemorySegment
allocateNativeArray
(double[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of doubles.static MemorySegment
allocateNativeArray
(float[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of floats.static MemorySegment
allocateNativeArray
(int[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of floats.static MemorySegment
allocateNativeArray
(long[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of longs.static MemorySegment
allocateNativeArray
(short[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of shorts.static MemorySegment
allocateNativeArray
(MemorySegment[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of memory addresses.static MemorySegment
allocateNativeArray
(String[][] strvs, boolean zeroTerminated, SegmentAllocator alloc, SegmentAllocator elementAlloc) Allocate and initialize an (optionallyNULL
-terminated) array of arrays of strings (a Strv-array).static MemorySegment
allocateNativeArray
(String[] strings, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of strings (NULL
-terminated utf8char*
).static MemorySegment
allocateNativeArray
(Proxy[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of pointers (from Proxy instances).static MemorySegment
allocateNativeArray
(Proxy[] array, MemoryLayout layout, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of structs (from Proxy instances).static MemorySegment
allocateNativeString
(String string, SegmentAllocator alloc) Allocate a native string usingSegmentAllocator.allocateFrom(String)
, but returnMemorySegment.NULL
for anull
argument.static MemorySegment
allocateUnownedString
(String string) Allocate a native string usingg_malloc0()
.static Arena
attachArena
(Arena arena, Object instance) Register a Cleaner action that will close the arena when the instance is garbage-collected, coupling the lifetime of the arena to the lifetime of the instance.static void
checkNull
(MemorySegment pointer) Whenpointer
isnull
, orpointer
equalsMemorySegment.NULL
or reading an address frompointer
returnsMemorySegment.NULL
, raise aNullPointerException
.static void
copy
(MemorySegment src, MemorySegment dst, long size) Reinterpret both memory segments to the specified size and copysize
bytes fromsrc
intodst
.static MemorySegment
dereference
(MemorySegment pointer) Dereference a pointerstatic MethodHandle
downcallHandle
(MemorySegment symbol, FunctionDescriptor fdesc, Linker.Option... options) Create a method handle that is used to call the native function at the provided memory address.static MethodHandle
downcallHandle
(String name, FunctionDescriptor fdesc) Convenience method that callsdowncallHandle(String, FunctionDescriptor, boolean)
with variadic=false.static MethodHandle
downcallHandle
(String name, FunctionDescriptor fdesc, boolean variadic) Create a method handle that is used to call the native function with the provided name and function descriptor.static <T extends Enum<T> & Enumeration>
intenumSetToInt
(Set<T> set) Create a bitfield from the provided Set of enumsstatic void
freeGBytes
(MemorySegment address) Free a GBytes withg_bytes_unref()
static void
freeGString
(MemorySegment address) Free a GString (including the character data).static byte[]
fromGBytes
(MemorySegment address) Create a Java byte array from a GBytesstatic String
fromGString
(MemorySegment address, TransferOwnership transfer) Marshal a GString to a Java String.static MemorySegment
getAddress
(Object o, SegmentAllocator alloc) static MemorySegment[]
getAddressArrayFrom
(MemorySegment address, int length, TransferOwnership transfer) Read an array of pointers with the requested length from native memory.static MemorySegment[]
getAddressArrayFrom
(MemorySegment address, TransferOwnership transfer) Read an array of pointers from aNULL
-terminated array in native memory.static <T> T[]
getArrayFromIntPointer
(MemorySegment address, int length, Class<T> cls, Function<Integer, T> make) Read an array of integers from native memory, create a Java instance for each integer value with the provided constructor, and return an array of these instances.static boolean[]
getBooleanArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of booleans with the requested length from native memory.static boolean
getBooleanFrom
(MemorySegment address) Copy a boolean value from native memory.static boolean
getBooleanFrom
(MemorySegment address, TransferOwnership transfer) Copy a boolean value from native memory.static byte[]
getByteArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.static byte[]
getByteArrayFrom
(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of bytes from native memory.static byte
getByteFrom
(MemorySegment address) Copy a byte value from native memory.static byte
getByteFrom
(MemorySegment address, TransferOwnership transfer) Copy a byte value from native memory.static char[]
getCharacterArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of chars with the requested length from native memory.static char
getCharacterFrom
(MemorySegment address) Copy a char value from native memory.static char
getCharacterFrom
(MemorySegment address, TransferOwnership transfer) Copy a char value from native memory.static double[]
getDoubleArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.static double
getDoubleFrom
(MemorySegment address) Copy a double value from native memory.static double
getDoubleFrom
(MemorySegment address, TransferOwnership transfer) Copy a double value from native memory.static float[]
getFloatArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of floats with the requested length from native memory.static float[]
getFloatArrayFrom
(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of float from native memory.static float
getFloatFrom
(MemorySegment address) Copy a float value from native memory.static float
getFloatFrom
(MemorySegment address, TransferOwnership transfer) Copy a float value from native memory.static int[]
getIntegerArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of integers with the requested length from native memory.static int[]
getIntegerArrayFrom
(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of integers from native memory.static int
getIntegerFrom
(MemorySegment address) Copy an integer value from native memory.static int
getIntegerFrom
(MemorySegment address, TransferOwnership transfer) Copy an integer value from native memory.static long[]
getLongArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of longs with the requested length from native memory.static long[]
getLongArrayFrom
(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of longs from native memory.static long
getLongFrom
(MemorySegment address) Copy a long value from native memory.static long
getLongFrom
(MemorySegment address, TransferOwnership transfer) Copy a long value from native memory.static <T extends Proxy>
T[]getProxyArrayFrom
(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make) Read an array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.static <T extends Proxy>
T[]getProxyArrayFrom
(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make) Read aNULL
-terminated array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.static short[]
getShortArrayFrom
(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.static short[]
getShortArrayFrom
(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of shorts from native memory.static short
getShortFrom
(MemorySegment address) Copy a short value from native memory.static short
getShortFrom
(MemorySegment address, TransferOwnership transfer) Copy a short value from native memory.static String[]
getStringArrayFrom
(MemorySegment address, int length, TransferOwnership transfer) Read an array of Strings with the requested length from native memory.static String[]
getStringArrayFrom
(MemorySegment address, TransferOwnership transfer) Read an array of Strings from aNULL
-terminated array in native memory.static String
getStringFrom
(MemorySegment address) Copy a Java string from native memory usingMemorySegment.getString()
.static String
getStringFrom
(MemorySegment address, TransferOwnership transfer) Copy a Java string from native memory usingMemorySegment.getString()
.static <T extends Proxy>
T[]getStructArrayFrom
(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read an array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.static <T extends Proxy>
T[]getStructArrayFrom
(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read aNULL
-terminated array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.static String[][]
getStrvArrayFrom
(MemorySegment address, int length, TransferOwnership transfer) Readlength
arrays of Strings from native memory.static String[][]
getStrvArrayFrom
(MemorySegment address, TransferOwnership transfer) ReadNULL
-terminated arrays of Strings from aNULL
-terminated array in native memory.static Type
Get a GType by executing the provided get-type function.static int[]
getValues
(Enumeration[] array) Convert an array of enums into an array of integers.static <T extends Enum<T> & Enumeration>
EnumSet<T> intToEnumSet
(Class<T> cls, Function<Integer, T> make, int bitfield) Create an EnumSet of class `cls` from the provided bitfield.static void
loadLibrary
(String name) Load the specified library usingSymbolLookup.libraryLookup(String, Arena)
RESTRICTED.static boolean
static SegmentAllocator
Create a SegmentAllocator that usesmalloc()
to allocate memory.static MemorySegment
newGArray
(int elementSize) Create a new empty GArray.static MemorySegment
newGArray
(MemorySegment data, long length, long elementSize) Create a new GArray with the provided data.static MemorySegment
Create a new empty GPtrArray.static MemorySegment
newGPtrArray
(MemorySegment data, long length) Create a new empty GPtrArray.static MemorySegment
reinterpret
(MemorySegment address, long newSize) Reinterpretaddress
tonewSize
iffnewSize
is larger than the current size ofaddress
.static boolean
Null-safe retrieve the value of a Boolean Outstatic byte
Null-safe retrieve the value of a Byte Outstatic char
toCharacter
(@Nullable Out<@Nullable Character> val) Null-safe retrieve the value of a Character Outstatic double
Null-safe retrieve the value of a Double Outstatic float
Null-safe retrieve the value of a Float Outstatic MemorySegment
toGBytes
(byte[] data) Create a GBytes from a Java byte arraystatic MemorySegment
Marshal a Java String to a GString.static int
Null-safe retrieve the value of an Integer Outstatic long
Null-safe retrieve the value of a Long Outstatic short
Null-safe retrieve the value of a Short Outstatic MethodHandle
upcallHandle
(MethodHandles.Lookup lookup, Class<?> cls, FunctionDescriptor descriptor) Create a method handle for theupcall
method in the provided class.static MethodHandle
upcallHandle
(MethodHandles.Lookup lookup, Class<?> cls, String name, FunctionDescriptor descriptor) Create a method handle for a method in the provided class.
-
Constructor Details
-
Interop
public Interop()
-
-
Method Details
-
longAsInt
public static boolean longAsInt() -
loadLibrary
Load the specified library usingSymbolLookup.libraryLookup(String, Arena)
RESTRICTED.- Parameters:
name
- the name of the library
-
downcallHandle
Convenience method that callsdowncallHandle(String, FunctionDescriptor, boolean)
with variadic=false.- Parameters:
name
- name of the native functionfdesc
- function descriptor of the native function- Returns:
- the MethodHandle
-
downcallHandle
Create a method handle that is used to call the native function with the provided name and function descriptor.- Parameters:
name
- name of the native functionfdesc
- function descriptor of the native functionvariadic
- whether the function has varargs- Returns:
- the newly created MethodHandle
-
downcallHandle
public static MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor fdesc, Linker.Option... options) Create a method handle that is used to call the native function at the provided memory address.- Parameters:
symbol
- memory address of the native functionfdesc
- function descriptor of the native functionoptions
- linker options- Returns:
- the newly created MethodHandle
-
upcallHandle
public static MethodHandle upcallHandle(MethodHandles.Lookup lookup, Class<?> cls, FunctionDescriptor descriptor) Create a method handle for theupcall
method in the provided class.- Parameters:
cls
- the callback classdescriptor
- the function descriptor for the native function- Returns:
- a method handle to use when creating an upcall stub
-
upcallHandle
public static MethodHandle upcallHandle(MethodHandles.Lookup lookup, Class<?> cls, String name, FunctionDescriptor descriptor) Create a method handle for a method in the provided class.- Parameters:
cls
- the callback classname
- the name of the callback methoddescriptor
- the function descriptor for the native function- Returns:
- a method handle to use when creating an upcall stub
-
mallocAllocator
Create a SegmentAllocator that usesmalloc()
to allocate memory.The returned allocator currently calls
GLib.tryMalloc0(long)
, so the allocated memory is zero-initialized, but they may change in the future.It is up to the user of this allocator to release the allocated memory, for example with
GLib.free(java.lang.foreign.MemorySegment)
.- Returns:
- the newly created SegmentAllocator
-
attachArena
Register a Cleaner action that will close the arena when the instance is garbage-collected, coupling the lifetime of the arena to the lifetime of the instance.- Parameters:
arena
- a memory arena that can be closed (normallyArena.ofConfined()
orArena.ofShared()
.instance
- an object- Returns:
- the arena (for method chaining)
-
reinterpret
Reinterpretaddress
tonewSize
iffnewSize
is larger than the current size ofaddress
.- Parameters:
address
- a MemorySegmentnewSize
- new size for the MemorySegment- Returns:
- the same MemorySegment reinterpreted to at least
newSize
-
dereference
Dereference a pointer- Parameters:
pointer
- the pointer to dereference- Returns:
- the value of the pointer
- Throws:
NullPointerException
- when the pointer is null or references null
-
copy
Reinterpret both memory segments to the specified size and copysize
bytes fromsrc
intodst
.- Parameters:
src
- source memory segmentdst
- destination memory segmentsize
- the number of bytes to copy
-
checkNull
Whenpointer
isnull
, orpointer
equalsMemorySegment.NULL
or reading an address frompointer
returnsMemorySegment.NULL
, raise aNullPointerException
.- Parameters:
pointer
- the pointer to check- Throws:
NullPointerException
- when the check failed
-
getType
-
allocateNativeString
Allocate a native string usingSegmentAllocator.allocateFrom(String)
, but returnMemorySegment.NULL
for anull
argument.- Parameters:
string
- the string to allocate as a native string (utf8 char*) (can benull
)alloc
- the segment allocator to use- Returns:
- the allocated MemorySegment with the native utf8 string, or
MemorySegment.NULL
-
allocateUnownedString
- Parameters:
string
- the string to allocate as a native string (utf8 char*) (can benull
)- Returns:
- the allocated MemorySegment with the native utf8 string, that
must be freed with
g_free()
, orMemorySegment.NULL
-
getStringFrom
Copy a Java string from native memory usingMemorySegment.getString()
. If an error occurs or when the native address is NULL, null is returned.The native memory is not freed.
- Parameters:
address
- the memory address of the native String (aNULL
-terminatedchar*
)- Returns:
- a String or null
-
getStringFrom
Copy a Java string from native memory usingMemorySegment.getString()
. If an error occurs or when the native address is NULL, null is returned.- Parameters:
address
- the memory address of the native String (aNULL
-terminatedchar*
)transfer
- ownership transfer- Returns:
- a String or null
-
getBooleanFrom
Copy a boolean value from native memory. If the native address is NULL or contains the value 0, false is returned; else, true is returned.- Parameters:
address
- the memory address of the native boolean (0 is false, any other value is true)- Returns:
- the resulting boolean
-
getBooleanFrom
Copy a boolean value from native memory. If the native address is NULL or contains the value 0, false is returned; else, true is returned.- Parameters:
address
- the memory address of the native boolean (0 is false, any other value is true)transfer
- ownership transfer- Returns:
- the resulting boolean
-
getByteFrom
Copy a byte value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native byte- Returns:
- the resulting byte
-
getByteFrom
Copy a byte value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native bytetransfer
- ownership transfer- Returns:
- the resulting byte
-
getCharacterFrom
Copy a char value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native char- Returns:
- the resulting char
-
getCharacterFrom
Copy a char value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native chartransfer
- ownership transfer- Returns:
- the resulting char
-
getDoubleFrom
Copy a double value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native double- Returns:
- the resulting double
-
getDoubleFrom
Copy a double value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native doubletransfer
- ownership transfer- Returns:
- the resulting double
-
getFloatFrom
Copy a float value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native float- Returns:
- the resulting float
-
getFloatFrom
Copy a float value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native floattransfer
- ownership transfer- Returns:
- the resulting float
-
getIntegerFrom
Copy an integer value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native integer- Returns:
- the resulting integer
-
getIntegerFrom
Copy an integer value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native integertransfer
- ownership transfer- Returns:
- the resulting integer
-
getLongFrom
Copy a long value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native long- Returns:
- the resulting long
-
getLongFrom
Copy a long value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native longtransfer
- ownership transfer- Returns:
- the resulting long
-
getShortFrom
Copy a short value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native short- Returns:
- the resulting short
-
getShortFrom
Copy a short value from native memory. If the native address is NULL, 0 is returned.- Parameters:
address
- the memory address of the native shorttransfer
- ownership transfer- Returns:
- the resulting short
-
getAddress
-
getStringArrayFrom
public static String[] getStringArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Read an array of Strings with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arraytransfer
- ownership transfer- Returns:
- array of Strings
-
getStringArrayFrom
Read an array of Strings from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmenttransfer
- ownership transfer- Returns:
- array of Strings
-
getStrvArrayFrom
ReadNULL
-terminated arrays of Strings from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmenttransfer
- ownership transfer- Returns:
- two-dimensional array of Strings
-
getStrvArrayFrom
public static String[][] getStrvArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Readlength
arrays of Strings from native memory.- Parameters:
address
- address of the memory segmentlength
- the length of the arraytransfer
- ownership transfer- Returns:
- two-dimensional array of Strings
-
getAddressArrayFrom
public static MemorySegment[] getAddressArrayFrom(MemorySegment address, int length, TransferOwnership transfer) Read an array of pointers with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arraytransfer
- ownership transfer- Returns:
- array of pointers
-
getAddressArrayFrom
public static MemorySegment[] getAddressArrayFrom(MemorySegment address, TransferOwnership transfer) Read an array of pointers from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmenttransfer
- ownership transfer- Returns:
- array of pointers
-
getBooleanArrayFrom
public static boolean[] getBooleanArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of booleans with the requested length from native memory. The array is read from native memory as an array of integers with value 1 or 0, and converted to booleans with 1 = true and 0 = false.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of booleans
-
getByteArrayFrom
public static byte[] getByteArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of bytes with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of bytes
-
getByteArrayFrom
public static byte[] getByteArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of bytes from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of bytes
-
getCharacterArrayFrom
public static char[] getCharacterArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of chars with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of chars
-
getDoubleArrayFrom
public static double[] getDoubleArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of doubles with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of doubles
-
getFloatArrayFrom
public static float[] getFloatArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of floats with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of floats
-
getFloatArrayFrom
public static float[] getFloatArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of float from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of floats
-
getIntegerArrayFrom
public static int[] getIntegerArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of integers with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of integers
-
getIntegerArrayFrom
public static int[] getIntegerArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of integers from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of integers
-
getLongArrayFrom
public static long[] getLongArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of longs with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of longs
-
getLongArrayFrom
public static long[] getLongArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of longs from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of longs
-
getShortArrayFrom
public static short[] getShortArrayFrom(MemorySegment address, long length, Arena arena, TransferOwnership transfer) Read an array of shorts with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of shorts
-
getShortArrayFrom
public static short[] getShortArrayFrom(MemorySegment address, Arena arena, TransferOwnership transfer) Read aNULL
-terminated array of shorts from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopetransfer
- ownership transfer- Returns:
- array of shorts
-
getProxyArrayFrom
public static <T extends Proxy> T[] getProxyArrayFrom(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make) Read aNULL
-terminated array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.- Type Parameters:
T
- the type of the Proxy instances- Parameters:
address
- address of the memory segmentcls
- class of the Proxy typemake
- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getProxyArrayFrom
public static <T extends Proxy> T[] getProxyArrayFrom(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make) Read an array of memory addresses from native memory, create a Proxy instance for each address, and return an array of Proxy instances.- Type Parameters:
T
- the type of the Proxy instances- Parameters:
address
- address of the memory segmentlength
- length of the arraycls
- class of the Proxy typemake
- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getStructArrayFrom
public static <T extends Proxy> T[] getStructArrayFrom(MemorySegment address, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read aNULL
-terminated array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances. The array must be terminated by a completely null-filled struct.- Type Parameters:
T
- the type of the Proxy instances- Parameters:
address
- address of the memory segmentcls
- class of the Proxy typemake
- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getStructArrayFrom
public static <T extends Proxy> T[] getStructArrayFrom(MemorySegment address, int length, Class<T> cls, Function<MemorySegment, T> make, MemoryLayout layout) Read an array of structs from native memory, create a Proxy instance for each struct, and return an array of Proxy instances.- Type Parameters:
T
- the type of the Proxy instances- Parameters:
address
- address of the memory segmentlength
- length of the arraycls
- class of the Proxy typemake
- constructor of the Proxy type- Returns:
- array of Proxy instances
-
getArrayFromIntPointer
public static <T> T[] getArrayFromIntPointer(MemorySegment address, int length, Class<T> cls, Function<Integer, T> make) Read an array of integers from native memory, create a Java instance for each integer value with the provided constructor, and return an array of these instances. This is used to create an array of Enumeration or Bitfield objects from a native integer array.- Type Parameters:
T
- the type to construct- Parameters:
address
- address of the memory segmentlength
- length of the arraycls
- class that will be returned in the arraymake
- constructor to create the instances- Returns:
- array of constructed instances
-
allocateNativeArray
public static MemorySegment allocateNativeArray(String[] strings, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of strings (NULL
-terminated utf8char*
).- Parameters:
strings
- array of StringszeroTerminated
- whether to add aNULL
to the arrayalloc
- the segment allocator for the array- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(String[][] strvs, boolean zeroTerminated, SegmentAllocator alloc, SegmentAllocator elementAlloc) Allocate and initialize an (optionallyNULL
-terminated) array of arrays of strings (a Strv-array).- Parameters:
strvs
- the array of String arrayszeroTerminated
- whether to add aNULL
to the array. The embedded arrays are alwaysNULL
-terminated.alloc
- the segment allocator for the arrayelementAlloc
- the segment allocator for the array elements- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(boolean[] array, boolean zeroTerminated, SegmentAllocator alloc) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(int[], boolean, SegmentAllocator)
. Each boolean value "true" is converted 1, boolean value "false" to 0.- Parameters:
array
- array of booleanszeroTerminated
- when true, an (int) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- The memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(byte[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of bytes.- Parameters:
array
- array of byteszeroTerminated
- when true, a (byte) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(char[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of chars.- Parameters:
array
- array of charszeroTerminated
- when true, a (char) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- whe memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(double[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of doubles.- Parameters:
array
- array of doubleszeroTerminated
- when true, a (double) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(float[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of floats.- Parameters:
array
- array of floatszeroTerminated
- when true, a (float) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(int[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of floats.- Parameters:
array
- array of floatszeroTerminated
- when true, a (int) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
newGArray
Create a new empty GArray.- Parameters:
elementSize
- element size- Returns:
- the newly created GArray
-
newGArray
Create a new GArray with the provided data.- Parameters:
data
- memory segment containing the array datalength
- number of array elementselementSize
- element size- Returns:
- the newly created GArray
-
newGPtrArray
Create a new empty GPtrArray.- Returns:
- the newly create GPtrArray
-
newGPtrArray
Create a new empty GPtrArray.- Returns:
- the newly create GPtrArray
-
allocateNativeArray
public static MemorySegment allocateNativeArray(long[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of longs.- Parameters:
array
- array of longszeroTerminated
- when true, a (long) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(short[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of shorts.- Parameters:
array
- array of shortszeroTerminated
- when true, a (short) 0 is appended to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(Proxy[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of pointers (from Proxy instances).- Parameters:
array
- array of Proxy instanceszeroTerminated
- whether to add aNULL
to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(Proxy[] array, MemoryLayout layout, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of structs (from Proxy instances). The actual struct contents (not the pointers) are copied into the array.- Parameters:
array
- array of Proxy instanceslayout
- the memory layout of the structzeroTerminated
- whether to terminate the array by a struct with all members beingNULL
alloc
- the allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(MemorySegment[] array, boolean zeroTerminated, SegmentAllocator alloc) Allocate and initialize an (optionallyNULL
-terminated) array of memory addresses.- Parameters:
array
- array of MemorySegmentszeroTerminated
- whether to add aNULL
to the arrayalloc
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
intToEnumSet
public static <T extends Enum<T> & Enumeration> EnumSet<T> intToEnumSet(Class<T> cls, Function<Integer, T> make, int bitfield) Create an EnumSet of class `cls` from the provided bitfield. Undefined flags are logged (with levelLogLevelFlags.LEVEL_WARNING
) and ignored.- Type Parameters:
T
- an enum implementing the Java-GI Enumeration interface- Parameters:
cls
- the class of the enummake
- function that will construct an enum from one flag valuebitfield
- the integer containing the bitfield- Returns:
- an EnumSet containing the enum values as set in the bitfield
-
enumSetToInt
Create a bitfield from the provided Set of enums- Type Parameters:
T
- an enum implementing the Java-GI Enumeration interface- Parameters:
set
- the set of enums- Returns:
- the resulting bitfield
-
getValues
Convert an array of enums into an array of integers.- Parameters:
array
- an array of enums- Returns:
- an array containing the integer values of the provided Enumeration instances
-
toGBytes
Create a GBytes from a Java byte array- Parameters:
data
- the Java byte array- Returns:
- the GBytes
-
fromGBytes
Create a Java byte array from a GBytes- Parameters:
address
- the memory address of the GBytes- Returns:
- the Java byte array
-
freeGBytes
Free a GBytes withg_bytes_unref()
- Parameters:
address
- the address of the GBytes to free
-
fromGString
Marshal a GString to a Java String.- Parameters:
address
- address of a GStringtransfer
- if not NONE, the GString is freed- Returns:
- the Java String
-
toGString
Marshal a Java String to a GString.- Parameters:
string
- a Java String- Returns:
- the address of the GString
-
freeGString
Free a GString (including the character data).- Parameters:
address
- address of a GString
-
toBoolean
-
toByte
-
toCharacter
-
toDouble
-
toFloat
-
toInteger
-
toLong
-
toShort
-