Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How can I do to share the array elements between "C" and "Java"

Reply
Thread Tools

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

 
 
Ejeep
Guest
Posts: n/a
 
      11-06-2007
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.

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-06-2007
On Mon, 05 Nov 2007 18:35:57 -0800, Ejeep wrote:
> 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

--
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      11-06-2007
On 06 Nov 2007 06:59:14 GMT, Gordon Beaton wrote:
> 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

--
 
Reply With Quote
 
Piotr Kobzda
Guest
Posts: n/a
 
      11-06-2007
Ejeep wrote:

> 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)


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.html#nio_support>


piotr
 
Reply With Quote
 
Ejeep
Guest
Posts: n/a
 
      11-06-2007

Gordon Beaton wrote:

> 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???

 
Reply With Quote
 
Ejeep
Guest
Posts: n/a
 
      11-06-2007
On Nov 6, 9:15 pm, Piotr Kobzda <(E-Mail Removed)> wrote:

> 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


Thank you for you reply.

I will try it.
By the way,the sun's spec doesn't contain an example.
Sign!~~~

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      11-06-2007
On Tue, 06 Nov 2007 06:11:46 -0800, Ejeep wrote:
> 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

--
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      11-06-2007
Gordon Beaton wrote:
> On 06 Nov 2007 06:59:14 GMT, Gordon Beaton wrote:
>> 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.


You're my hero!

--
Lew
 
Reply With Quote
 
Ejeep
Guest
Posts: n/a
 
      11-07-2007
On Nov 6, 9:15 pm, Piotr Kobzda <(E-Mail Removed)> wrote:
> Ejeep wrote:
> > 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)

>
> 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;
}

 
Reply With Quote
 
Ejeep
Guest
Posts: n/a
 
      11-07-2007
On Nov 6, 10:34 pm, Gordon Beaton <(E-Mail Removed)> wrote:
> On Tue, 06 Nov 2007 06:11:46 -0800, Ejeep wrote:
> > 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
>
> --


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?

----
ejeep

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
share function argument between subsequent calls but not between class instances! K. Jansma Python 7 02-21-2006 01:31 PM
can not share printer between xp pro and xp home =?Utf-8?B?RG9yb3RoeSBOYXBpZXI=?= Wireless Networking 12 07-10-2005 12:24 PM



Advertisments