

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
12 messages in net.java.dev.jna.usersRe: [jna-users] Buffer arguments as p...| From | Sent On | Attachments |
|---|---|---|
| Albert Strasheim | Aug 22, 2007 3:47 pm | |
| Wayne Meissner | Aug 22, 2007 4:19 pm | |
| Albert Strasheim | Aug 22, 2007 4:39 pm | |
| Wayne Meissner | Aug 22, 2007 5:33 pm | |
| Albert Strasheim | Aug 22, 2007 5:47 pm | |
| Wayne Meissner | Aug 22, 2007 6:13 pm | |
| Albert Strasheim | Aug 22, 2007 6:21 pm | |
| Albert Strasheim | Aug 22, 2007 6:26 pm | |
| Wayne Meissner | Aug 22, 2007 8:05 pm | |
| Albert Strasheim | Aug 23, 2007 3:25 pm | |
| Wayne Meissner | Aug 23, 2007 4:32 pm | |
| Albert Strasheim | Aug 23, 2007 4:50 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Buffer arguments as pointer-to-buffer | Actions... |
|---|---|---|
| From: | Albert Strasheim (full...@gmail.com) | |
| Date: | Aug 22, 2007 5:47:14 pm | |
| List: | net.java.dev.jna.users | |
Hello all
----- Original Message ----- From: "Wayne Meissner" <wmei...@gmail.com> To: <use...@jna.dev.java.net> Sent: Thursday, August 23, 2007 2:34 AM Subject: Re: [jna-users] Buffer arguments as pointer-to-buffer
Albert Strasheim wrote:
Hello
Unless I'm mistaken, ByteBuffer#asFloatBuffer() returns a FloatBuffer that throws UnsupportedOperationException when you call array(), so there's no way for me to get a float[] from this kind of buffer without copying.
There's no way to get a float[] without copying.
Maybe I wasn't clear here.
What I meant was that if I used FloatBuffer#allocate, i.e., a heap FloatBuffer, I could get the backing float[] by calling FloatBuffer#array, which means I don't copy while working in Java.
When using a direct ByteBuffer, you don't copy when going to C, but you have to copy if you want a float[].
When using a heap ByteBuffer, you might copy when going to C (if the VM can't pin the array), and you also have to copy when going to float[], since there is no float[] backing array that you can access.
The JVM doesn't appear to support mapping a region of native memory to primitive arrays - it all has to go through a DirectByteBuffer, and be accessed via ByteBuffer.get(), ByteBuffer.asFloatBuffer() and friends.
That's how I understand it.
Thus, it would be useful if I could pass these kinds of buffers directly to JNA.
The best I can do is make something like this work:
interface Foo extends Library { void some_function(FloatBuffer fb); }
which would be equivalent to: interface Foo extends Library { void some_function(float[] fb); }
That _should_ pass the array directly to JNI - depending on whether the VM supports/can pin the array. If not it will copy to/from a native buffer and pass that on. If your native code depends on the address of the array not changing, that will break, but otherwise it should be fine.
That would be great. With this implemented, I think all the bases are covered, namely:
- byte[] (VM pins or copies to give us a pointer) - heap ByteBuffer (since ByteBuffer#array returns a byte[] which the VM can pin or copy) - direct ByteBuffer (pointer is available via a JNI call) - float[] (VM pins or copies to give us a pointer) - heap FloatBuffer (since FloatBuffer#array returns a float[] which the VM can pin or copy) - direct FloatBuffer (pointer is available via a JNI call) - etc. for all the other primitive types
Hope I got all my heaps and pins and buffers right. :-)
Cheers,
Albert







