[ocaml-ctypes] C-array of fixed size, #defines and C11 anonymous unions
Jeremy Yallop
yallop at gmail.com
Fri Feb 14 12:03:56 GMT 2014
On 12/02/2014, Markus Weißmann <markus.weissmann at in.tum.de> wrote:
> 1.) I was wondering how fixed-size C-arrays should be handled in
> ctypes;
> there exist several APIs that use a fixed-size array as part of a
> struct, e.g. ifreq (used to configure network interfaces via ioctl(2) on
> linux)
>
> struct ifreq {
> union {
> char ifrn_name[16];
> } ifr_ifrn;
> ...
>
> How do I handle that in ctypes?
Array members are supported without the need to do anything special.
type ifreq
let ifreq : ifreq structure typ = structure "ifreq"
let ifr_ifrn = field ifreq "ifr_ifrn" (array 16 char)
> 2.) Is there a "clever" way of handling C-#defines?
[...]
> Something that would work, too, is to write a generator in C that
> creates a defines.ml for me (e.g. "printf("let bufsiz = %d\n", BUFSIZ))
This is probably the best approach at the moment. It's likely that
there'll eventually be some support for this in ctypes itself. The
stub generation code (in trunk, but not yet released) already supports
some function-like macros; it may eventually handle unparameterized
macros like BUFSIZ as well.
> 3.) The C11 standard allows for unnamed unions -- not a concrete
> problem right now, but is there a more elegant way than to introduce a
> new name for the union in my ocaml/ctypes code?
>
> typedef struct {
> union {
> float f;
> int i;
> };
> } some_t;
> ...
> some_t x;
> x.f = 4.5;
> printf("%d", x.i);
I think that naming the union is the best approach with the current
ctypes release. Things will improve when we have support for
retrieving the struct layout from C (issue #62). At that point making
'f' and 'i' members of the surrounding struct (some_t) will give you
something that behaves like the anonymous union, since C will report
that the offsets of the fields are the same.
More information about the Ctypes
mailing list