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: Thomas Chust
Subject: Re: [Chicken-users] foreign: Why is passing structs as arguments and return-types not supported?
Date: Wed, 29 Feb 2012 14:29:00 +0100

On Wed, 2012-02-29 at 14:07 +0100, Kristian Lein-Mathisen wrote:
> [...]
> Reading the chicken docs on foreign, you'll find:
> 
> 
>         Structs cannot be directly passed as arguments to foreign
>         functions, nor can they be result values.
>         (http://api.call-cc.org/doc/foreign/types#def:struct)
>         
>         
> Pointers to structs are supported, but not structs-by-value. I am
> wondering if there are technical reason why this is the case, or if
> it's simply a "missing feature".
> [...]

Hello,

there are several reasons why few foreign function interfaces out there
support this.

The most compelling reason maybe is that it is not strictly necessary to
support this feature and hence it adds no real value. For example, let's
assume we have a C library with an interface like this:

  typedef struct Dosh { int foo, bar; } Dosh;
  void distim(Dosh d);

Now if we want a CHICKEN binding for that it's easy to write:

  (define distim
    (foreign-lambda* void (((c-pointer "Dosh") d))
      "distim(*d);"))

Just use a pointer argument, dereference it on the C side and you're
done!

Another reason why structure types would not be all that useful is that
the canonical representation of C structures on the CHICKEN side would
be foreign pointers to blocks of memory anyway. Plus most C ABIs pass
most structure arguments by reference anyway. So supporting an
additional class of types that would effectively map to the same
constructs on both sides of the FFI would be redundant and could even be
considered needlessly confusing.

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.

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.




reply via email to

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