12 messages in net.java.dev.jna.usersRe: [jna-users] Buffer arguments as p...
FromSent OnAttachments
Albert StrasheimAug 22, 2007 3:47 pm 
Wayne MeissnerAug 22, 2007 4:19 pm 
Albert StrasheimAug 22, 2007 4:39 pm 
Wayne MeissnerAug 22, 2007 5:33 pm 
Albert StrasheimAug 22, 2007 5:47 pm 
Wayne MeissnerAug 22, 2007 6:13 pm 
Albert StrasheimAug 22, 2007 6:21 pm 
Albert StrasheimAug 22, 2007 6:26 pm 
Wayne MeissnerAug 22, 2007 8:05 pm 
Albert StrasheimAug 23, 2007 3:25 pm 
Wayne MeissnerAug 23, 2007 4:32 pm 
Albert StrasheimAug 23, 2007 4:50 pm 
Actions with this message:
Paste this link in email or IM:
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-bufferActions...
From:Albert Strasheim (full@gmail.com)
Date:Aug 23, 2007 3:25:31 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 5:05 AM Subject: Re: [jna-users] Buffer arguments as pointer-to-buffer

Albert Strasheim wrote:

else if (arg instanceof Buffer) { if (!(arg instanceof ByteBuffer) || !((ByteBuffer)arg).isDirect()) {

I think any direct buffer can be passed through, so it should probably simply be:

else if (arg instanceof Buffer) { if !((Buffer)arg).isDirect()) {

That would be nice, apart from the fact that Buffer.isDirect() didn't appear until JDK 6 :-)

Argh. I'll read the Javadoc more carefully next time.

JNA needs to be JDK 1.4 and JDK 1.5 (for V3) compatible.

Indeed.

Also, it would be useful if I could write the function in the interface as:

void foo(Buffer buf);

and have it accept any direct buffer. Also, all the other variants should also work, e.g.,

That should work now for any JDK provided direct Buffer (unless I missed one).

Seems to work for me. Great stuff.

void foo(FloatBuffer buf);

As far as I can tell, the tests don't cover these cases yet.

They do now.

I'm a bit uneasy about using undocumented behaviour here, since it could break on other VMs (e.g. gcj, JRE 1.4?). Then again, without using the undocumented behaviour, it won't work on them anyway, so we don't lose much.

Sounds like a good philosophy.

Thanks for all the work so far. I have one more request to make things just a little bit better. :-)

Looking at getBufferArg in Function.java, heap buffers currently have to have arrayOffset()==0 to be allowed to pass through.

I haven't looked at the code in depth, but maybe the addition of an argument type like:

class ArrayOffset<T> { int offset; T arr; public ArrayOffset(T arr, int offset) { this.arr = arr; this.offset = offset; } }

would be useful. This way you can write your function as:

void foo(ArrayOffset<float[]> arr);

and call it like:

MyLib.foo(new ArrayOffset<float[]>(new float[]{1.0f, 2.0f, 3.0f}, 1));

If the ArrayOffset object is passed through into the native dispatch function, it can check for this class like for a pointer:

(*env)->IsInstanceOf(env, arg, classArrayOffset)

and if one of these is encountered, pin the array, but then pass &ptr[offset] to the native function being called.

ArrayOffset can then also be used to pass heap buffers with non-zero arrayOffset() to any function.

Does this sound feasible? Maybe without the generics so that it can work with JDK 1.4...

Cheers,