If you're worried about performance, you can use a
memory-mapped shared memory, if you're willing to risk your
app breaking on the next API upgrade.
The android.os.MemoryFile() object uses the ashmem driver to
allocate a shared memory file. With a little reflection
trickery you can go get the mFD field out of that object and
send it into C to be mmap()'d. The Android source file
MemoryFile.java and the corresponding native_* methods are
your best reference for figuring out the details on how to
make that work.
In C you use memcpy() to put data into it, and in Java use
readBytes() to extract the data out. I'd still recommend an
int[] as it's the simplest way to deal with the data on both
sides.
This is, of course, *totally unsupported* -- they changed
the internals of MemoryFile between 1.6 and 2.0 in a
non-compatible way, and there's no guarantee it won't happen
again. But in the meantime, it's the only option I know of
to actually pass data in bulk between Java <--> C in any
reasonably fast way.
http://developer.android.com/reference/android/os/MemoryFile.html
You can determine whether or not the data
was copied by passing in a
non-NULL pointer for the isCopy argument.
The memory will be pinned when locked for use by native code, but
that is generally not a problem. You do have to carefully manage
local and global references, and there is a restriction that the
lock and unlock, and maybe also the local/global reference, have to
be done from the same thread.Memory-mapped shared memory is very useful for certain things, especially efficient passing of data between processes. However, based on the Java calls, it appears that it is doing a copy of the data. Most interaction between JNI and native results in copying data.
-- Stephen D. Williams s...@lig.net stephend...@gmail.com LinkedIn: http://sdw.st/in V:650-450-UNIX (8649) V:866.SDW.UNIX V:703.371.9362 F:703.995.0407 AIM:sdw Skype:StephenDWilliams Yahoo:sdwlignet Resume: http://sdw.st/gres Personal: http://sdw.st facebook.com/sdwlig twitter.com/scienteer
--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.