dcsimg
Using JNI with NewDirectByteBuffer to access shared memory chunk and return this ByteBuffer to java for parsing
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mark_Krutz
Posted On:   Monday, February 24, 2003 02:19 PM

I have a "C" client library that accesses a segment of shared memory. I realize that the shared memory address space is outside the JVM and normally unaccessible. However, in reading some updates on the java.sun website, it came to my attention that the latest release 1.4.1_01 might offer a way to access the native address space. Snippet of native code: JNIEXPORT jobject Java_TEST_getSharedMemoryBlock( JNI *env, jobject obj, jint ptr...) { jobject rc; rc = (*env)->NewDirectByteBuffer(env, sharedMemStartAddress, sharedMemSize); return rc; } This "seems" to work. I haven't yet figured out yet how to print items from this ByteBuffer(actually a jobject) on the nat   More>>

I have a "C" client library that accesses a segment of shared memory. I realize that the shared memory address space is outside the JVM and normally unaccessible. However, in reading some updates on the java.sun website, it came to my attention that the latest release 1.4.1_01 might offer a way to access the native address space.

Snippet of native code:
JNIEXPORT jobject Java_TEST_getSharedMemoryBlock(
JNI *env, jobject obj, jint ptr...) {

jobject rc;

rc = (*env)->NewDirectByteBuffer(env, sharedMemStartAddress, sharedMemSize);
return rc;
}

This "seems" to work. I haven't yet figured out yet how to print items from this ByteBuffer(actually a jobject) on the native side. It doesn't crash the application or the JVM.

The problem I have is when I get the ByteBuffer back into the Java side.

Java snippet (please assume I have done all the appropriate native building correctly):
// ptr was returned through a previous call to a connect
// to shared memory native call. It seems to have a
// valid value
bout = getSharedMemoryBlock(ptr);

// The above call does return successfully!

Now when I try to actually pull data out of this ByteBuffer (like below):
int a = bout.getInt();

the JVM actually crashes. I get reams of output all boiling down to this pseudo stack dump:

Unxpected Signal: 11 occurred at PC=0x40395E09
Function=Unsafe_GetNativeInt+0x6d
Library=/opt/java/j2sdk1.4.0_01.jre/lib/i386/client/libjvm.so

Current Java thread:
at sun.misc.Unsafe.getInt(Native Method)
at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:601)
at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:608)


Any suggestions about this would be much appreciated. Maybe there is a better way to gain access (return a bytestream from shared memory) than I am trying? Is this even doable?

   <<Less
About | Sitemap | Contact