[erlang-questions] Passing data between Erlang and C port while preserving padding and alignment?

Mikael Pettersson mikpelinux@REDACTED
Fri Dec 7 18:30:34 CET 2018


You'll need to use C's offsetof() and sizeof() to discover (a) any
internal padding between end of a previous field and the start of a
new field, basically offsetof(struct, f2) - (offsetof(struct, f1) +
sizeof(struct.f1)) gives that internal padding, and (2) any trailing
padding, given by sizeof(struct) - (offsetof(struct, lastField) +
sizeof(struct.lastField)).  Beware that the contents of padding is
unspecified, so might not be all-bits-zero.

You'll need to derive these values via the C compiler.  In theory you
could derive them in pure Erlang given the types involved, but that'd
require not only a C type parser but also a complete description of
the machine's C ABI.  I would't go that way.
On Fri, Dec 7, 2018 at 9:49 AM Frank Muller <frank.muller.erl@REDACTED> wrote:
>
> Hello Vance
>
> Fantastic!!!
>
> It would be great if you can share some code.
> I’m totally lost with this tricky problem and no one was able to help me.
>
> Thanks in advance...
>
> /Frank
>>
>> On Fri, Dec 7, 2018 at 9:51 AM Frank Muller <frank.muller.erl@REDACTED> wrote:
>> > Question: how can I derive the GCC's padding, and automatically add it to my Erlang binary?
>>
>> Frank,
>>
>> I used to write linked-in drivers for embedded systems which provided
>> C library APIs. Back in those days I was building for 32 & 64 bit, x86
>> & SPARC. I used GNU autotools to figure out the target environment and
>> generate macros in my Erlang header files. It all worked quite
>> smoothly in the end.  I'd be happy to send you an example project if
>> you think it'd helpful.
>>
>> To your specific question I think the autoconf macro AC_CHECK_ALIGNOF
>> does what you want:
>>      https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Generic-Compiler-Characteristics.html
>>
>> --
>>      -Vance
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list