GetPrimitiveArrayCritical
ReleasePrimitiveArrayCritical
void * GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy);
void ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode);
The semantics of these two functions are very similar to the existing
Get/Release<primitivetype>ArrayElements functions. If possible, the VM returns a pointer to the primitive array; otherwise, a copy is made. However, there are significant restrictions on how these functions can be used.
After calling
GetPrimitiveArrayCritical
, the native code should not run for an extended period of time before it callsReleasePrimitiveArrayCritical
. We must treat the code inside this pair of functions as running in a "critical region." Inside a critical region, native code must not call other JNI functions, or any system call that may cause the current thread to block and wait for another Java thread. (For example, the current thread must not call read
on a stream being written by another Java thread.)
These restrictions make it more likely that the native code will obtain an uncopied version of the array, even if the VM does not support pinning. For example, a VM may temporarily disable garbage collection when the native code is holding a pointer to an array obtained via
GetPrimitiveArrayCritical
.
Multiple pairs of
GetPrimtiveArrayCritical
and ReleasePrimitiveArrayCritical
may be nested. For example:
Example of GetPrimitiveArrayCritical
jint len = (*env)->GetArrayLength(env, arr1); jbyte *a1 = (*env)->GetPrimitiveArrayCritical(env, arr1, 0); jbyte *a2 = (*env)->GetPrimitiveArrayCritical(env, arr2, 0); /* We need to check in case the VM tried to make a copy. */ if (a1 == NULL || a2 == NULL) { ... /* out of memory exception thrown */ } memcpy(a1, a2, len); (*env)->ReleasePrimitiveArrayCritical(env, arr2, a2, 0); (*env)->ReleasePrimitiveArrayCritical(env, arr1, a1, 0);
Note that
GetPrimitiveArrayCritical
might still make a copy of the array if the VM internally represents arrays in a different format. Therefore we need to check its return value against NULL
for possible out of memory situations.LINKAGE (GetPrimitiveArrayCritical):
Linkage Index 222 in the JNIEnv interface function table.
LINKAGE (ReleasePrimitiveArrayCritical):
Linkage Index 223 in the JNIEnv interface function table.