Markus Flambard | 27 Jul 13:20 2008
Picon

Passing struct by value, kind of

Hello lispers!

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?

Example:

struct MyStruct {
  int32 x, y;
};

int myFunction(struct MyStruct s);

Could I force/lie to CFFI and declare 's' to be of type int64 and pass
it that way?

I tried something similar, but then CFFI (well, ClozureCL) just
couldn't resolve the function symbol..

--

-- 
Markus Flambard
6A63 2761 0A9E E510 DDAF C796 A3DF AB90 9C7C AD5C
Markus Flambard | 27 Jul 16:26 2008
Picon

Re: Passing struct by value, kind of

With some test I found that it is indeed possible. The unresolved
symbol problem was unrelated.

Thanks for listening! :-)

--

-- 
Markus Flambard
6A63 2761 0A9E E510 DDAF C796 A3DF AB90 9C7C AD5C
Luís Oliveira | 27 Jul 16:44 2008
Picon

Re: Passing struct by value, kind of

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/

Gmane