[ocaml-ctypes] Bit field encoding.

Philippe Veber philippe.veber at gmail.com
Tue May 20 06:53:06 BST 2014


Thanks a lot Jeremy for your answer! I have chosen to write helper C
functions to access the records containing bitfields, so that I let the
compiler do its business without caring too much. I guess that no field
after a bitfield definition in a struct can be reliably accessed either
(with ctypes), even if it's not a bitfield itself (as there is no means to
know if the bitfield was packed or padded). Is that right?


2014-05-17 2:14 GMT+02:00 Jeremy Yallop <yallop at gmail.com>:

> Dear Philippe,
>
> On 16/05/2014, Philippe Veber <philippe.veber at gmail.com> wrote:
> > Has ctypes some support for bit fields in C struct?
>
> Not yet, I'm afraid.
>
> > If not is it correct to
> > encode:
> >
> > typedef struct {
> >    int32_t pos;
> >    uint32_t bin:16, qual:8, l_qname:8;
> >    ...
> > } bam1_core_t
> >
> > as:
> >
> >    type bam1_core_t
> >    let bam1_core_t : bam1_core_t structure typ = structure "bam1_core_t"
> >    let bam1_core_t_pos_f = field bam1_core_t "pos" int32_t
> >    let bam1_core_t_props_f = field bam1_core_t "props" int32_t
> >    ...
> >    let () = seal bam1_core_t
> >
> > and then play with the fake "props" field directly?
>
> It's quite likely to work but it's not guaranteed, since the C
> standard gives compilers a fair amount of freedom in laying out
> bit-fields.  The good news is that the standard also requires
> compilers to document how bit-fields are laid out, so your compiler
> documentation may have helpful details.
>
> Kind regards,
>
> Jeremy.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20140520/89072f0d/attachment.html>


More information about the Ctypes mailing list