How can I do to share the array elements between "C" and "Java"

E

Ejeep

Hi,all

I want to implement the function about this: c and java between JNI
can share the same data area. For example, when change the value of
the array in java, i can get the same value in c.

I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
jboolean isCopy)

But i find that the "isCopy" is JNI_TRUE which means that i get the
pointer which point to a copy,so i can't get the change
in Java later.

How can I do?Can i set "isCopy" with JNI_FALSE??

Thank u.
 
G

Gordon Beaton

I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray
array, jboolean isCopy)

But i find that the "isCopy" is JNI_TRUE which means that i get the
pointer which point to a copy,so i can't get the change in Java
later.

How can I do?Can i set "isCopy" with JNI_FALSE??

You can't make any meaningful change to "isCopy", it's purpose is just
to inform the caller.

However when you release the copy (which you must do), the changes
will propagate to the Java array: see ReleaseXxxArrayElements().

Or you could use GetPrimitiveArrayCritical() (and corresponding
Release...), which claim to make it "more likely" that you get a
direct pointer, but then there are additional restrictions on what you
can do while you are holding the array. YMMV.

/gordon

--
 
G

Gordon Beaton

You can't make any meaningful change to "isCopy", it's purpose is just
to inform the caller.

Agh! "Its purpose", of course. It's early here.

/gordon

--
 
P

Piotr Kobzda

E

Ejeep

Gordon said:
Or you could use GetPrimitiveArrayCritical() (and corresponding
Release...), which claim to make it "more likely" that you get a
direct pointer, but then there are additional restrictions on what you
can do while you are holding the array. YMMV.

/gordon

--

Thank u for you reply.
I use GetPrimitiveArrayCritical() directly in an example code ,and it
is useful.
But in my project ,it appears the error:"fixing up unaligned userspace
access"

How can i fix this???
 
G

Gordon Beaton

I use GetPrimitiveArrayCritical() directly in an example code ,and
it is useful. But in my project ,it appears the error:"fixing up
unaligned userspace access"

How can i fix this???

This is not a Java or even a JNI issue. You appear to invoking a
system call from your native code, and are passing a pointer that is
not sufficiently aligned for the specific datatype it refers to. An
example of this is attempting to refer to an "int" at an odd address,
when the architecture requires every "int" to be aligned at an even
address.

/gordon

--
 
E

Ejeep

You may also try to use a /direct buffers/ to achieve your goal.

See:
<http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions...>

piotr

Hi,Piotr
I try to use JNI function: GetDirectBufferAddress(),
GetDirectBufferCapacity()
and I get the return value with "NULL" and "-1".


test code:
------------------------------
JNIEXPORT void JNICALL Java_test_nativeInit
(JNIEnv *env, jclass obj, jintArray arr)
{

jlong jl;
char* ch;

ch = (char*)(*env)->GetDirectBufferAddress(env,arr);
jl = (*env)->GetDirectBufferCapacity(env,arr);

return;
}
 
E

Ejeep

This is not a Java or even a JNI issue. You appear to invoking a
system call from your native code, and are passing a pointer that is
not sufficiently aligned for the specific datatype it refers to. An
example of this is attempting to refer to an "int" at an odd address,
when the architecture requires every "int" to be aligned at an even
address.

/gordon

--

Thank you.

It's hard for me to check the "aligned" question.
The variable I use is the same type "int".
Have any methods to check?
 
G

Gordon Beaton

It's hard for me to check the "aligned" question.
The variable I use is the same type "int".
Have any methods to check?

No, but "proper" use of pointers won't cause this problem. It can only
occur when you cast pointers from one type to another, and they have
different alignment requirements (e.g. from void* or char* to int*).

Again, this isn't a Java or JNI issue.

/gordon

--
 
E

Ejeep

No, but "proper" use of pointers won't cause this problem. It can only
occur when you cast pointers from one type to another, and they have
different alignment requirements (e.g. from void* or char* to int*).

Again, this isn't a Java or JNI issue.

/gordon

--

I see.
it isn't a Java or JNI issue
Just don't find the solution.

Thank u very much
 
P

Piotr Kobzda

Ejeep said:
I try to use JNI function: GetDirectBufferAddress(),
GetDirectBufferCapacity()
and I get the return value with "NULL" and "-1".

Most likely your JVM do not support that. What is the JVM's version,
vendor, and the OS you're trying to use that with?


piotr
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top