CRuntime

public class CRuntime extends NativeRuntime

CRuntime.

NativeRuntime implementation used for supporting C features like structures and callbacks.

Fields

BOOLEAN_SIZE

public static final int BOOLEAN_SIZE

Native size of a boolean in bytes.

BYTE_SIZE

public static final int BYTE_SIZE

Native size of a byte in bytes.

CHAR_SIZE

public static final int CHAR_SIZE

Native size of a char in bytes.

DOUBLE_SIZE

public static final int DOUBLE_SIZE

Native size of a double in bytes.

FLOAT_SIZE

public static final int FLOAT_SIZE

Native size of a float in bytes.

INT_SIZE

public static final int INT_SIZE

Native size of an int in bytes.

LONG_SIZE

public static final int LONG_SIZE

Native size of a long in bytes.

NATIVE_LONG_SIZE

public static final int NATIVE_LONG_SIZE

Native size of a ‘native’ long in bytes.

NATIVE_WCHART_SIZE

public static final int NATIVE_WCHART_SIZE

Native size of a ‘native’ wchar_t in bytes.

POINTER_SIZE

public static final int POINTER_SIZE

Native size of a pointer in bytes.

SHORT_SIZE

public static final int SHORT_SIZE

Native size of a short in bytes.

Methods

allocBoolean

public static native long allocBoolean(int count)

Allocates boolean array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocByte

public static native long allocByte(int count)

Allocates byte array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocChar

public static native long allocChar(int count)

Allocates char array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocDouble

public static native long allocDouble(int count)

Allocates double array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocFloat

public static native long allocFloat(int count)

Allocates float array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocInt

public static native long allocInt(int count)

Allocates int array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocLong

public static native long allocLong(int count)

Allocates long array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

allocNativeCallback

public static native long allocNativeCallback(Object instance, Method method, long[] extra)

Construct a native callback from a Java method.

Also documented in CRuntime.h

Parameters:
  • instance – The Java instance we want to create native callback for
  • method – The Java method we want to create native callback from
  • extra – The out parameter used for storing extra informations
Returns:

The native callback

allocNativeObject

public static native long allocNativeObject(Class<? extends NativeObject> type, int count)

Allocate one continuous memory block with sufficient size for containing count instances of type.

Also documented in CRuntime.h

Parameters:
  • type – Java class of native object
  • count – Count of native object
Returns:

The pointer of the newly allocated space

allocPointer

public static native long allocPointer(int count)

Allocates space for count pointer.

Also documented in CRuntime.h

Parameters:
  • count – The length of the array we want to create
Returns:

The pointer of the allocated space

allocShort

public static native long allocShort(int count)

Allocates short array of size count.

Parameters:
  • count – The length of the array
Returns:

The pointer

cast

public static <T extends OpaquePtr> T cast(ConstVoidPtr ptr, java.lang.Class<T> cls)

Casts a void pointer to an opaque pointer.

Parameters:
  • <T> – The opaque pointer type class to cast to
  • ptr – The void pointer we want to cast
  • cls – The desired type of the class
Returns:

The new opaque pointer

cast

public static VoidPtr cast(OpaquePtr ptr)

Casts an opaque pointer to a void pointer.

Parameters:
  • ptr – The opaque pointer we want to cast
Returns:

The new void pointer

copyBooleanArray

public static native void copyBooleanArray(long dst, int startOffset, boolean[] array, int buffOffset, int length)

Copies boolean array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyByteArray

public static native void copyByteArray(long dst, int startOffset, byte[] array, int buffOffset, int length)

Copies byte array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyCharArray

public static native void copyCharArray(long dst, int startOffset, char[] array, int buffOffset, int length)

Copies char array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyDoubleArray

public static native void copyDoubleArray(long dst, int startOffset, double[] array, int buffOffset, int length)

Copies double array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFloatArray

public static native void copyFloatArray(long dst, int startOffset, float[] array, int buffOffset, int length)

Copies float array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeBooleanArray

public static native void copyFromNativeBooleanArray(boolean[] dst, int startOffset, long array, int buffOffset, int length)

Copies boolean array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeByteArray

public static native void copyFromNativeByteArray(byte[] dst, int startOffset, long array, int buffOffset, int length)

Copies byte array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeCharArray

public static native void copyFromNativeCharArray(char[] dst, int startOffset, long array, int buffOffset, int length)

Copies char array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeDoubleArray

public static native void copyFromNativeDoubleArray(double[] dst, int startOffset, long array, int buffOffset, int length)

Copies double array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeFloatArray

public static native void copyFromNativeFloatArray(float[] dst, int startOffset, long array, int buffOffset, int length)

Copies float array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeIntArray

public static native void copyFromNativeIntArray(int[] dst, int startOffset, long array, int buffOffset, int length)

Copies int array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeLongArray

public static native void copyFromNativeLongArray(long[] dst, int startOffset, long array, int buffOffset, int length)

Copies long array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyFromNativeShortArray

public static native void copyFromNativeShortArray(short[] dst, int startOffset, long array, int buffOffset, int length)

Copies short array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyIntArray

public static native void copyIntArray(long dst, int startOffset, int[] array, int buffOffset, int length)

