I'm trying to determine whether this is even possible... I have a
_very_ large buffer malloc'd on my C heap that I would like to hand
over to Java, preferably as a Byte[]. So far, every example of this
that I have come across involves _copying_ the data from my C buffer
into java storage. Is there any way to avoid this expensive (very
expensive in my case) operation?
How come it's so expensive ? You have presumably filled in the array in your C
code, which will take time (at least) proportionate to the size of the
buffer -- with constants or proportionality that are (at least) approaching the
cost of copying. And you are presumably going to use the data in the buffer in
your Java code, which again will take time (at least) proportionate to the size
of the buffer -- and again with constants that are (at least) approaching the
cost of copying. In fact either the processing on the C side must be
significantly more expensive than the copy, or the processing on the Java side
must be -- or else both are so trivial that there's no point in copying the
data across at all. So I don't see how the copy can take more a small part of
your total execution time.
Anyway, as Thomas has already mentioned, you can use NIO buffers to wrap a
C-side buffer in a Java object which will access it directly. But remember
that /every/ access to that data from Java will then cost more than a direct
lookup in a byte[] array would (several times more according to a link that
Thomas posted recently), so you should carefully consider whether that overhead
will overall be greater than that of a single copy.
Another approach would be to ask Java to allocate the byte[] array, and then
for the C code to use that memory as its working data (rather than malloc()-ing
a buffer). For that to work depends on GetByteArrayElements() returning a
pointer to the JVM's internal data directly, rather than making a copy (which
is implementation-dependent, and may vary depending on other factors too).
Another factor to consider is that if the JVM allocates the byte buffer then
it'll zero the memory for you, which will add a cost not so far off the cost of
the copy -- but that might be acceptable if the C code would otherwise have to
zero the memory itself.
-- chris