JamaicaVM -- User Documentation: The Virtual Machine for Real-time and Embedded Systems | ||
---|---|---|
Prev | Chapter 9. The Jamaica Binary Interface | Next |
Arrays that are passed to JBI code are passed as values of type jamaica_ref. The array length can be accessed through the macro JAMAICA_GET_ARRAY_LENGTH(array), while the following macros permit reading of array elements.
JAMAICA_GET_REF_ARRAY(array,index,result) store reference array element at index in result. result is of type jamaica_ref.
JAMAICA_GET_ARRAY1(array,index,result) store boolean array element at index in result. result is of type jamaica_int32.
JAMAICA_GET_ARRAY8(array,index,result) store byte array element at index in result. result is of type jamaica_int32.
JAMAICA_GET_ARRAY16(array,index,result) store short or character array element at index in result. result is of type jamaica_int32.
JAMAICA_GET_ARRAY32(array,index,result) store int or float array element at index in result. result is of type jamaica_value32.
JAMAICA_GET_ARRAY64(array,index,result) store long or double array element at index in result. result is of type jamaica_value64.
A corresponding set of macros is available to write array elements.
JAMAICA_SET_REF_ARRAY(ct, array,index,value) write reference array element at index from value. value is of type jamaica_ref. Note that this macro requires a reference to the current thread structure (of type jamaica_thread *) for the GC's write barrier code to be performed.
JAMAICA_SET_ARRAY1(array,index,value) write boolean array element at index from value. value is of type jamaica_int32.
JAMAICA_SET_ARRAY8(array,index,value) write byte array element at index from value. value is of type jamaica_int32.
JAMAICA_SET_ARRAY16(array,index,value) write short or character array element at index from value. value is of type jamaica_int32.
JAMAICA_SET_ARRAY32(array,index,value) write int or float array element at index from value. value is of type jamaica_value32.
JAMAICA_SET_ARRAY64(array,index,value) write long or double array element at index from value. value is of type jamaica_value64.
Example code that sums up the elements of an integer array within native code may look like this C routine.
jamaica_int32 Jam_Test_sum( jamaica_thread *ct, jamaica_ref array) { jamaica_int32 Result, len, i, v; /* protect array from being GC'ed */ Jamaica_SaveLocal(ct,array); { Result = 0; len = JAMAICA_GET_ARRAY_LENGTH(array); for(i=0; i<len; i++) { /* permit thread switch */ JAMAICA_SYNCHRONIZATION_POINT(ct); JAMAICA_GET_ARRAY32(array,i,v); Result = Result + v; } } Jamaica_ReleaseLocal(ct); /* array */ return Result; } |
The corresponding declaration in the Java class Test.java is
public static native int sum(int[] a); |