Wednesday, December 10, 2003 05:40 PM
Callbacks from jni layer back to java are pretty expensive.
Al alternative would be to make use of java nio direct buffers.
Direct buffers allow u to share a physical memory between jni and java
eg: - A ByteBuffer allocated with ByteBuffer.allocateDirect(... ) fn would represent a piece of memory which corresponds to a memory address in the native layer
U could have a jni call which passes the ByteBuffer in the jni layer as part of some initialisation process.
Inside this call - extract the memory address of the shared memory using the jnienv->GetDirectBufferAddress(.. function as a char*.
Once u get this address, u can diretly typecast ur struct to this memory location.
char* buf = env->GetDirectBufferAddress(..
int a ;
ASTRUCT *astruct = (ASTRUCT*) buf;
astruct->a = 1;
astruct->b = 2;
astruct->c = 3;
u can now extract the same values from the java side, by calling getInt() from the associated ByteBuffer object,.
u can store the ByteBuffers in a hashmap. Or do some other kind of optimised storage