Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI Novice question

Reply
Thread Tools

JNI Novice question

 
 
Aaron Fude
Guest
Posts: n/a
 
      05-25-2004
Hi,

Is it possible to be in control of allocating your memory for arrays? In
other words, rather than

jintArray tri = inEnv->NewIntArray(10);
jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));

to do

jint *thearray = new jint[10];
jintArray tri = inEnv->NewIntArray(Use theArray);

Thanks!

Aaron Fude



 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      05-26-2004
On Tue, 25 May 2004 17:58:10 -0400, Aaron Fude wrote:
> Is it possible to be in control of allocating your memory for
> arrays?


[...]

> jint *thearray = new jint[10];
> jintArray tri = inEnv->NewIntArray(Use theArray);


Not exactly like that, but have a look at NewDirectByteBuffer(),
described under "JNI enhancements in 1.4". It lets you create a
ByteBuffer from memory you've allocated using some other mechanism.
Other JNI functions let you retrieve the address and size of the
original buffer:

jobject allocateByteBuffer(JNIEnv *env, jclass this, jint size)
{
void *p;

if ((p = malloc(size))) {
return (*env)->NewDirectByteBuffer(env,p,size);
}
else return NULL;
}

void freeByteBuffer(JNIEnv *env, jclass this, jobject buf)
{
char *p = (*env)->GetDirectBufferAddress(env,buf);
free(p);
}

In Java, you can treat the ByteBuffer as different buffer type, for
example:

ByteBuffer bb = allocateByteBuffer(100); // allocate ByteBuffer
IntBuffer ib = bb.asIntBuffer(); // get IntBuffer view

In theory you should be able to do the following to get an int array
backed by the ByteBuffer you've just allocated, but unfortunately the
array() method is optional and does not seem to be implemented for
ByteBuffers created this way:

if (ib.hasArray()) {
int[] arr = ib.array();
}

Also have a look at java.nio.MappedByteBuffer, which appears to let
you do something similar with sections of a mapped file.

You didn't mention what specific problem you're trying to solve, so it
might be worth pointing out that when you do this:

> jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));


....that you can make modifications to thearray just as you would any
other array of (j)int, and any changes will propagate back to the
original int[] when you later call ReleaseIntArrayElements() (i.e. you
don't need to use SetIntArrayRegion() to write to the int[]).

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Aaron Fude
Guest
Posts: n/a
 
      05-28-2004
Thank you. Very informative.

"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:40b4de87$(E-Mail Removed)...
> On Tue, 25 May 2004 17:58:10 -0400, Aaron Fude wrote:
> > Is it possible to be in control of allocating your memory for
> > arrays?

>
> [...]
>
> > jint *thearray = new jint[10];
> > jintArray tri = inEnv->NewIntArray(Use theArray);

>
> Not exactly like that, but have a look at NewDirectByteBuffer(),
> described under "JNI enhancements in 1.4". It lets you create a
> ByteBuffer from memory you've allocated using some other mechanism.
> Other JNI functions let you retrieve the address and size of the
> original buffer:
>
> jobject allocateByteBuffer(JNIEnv *env, jclass this, jint size)
> {
> void *p;
>
> if ((p = malloc(size))) {
> return (*env)->NewDirectByteBuffer(env,p,size);
> }
> else return NULL;
> }
>
> void freeByteBuffer(JNIEnv *env, jclass this, jobject buf)
> {
> char *p = (*env)->GetDirectBufferAddress(env,buf);
> free(p);
> }
>
> In Java, you can treat the ByteBuffer as different buffer type, for
> example:
>
> ByteBuffer bb = allocateByteBuffer(100); // allocate ByteBuffer
> IntBuffer ib = bb.asIntBuffer(); // get IntBuffer view
>
> In theory you should be able to do the following to get an int array
> backed by the ByteBuffer you've just allocated, but unfortunately the
> array() method is optional and does not seem to be implemented for
> ByteBuffers created this way:
>
> if (ib.hasArray()) {
> int[] arr = ib.array();
> }
>
> Also have a look at java.nio.MappedByteBuffer, which appears to let
> you do something similar with sections of a mapped file.
>
> You didn't mention what specific problem you're trying to solve, so it
> might be worth pointing out that when you do this:
>
> > jint *thearray = (jint *) (inEnv->GetIntArrayElements(tri, 0));

>
> ...that you can make modifications to thearray just as you would any
> other array of (j)int, and any changes will propagate back to the
> original int[] when you later call ReleaseIntArrayElements() (i.e. you
> don't need to use SetIntArrayRegion() to write to the int[]).
>
> /gordon
>
> --
> [ do not email me copies of your followups ]
> g o r d o n + n e w s @ b a l d e r 1 3 . s e



 
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
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments