Package io.github.jwharm.javagi.interop
Class Interop
java.lang.Object
io.github.jwharm.javagi.interop.Interop
The Interop class contains functionality for interoperability with native
code.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic MemorySegment
allocateNativeArray
(boolean[] array, boolean zeroTerminated, Arena arena) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(int[], boolean, Arena)
.static MemorySegment
allocateNativeArray
(byte[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of bytes.static MemorySegment
allocateNativeArray
(char[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of chars.static MemorySegment
allocateNativeArray
(double[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of doubles.static MemorySegment
allocateNativeArray
(float[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of floats.static MemorySegment
allocateNativeArray
(int[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of floats.static MemorySegment
allocateNativeArray
(long[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of longs.static MemorySegment
allocateNativeArray
(short[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of shorts.static MemorySegment
allocateNativeArray
(Proxy[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of pointers (from Proxy instances).static MemorySegment
allocateNativeArray
(Proxy[] array, MemoryLayout layout, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of structs (from Proxy instances).static MemorySegment
allocateNativeArray
(MemorySegment[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of memory addresses.static MemorySegment
allocateNativeArray
(String[][] strvs, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of arrays of strings (a Strv-array).static MemorySegment
allocateNativeArray
(String[] strings, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of strings (NULL
-terminated utf8char*
).static MemorySegment
allocateNativeString
(String string, SegmentAllocator alloc) Allocate a native string usingSegmentAllocator.allocateFrom(String)
, but returnMemorySegment.NULL
for anull
argument.static Arena
attachArena
(Arena arena, Object instance) Register a Cleaner 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
copy
(MemorySegment src, MemorySegment dst) First reinterpret the memory segments so they have equal size, then copysrc
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 MemorySegment
getAddress
(Object o, Arena arena) static MemorySegment[]
getAddressArrayFrom
(MemorySegment address, boolean free) Read an array of pointers from aNULL
-terminated array in native memory.static MemorySegment[]
getAddressArrayFrom
(MemorySegment address, int length, boolean free) Read an array of pointers with the requested length from 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, boolean free) 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, boolean free) Copy a boolean value from native memory.static byte[]
getByteArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) Read an array of bytes with the requested length from native memory.static byte[]
getByteArrayFrom
(MemorySegment address, Arena arena, boolean free) 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, boolean free) Copy a byte value from native memory.static char[]
getCharacterArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) 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, boolean free) Copy a char value from native memory.static double[]
getDoubleArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) 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, boolean free) Copy a double value from native memory.static float[]
getFloatArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) Read an array of floats with the requested length from native memory.static float
getFloatFrom
(MemorySegment address) Copy a float value from native memory.static float
getFloatFrom
(MemorySegment address, boolean free) Copy a float value from native memory.static int[]
getIntegerArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) Read an array of integers with the requested length from native memory.static int[]
getIntegerArrayFrom
(MemorySegment address, Arena arena, boolean free) 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, boolean free) Copy an integer value from native memory.static long[]
getLongArrayFrom
(MemorySegment address, long length, Arena arena, boolean free) Read an array of longs with the requested length from native memory.static long[]
getLongArrayFrom
(MemorySegment address, Arena arena, boolean free) 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, boolean free) 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, boolean free) Read an array of shorts with the requested length from native memory.static short[]
getShortArrayFrom
(MemorySegment address, Arena arena, boolean free) 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, boolean free) Copy a short value from native memory.static String[]
getStringArrayFrom
(MemorySegment address, boolean free) Read an array of Strings from aNULL
-terminated array in native memory.static String[]
getStringArrayFrom
(MemorySegment address, int length, boolean free) Read an array of Strings with the requested length from native memory.static String
getStringFrom
(MemorySegment address) Copy a Java string from native memory usingMemorySegment.getString()
.static String
getStringFrom
(MemorySegment address, boolean free) 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 String[][]
getStrvArrayFrom
(MemorySegment address, boolean free) 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 MemorySegment
reinterpret
(MemorySegment address, long newSize) Reinterpretaddress
tonewSize
iffnewSize
is larger than the current size ofaddress
.static 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
-
attachArena
Register a Cleaner 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.ofAuto()
.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, or
null
in case of a pointer toNULL
.
-
copy
First reinterpret the memory segments so they have equal size, then copysrc
intodst
.- Parameters:
src
- source memory segmentdst
- destination memory segment
-
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
-
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*
)free
- if the address must be freed- 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)free
- if the address must be freed- 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 bytefree
- if the address must be freed- 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 charfree
- if the address must be freed- 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 doublefree
- if the address must be freed- 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 floatfree
- if the address must be freed- 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 integerfree
- if the address must be freed- 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 longfree
- if the address must be freed- 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 shortfree
- if the address must be freed- Returns:
- the resulting short
-
getAddress
-
getStringArrayFrom
Read an array of Strings with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayfree
- if the strings and the array must be freed- Returns:
- array of Strings
-
getStringArrayFrom
Read an array of Strings from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmentfree
- if the strings and the array must be freed- Returns:
- array of Strings
-
getStrvArrayFrom
ReadNULL
-terminated arrays of Strings from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmentfree
- if the strings and the array must be freed- Returns:
- two-dimensional array of Strings
-
getAddressArrayFrom
Read an array of pointers with the requested length from native memory.- Parameters:
address
- address of the memory segmentlength
- length of the arrayfree
- if the array must be freed- Returns:
- array of pointers
-
getAddressArrayFrom
Read an array of pointers from aNULL
-terminated array in native memory.- Parameters:
address
- address of the memory segmentfree
- if the array must be freed- Returns:
- array of pointers
-
getBooleanArrayFrom
public static boolean[] getBooleanArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of booleans
-
getByteArrayFrom
public static byte[] getByteArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of bytes
-
getByteArrayFrom
Read aNULL
-terminated array of bytes from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopefree
- if the array must be freed- Returns:
- array of bytes
-
getCharacterArrayFrom
public static char[] getCharacterArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of chars
-
getDoubleArrayFrom
public static double[] getDoubleArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of doubles
-
getFloatArrayFrom
public static float[] getFloatArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of floats
-
getIntegerArrayFrom
public static int[] getIntegerArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of integers
-
getIntegerArrayFrom
Read aNULL
-terminated array of integers from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopefree
- if the array must be freed- Returns:
- array of integers
-
getLongArrayFrom
public static long[] getLongArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of longs
-
getLongArrayFrom
Read aNULL
-terminated array of longs from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopefree
- if the array must be freed- Returns:
- array of longs
-
getShortArrayFrom
public static short[] getShortArrayFrom(MemorySegment address, long length, Arena arena, boolean free) 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 scopefree
- if the array must be freed- Returns:
- array of shorts
-
getShortArrayFrom
Read aNULL
-terminated array of shorts from native memory.- Parameters:
address
- address of the memory segmentarena
- the memory scopefree
- if the array must be freed- 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, 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, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of strings (NULL
-terminated utf8char*
).- Parameters:
strings
- array of StringszeroTerminated
- whether to add aNULL
to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(String[][] strvs, boolean zeroTerminated, Arena arena) 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.arena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(boolean[] array, boolean zeroTerminated, Arena arena) Convert a boolean[] array into an int[] array, and callsallocateNativeArray(int[], boolean, Arena)
. 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 arrayarena
- the segment allocator for memory allocation- Returns:
- The memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of bytes.- Parameters:
array
- array of byteszeroTerminated
- when true, a (byte) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of chars.- Parameters:
array
- array of charszeroTerminated
- when true, a (char) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- whe memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(double[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of doubles.- Parameters:
array
- array of doubleszeroTerminated
- when true, a (double) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of floats.- Parameters:
array
- array of floatszeroTerminated
- when true, a (float) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of floats.- Parameters:
array
- array of floatszeroTerminated
- when true, a (int) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of longs.- Parameters:
array
- array of longszeroTerminated
- when true, a (long) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of shorts.- Parameters:
array
- array of shortszeroTerminated
- when true, a (short) 0 is appended to the arrayarena
- the segment allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
Allocate and initialize an (optionallyNULL
-terminated) array of pointers (from Proxy instances).- Parameters:
array
- array of Proxy instanceszeroTerminated
- whether to add aNULL
to the arrayarena
- 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, Arena arena) 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
arena
- the allocator for memory allocation- Returns:
- the memory segment of the native array
-
allocateNativeArray
public static MemorySegment allocateNativeArray(MemorySegment[] array, boolean zeroTerminated, Arena arena) Allocate and initialize an (optionallyNULL
-terminated) array of memory addresses.- Parameters:
array
- array of MemorySegmentszeroTerminated
- whether to add aNULL
to the arrayarena
- 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
-