Re: Returning structs in c_func
- Posted by jimcbrown (admin) Jul 03, 2013
- 2273 views
Side note: I know Matt's been working on struct support for 4.1. Any chance that could make its way to returning structs-by-value via c_func/c_proc? This would help with a few other libraries as well.
We'd have to understand how compilers handle this. I've never looked to see what they do in those cases, but I agree that it would be extremely useful.
Matt
My understanding was that structs, when returned (passed by value) from a C function, will be passed on the stack. So, a struct made up of 5 elements, each being a 32bit int, will take up 20 bytes of stack space. Likewise, a struct made up of a single element, an array of bytes, with the array having a length of 12, will take up 3 dwords of stack space.
I was thinking that, in these cases, we could somehow encode the length of the struct in the return type, and return a sequence of bytes to be reconstructed by the caller.
We'd have to figure out how to get that stuff off the stack. Yuck.
Hmm. Is this really more difficult than doing some pop() calls, like older versions of be_callc.c used to do for integer return types?
And I suspect it will work differently on different architectures. Double yuck.
I would have thought that this would actually depend on calling convention... I see no reason for the assembly code to work differently on an ARM vs x86-64 for this case.
Especially considering the semi-hardcoded way we call stuff on x86-64 (and ARM? I can't recall of the top of my head).
I have thought of a way to get this to work, but it may be too ugly to implement. (It resembles the trick used to implement callbacks in the DOS branch, working around the issue of copying memory around that presumbly caused the DOS extender to bomb out.)
I would rather return a sequence containing the data as it would appear if you peeked it as a memstruct.
Matt
I agree that this is the better approach, but we'd need to support memstructs first before this can be done.