chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] foreign: Why is passing structs as arguments and ret


From: Kristian Lein-Mathisen
Subject: Re: [Chicken-users] foreign: Why is passing structs as arguments and return-types not supported?
Date: Wed, 29 Feb 2012 16:18:13 +0100


Thanks to both of you for some valuable insight! I didn't realize de-referencing on the c-side would be that simple, nor that structs-by-value would actually be pointers anyway.

However, my problem is still not entirely solved. I am trying to interface to the physics engines Box2D and Chipmunk, which are quite big. They both use struct-by-value extensively. Small structs like

struct b2Vec2 {float x,y};
 
are passed around virtually everywhere.

When I use `chicken-bind` on my stripped-down version of Box2D.h, it generates foreign-lambda's that return struct-by-value without complaint. When I compile, it fails like the docs say:

Error: illegal foreign return type `b2Vec2'

Since struct-by-value is so central in both of these libraries, manually writing wrapper-code would take too long. I am unsure of where to go from here, but I suppose my options are:

- Modify `foreign` to support struct-by-value
- Modify `chicken-bind` to automatically write wrapper-code for struct-by-value
- Write a script that re-parses the generated scheme-binding and inserts the wrappers

Any thoughts on how to pursue this?
Thanks!
K.

On Wed, Feb 29, 2012 at 3:26 PM, Alaric Snell-Pym <address@hidden> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 02/29/2012 01:29 PM, Thomas Chust wrote:

> Last but not least, passing structures as pointers makes memory
> management for them explicit and that is a good thing in this case
> because there is simply no way the FFI could automatically figure out
> how the optimal memory management strategy for structures passed by
> value should look like. For example, whether such a structure can be
> allocated as an atomic block of memory or not is impossible to answer
> without knowing what the functions producing and consuming instances of
> the structure actually do.

I slightly disagree here. A structure passed or returned by value is
just shoved on the stack, maybe in registers if it's a small structure.
There's no extra memory management required; merely some extra copying
to copy from the Chicken-side foreign-type pointer into the appropriate
bit of stack or registers, or back again for returned values.

But, indeed, it's easy to do that in C as your distim demonstration
shows; return values are only slightly harder as you'll need to allocate
a struct and copy the result in, then return that pointer.

ABS

- --
Alaric Snell-Pym
http://www.snell-pym.org.uk/alaric/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk9ONY8ACgkQRgz/WHNxCGpVFQCdFmrrkp6RhMkQQNIaRtElcQUw
At8An3tuScsj6vKNCi/CQDVVTeRR1ZHE
=foQX
-----END PGP SIGNATURE-----

_______________________________________________
Chicken-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users


reply via email to

[Prev in Thread] Current Thread [Next in Thread]