accessing a Java int array from JNI

G

Guest

How can I access an encapsulated Java int array from a
native method, given the Object that declared it? ie, can
anyone replace the 3rd & 4th lines in the first native method
below with actual code that does the job?

Also, assuming that this problem is solvable, is it possible
to keep an array locked-down in the JVM beyond the scope
of the first native method that was called? Obviously, my
goal is to write a third native method that can be called by
the Java side whenever it wants to repopulate an array with
data available to the native code but only go through the over-
head of translating the address space one time. (Yes,
synchronization may be tricky; but for this app, I don't really
care how volitile the data appears to be on the Java side as
long as it gets updated *fast* and without cloning/copying.)

TIA,

// file Main.java

class MyObject
{
int myArray[] = new int[500000];
}

class Main
{
public void native doSomething (Object o);
public void native releaseArray ();

public static void main (String args[])
{
MyObject mo = new MyObject ();
mo.myArray[4] = 4321;
doSomething (mo);
// yada yada ...
releaseArray ();
}
}

// file myjni.cc

include "Main.h"

jbyte *data = NULL;
jarray array;

JNIEXPORT void JNICALL
Java_Main_doSomething (JNIEnv *env, jobject obj, jobject anObject)
{
jclass clazz = env -> GetObjectClass (anObject);
jfieldID fid = env -> GetFieldID (clazz, "myArray", "[I");

// HULP NEEDED HERE!
jniMagic hulp = env -> CallSomeJNImethodIhaventHeardAbout (fid);
array = env -> MoreHulpNeededHere (hulp);

int isCopy;
data = env -> GetPrimitiveArrayCritical (array, &isCopy);
if (data[4] > 4000)
printf ("yeah, verily!\n");
else
printf ("boo, hiss!\n");
}

JNIEXPORT void JNICALL
Java_Main_releaseArray (JNIEnv *env, jobject obj)
{ // is this legal and/or wise?
if (data != NULL)
{
env -> ReleasePrimitiveArrayCritical (array, data, JNI_ABORT);
data = NULL;
}
}
 
G

Gordon Beaton

How can I access an encapsulated Java int array from a native
method, given the Object that declared it? ie, can anyone replace
the 3rd & 4th lines in the first native method below with actual
code that does the job?

The JNI function you're looking for is GetObjectField() (arrays are
Objects).

Note that there is no need to pass your object as an explicit argument
to the native method not declared static. "this" is always passed
anyway (in your example, you get it twice).
Also, assuming that this problem is solvable, is it possible to keep
an array locked-down in the JVM beyond the scope of the first native
method that was called? Obviously, my goal is to write a third
native method that can be called by the Java side whenever it wants
to repopulate an array with data available to the native code but
only go through the over- head of translating the address space one
time. (Yes, synchronization may be tricky; but for this app, I don't
really care how volitile the data appears to be on the Java side as
long as it gets updated *fast* and without cloning/copying.)

Presumably you can hang on to your native copy as long as you want,
but realize that any changes you make won't propagate back to the
original array until you call ReleaseIntArrayElements(). Otherwise
maybe have a look at Get/SetIntArrayRegion() instead.

/gordon
 
C

Chris Uppal

Also, assuming that this problem is solvable, is it possible
to keep an array locked-down in the JVM beyond the scope
of the first native method that was called?

I'm not sure whether that is possible. It /might/ be, but there are issues.
The get/release JNI functions work with a jobject, and jobects are not valid
past the lifetime of the call to a native method, so you could not use the
/same/ jobject for both get and release. The JVM /might/ allow you to release
the array data in a separate call with a different jobject that "pointed" to
the same Java array, but I don't think there's any guarantee that it will work.
So at best it's implementation dependent. Unfortunately, I suspect that it
might be one of those things that /seem/ to work, but still fail intermitently.

You might want to review the recent thread "Passing large C buffers to Java
(via JNI) without copying?" for more options and opinions.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI and GetFieldID 12
JNI Error in passing array 0
JNI generic type of jobject 6
JNI Invocation API example 4
JNI 1
JNI return jobjectArray 7
Problem with JNI 7
JNI with multidimensional Byte Array 4

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top