Copies int array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyLongArray

public static native void copyLongArray(long dst, int startOffset, long[] array, int buffOffset, int length)

Copies long array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeBooleanArray

public static native void copyNativeBooleanArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native boolean array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeByteArray

public static native void copyNativeByteArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native byte array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeCharArray

public static native void copyNativeCharArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native char array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeDoubleArray

public static native void copyNativeDoubleArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native double array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeFloatArray

public static native void copyNativeFloatArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native float array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeIntArray

public static native void copyNativeIntArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native int array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeLongArray

public static native void copyNativeLongArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native long array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyNativeObject

public static native void copyNativeObject(Class<? extends NativeObject> type, long dst, long src)

Copies one instance of type from src to dst.

Also documented in CRuntime.h

Parameters:
  • type – The type of the native object
  • dst – The pointer that points to the memory space to where we want to copy
  • src – The pointer that points to the memory space from where we want to copy

copyNativeObjectArray

public static native <T extends NativeObject> long copyNativeObjectArray(T[] array)

Copies the native content of the NativeObject array to a new memory space.

Also documented in CRuntime.h

Parameters:
  • <T> – The element type of the Java array
  • array – The array we want to copy
Returns:

The pointer of the new native array

copyNativeShortArray

public static native void copyNativeShortArray(long dst, int startOffset, long array, int buffOffset, int length)

Copies native short array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

copyPointerArray

public static native void copyPointerArray(long dst, long[] array)

Copies the native content of the array to dst.

Also documented in CRuntime.h

Parameters:
  • dst – Where we want to copy
  • array – The array we want to copy

copyShortArray

public static native void copyShortArray(long dst, int startOffset, short[] array, int buffOffset, int length)

Copies short array.

Parameters:
  • dst – The pointer pointing to where we want to copy
  • startOffset – The offset for the destination
  • array – The array we want to copy
  • buffOffset – The offset of the pointer we want to copy
  • length – The length of the copy

createBooleanArray

public static native boolean[] createBooleanArray(long src, int length)

Creates a boolean array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createByteArray

public static native byte[] createByteArray(long src, int length)

Creates a byte array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createCharArray

public static native char[] createCharArray(long src, int length)

Creates a char array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createDoubleArray

public static native double[] createDoubleArray(long src, int length)

Creates a double array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createFloatArray

public static native float[] createFloatArray(long src, int length)

Creates a float array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createIntArray

public static native int[] createIntArray(long src, int length)

Creates a int array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createJavaCallback

public static native Object createJavaCallback(long extra)

Returns a Java instance from a given callback specified by it’s extra.

Works only with callbacks we create with allocNativeCallback(Object,Method,long[]).

Also documented in CRuntime.h

Parameters:
  • extra – Extra info pointer
Returns:

The Java instance

createJavaString

public static native String createJavaString(long pointer)

Constructs a Java string from a C string.

Also documented in CRuntime.h

Parameters:
  • pointer – The C string
Returns:

The Java string

createLongArray

public static native long[] createLongArray(long src, int length)

Creates a long array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createNativeString

public static native long createNativeString(String string)

Constructs a C string from a Java string.

Also documented in CRuntime.h

Parameters:
  • string – The Java string
Returns:

The native string

createNativeStringArray

public static native long createNativeStringArray(String[] strings)

Constructs a native string array from a Java string array.

The C string array will be constructed into one continuous memory block: <ptr1>|<ptr2>|…|<first char of first string>|<second char of first string>|…| <first char of second string>|… Because of this we can release every C string array we created by doing so with only one free call.

Also documented in CRuntime.h

Parameters:
  • strings – Java string array we want to convert
Returns:

The native string array

createShortArray

public static native short[] createShortArray(long src, int length)

Creates a short array from a pointer.

Parameters:
  • src – The pointer we want to create Java array object from
  • length – The length of the array
Returns:

The Java array.

createStrongPointer

public static Pointer createStrongPointer(long peer, boolean owned)

Creates a Pointer object.

If owned == true, then it will create a Pointer with strongReleaser as its releaser.

Parameters:
  • peer – The C pointer
  • owned – Is it a strong ownership?
Returns:

The created Pointer object

deallocNativeCallback

public static native void deallocNativeCallback(long extra)

Releases a native callback specified by it’s extra.

Works only with callbacks we create with allocNativeCallback(Object,Method,long[]).

Also documented in CRuntime.h

Parameters:
  • extra – Extra information needed for the callback

doRegistration

protected void doRegistration(Class<?> type)

Process the Java class.

This will register native handlers for native methods. Handles field getters, setters, checkers and C functions.

free

public static native void free(long peer)

Method for using C free function from java.

Also documented in CRuntime.h

Parameters:
  • peer – The memory space to release

getByteBufferPointer

public static native long getByteBufferPointer(ByteBuffer buffer)

Gets the native pointer from a ByteBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getCharBufferPointer

public static native long getCharBufferPointer(CharBuffer buffer)

Gets the native pointer from a CharBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getDefaultUnboxPolicy

public byte getDefaultUnboxPolicy()

