[cffi-devel] Passing struct by value, kind of

Luís Oliveira luismbo at gmail.com
Sun Jul 27 14:44:59 UTC 2008


On Sun, Jul 27, 2008 at 12:20 PM, Markus Flambard <markus at flambard.se> wrote:

> I know that CFFI doesn't support passing structs by value. What I'm
> wondering is, is it possible to squeeze in a small struct by instead
> passing it as an int (or something) of the same size as the struct?

On linux/x86, for instance, it's fairly straightforward to pass
structs by value since all arguments are passed on the stack,
word-aligned. For instance, given the following C code:

    /* sizeof(s) => 12 */
    struct s { char c; short s; int i; };
    void f(char x, struct s y, short z);

f can be declared as:

    (defcfun f :void (x :char) (y0 :int) (y1 :int) (z :short))

f(1, {2, 3, 4}, 5) is then equivalent to (f 1 #x00030002 4 5), or
something like that. This works for any struct regardless of its size
or its members' data types. Other architectures might have less
straightforward calling conventions.

-- 
Luís Oliveira
http://student.dei.uc.pt/~lmoliv/



More information about the cffi-devel mailing list