Returns the default unbox policy for variadic arguments.

For C Runtime the policy is unboxing, because this runtime does not have any object types.

Returns:The default unbox policy.

getDoubleBufferPointer

public static native long getDoubleBufferPointer(DoubleBuffer buffer)

Gets the native pointer from a DoubleBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getFloatBufferPointer

public static native long getFloatBufferPointer(FloatBuffer buffer)

Gets the native pointer from a FloatBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getIntBufferPointer

public static native long getIntBufferPointer(IntBuffer buffer)

Gets the native pointer from an IntBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getLongBufferPointer

public static native long getLongBufferPointer(LongBuffer buffer)

Gets the native pointer from a LongBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

getShortBufferPointer

public static native long getShortBufferPointer(ShortBuffer buffer)

Gets the native pointer from a ShortBuffer.

Parameters:
  • buffer – The buffer
Returns:

The pointer

loadBoolean

public static native boolean loadBoolean(long src, int idx)

Loads a boolean.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded boolean value

loadByte

public static native byte loadByte(long src, int idx)

Loads a byte.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded byte value

loadChar

public static native char loadChar(long src, int idx)

Loads a char.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded char value

loadDouble

public static native double loadDouble(long src, int idx)

Loads a double.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded double value

loadFloat

public static native float loadFloat(long src, int idx)

Loads a float.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded float value

loadInt

public static native int loadInt(long src, int idx)

Loads an int.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded int value

loadLong

public static native long loadLong(long src, int idx)

Loads a long.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded long value

loadPointer

public static native long loadPointer(long ptr, int idx)

Loads a pointer from ptr with index idx.

Also documented in CRuntime.h

Parameters:
  • ptr – The pointer from where we want to load
  • idx – The index we want to offset with
Returns:

The loaded pointer.

loadShort

public static native short loadShort(long src, int idx)

Loads a short.

Parameters:
  • src – From we want to load
  • idx – Index of the loading
Returns:

The loaded short value

memcpy

public static native void memcpy(long dst, long src, int length)

Bride for native memcpy.

Also documented in CRuntime.java

Parameters:
  • dst – the pointer we want to copy to
  • src – the pointer we want to copy from
  • length – number of bytes to copy

memcpy

public static void memcpy(VoidPtr dst, ConstVoidPtr src, int length)

Bride for native memcpy.

Parameters:
  • dst – the pointer we want to copy to
  • src – the pointer we want to copy from
  • length – number of bytes to copy

memset

public static native void memset(long dst, int idx, int length, byte value)

Bride for native memset.

Also documented in CRuntime.java

Parameters:
  • dst – The pointer we want to do memset on
  • idx – The offset of the memsetting
  • length – The length of the memsetting
  • value – The value by we want to overwrite

memset

public static void memset(VoidPtr ptr, int idx, int length, byte value)

Bride for native memset.

Parameters:
  • ptr – the pointer we want to memset
  • idx – The offset of the memsetting
  • length – The length of the memsetting
  • value – The value by we want to overwrite

sizeOfLong

public static native int sizeOfLong()

Returns the long size for the current architecture.

Also documented in CRuntime.h

Returns:The long size

sizeOfNativeObject

public static native long sizeOfNativeObject(Class<? extends NativeObject> type)

Returns the native size of a NativeObject.

Also documented in CRuntime.h

Parameters:
  • type – The NativeObject we want to get the size of
Returns:

The size of the NativeObject

sizeOfPointer

public static native int sizeOfPointer()

Returns the pointer size for the current architecture.

Also documented in CRuntime.h

Returns:The pointer size

sizeOfWCharT

public static native int sizeOfWCharT()

Returns the wchar_t size for the current architecture.

Also documented in CRuntime.h

Returns:The wchar_t size

storeBoolean

public static native void storeBoolean(long dst, int idx, boolean value)

Stores a boolean value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The boolean value we want to store

storeByte

public static native void storeByte(long dst, int idx, byte value)

Stores a byte value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The byte value we want to store

storeChar

public static native void storeChar(long dst, int idx, char value)

Stores a char value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The char value we want to store

storeDouble

public static native void storeDouble(long dst, int idx, double value)

Stores a double value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The double value we want to store

storeFloat

public static native void storeFloat(long dst, int idx, float value)

Stores a float value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The float value we want to store

storeInt

public static native void storeInt(long dst, int idx, int value)

Stores an int value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The int value we want to store

storeLong

public static native void storeLong(long dst, int idx, long value)

Stores a long value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The long value we want to store

storePointer

public static native void storePointer(long dst, int idx, long value)

Stores value to ptr with index idx.

Also documented in CRuntime.h

Parameters:
  • dst – The pointer to where we want to store
  • idx – The index we want to offset with
  • value – The value we want to store

storeShort

public static native void storeShort(long dst, int idx, short value)

Stores a short value.

Parameters:
  • dst – The pointer pointing to where we want to store
  • idx – The index of the storing
  • value – The short value we want to store

tryToDisposeCallback

public void tryToDisposeCallback(Object callback)

Disposes a callback.

Removes the cached callback for a Java instance and does cleanup.

Parameters:
  • callback – The Java instance