Loading ...
Sorry, an error occurred while loading the content.

From C bitfield to OCaml value

Expand Messages
  • rixed@happyleptic.org
    Suppose you have a C value like this : #define FOO (1U
    Message 1 of 5 , Jan 5, 2010
    • 0 Attachment
      Suppose you have a C value like this :

      #define FOO (1U<<0)
      #define BAR (1U<<1)
      #define BAZ (1U<<2)
      unsigned flags;

      What's the best way to export it into an OCaml value ?

      Is it to export it as an int, and let the user perform logical operations
      to extract fields, or is it to build a variant type list, or a record of
      boolean values, or anything else ?
    • Richard Jones
      ... The usual solution to this is to turn it into a list of flags. More often this happens in the other direction, so there are *lots* of examples in OCaml
      Message 2 of 5 , Jan 5, 2010
      • 0 Attachment
        On Tue, Jan 05, 2010 at 03:07:09PM +0100, rixed@... wrote:
        > Suppose you have a C value like this :
        >
        > #define FOO (1U<<0)
        > #define BAR (1U<<1)
        > #define BAZ (1U<<2)
        > unsigned flags;
        >
        > What's the best way to export it into an OCaml value ?
        >
        > Is it to export it as an int, and let the user perform logical operations
        > to extract fields, or is it to build a variant type list, or a record of
        > boolean values, or anything else ?

        The usual solution to this is to turn it into a list of flags.

        More often this happens in the other direction, so there are *lots*
        of examples in OCaml itself and in lablgtk2 where there is code
        to convert from:

        [O_WRONLY; O_EXCL; O_CREAT]

        to this C integer:

        O_WRONLY | O_EXCL | O_CREAT

        usually using a loop. Here is how that precise transformation is done
        in the OCaml standard library:

        http://caml.inria.fr/cgi-bin/viewcvs.cgi/ocaml/trunk/otherlibs/unix/open.c?rev=6824&view=markup
        (see function unix_open)
        http://caml.inria.fr/cgi-bin/viewcvs.cgi/ocaml/trunk/byterun/alloc.c?rev=6045&view=markup
        (see function caml_convert_flag_list)

        You can do this in reverse easily enough.

        I would caution that this isn't very efficient, but that's probably
        the least of your worries for heavyweight operations like opening
        files. If you want efficiency and don't care about safety, convert it
        to an OCaml int using Val_int and let people do bitwise operations on
        the OCaml side.

        Rich.

        --
        Richard Jones
        Red Hat
      • grantrettke
        ... Would you say that lablgtk is a good source of idiomatic OCaml for folks to study?
        Message 3 of 5 , Jan 5, 2010
        • 0 Attachment
          --- In ocaml_beginners@yahoogroups.com, Richard Jones <rich@...> wrote:
          >
          > there are *lots* of examples in OCaml itself and in lablgtk2

          Would you say that lablgtk is a good source of idiomatic OCaml for folks to study?
        • Richard Jones
          ... Heh, perhaps :-( On the OCaml side, it s an example of very advanced use of OCaml features, a lot of those features were actually written by the author of
          Message 4 of 5 , Jan 5, 2010
          • 0 Attachment
            On Tue, Jan 05, 2010 at 02:58:29PM -0000, grantrettke wrote:
            > --- In ocaml_beginners@yahoogroups.com, Richard Jones <rich@...> wrote:
            > >
            > > there are *lots* of examples in OCaml itself and in lablgtk2
            >
            > Would you say that lablgtk is a good source of idiomatic OCaml for
            > folks to study?

            Heh, perhaps :-(

            On the OCaml side, it's an example of very advanced use of OCaml
            features, a lot of those features were actually written by the author
            of lablgtk, added to OCaml itself to support lablgtk. So labels
            obviously (hence "labl"), but also very advanced use of polymorphic
            variants and co/contra-variant types. It probably contains the most
            advanced use of the OCaml type system in real code.

            On the C side, it's a big mess of autogenerated code and C macros.

            I wouldn't necessarily recommend it to beginners.

            Rich.

            --
            Richard Jones
            Red Hat
          • Jon Harrop
            ... That kind of code is often full of bugs. When I tried to use LablGL (which is from the same OCaml developer) I found that the only non-trivial bindings
            Message 5 of 5 , Jan 5, 2010
            • 0 Attachment
              On Tuesday 05 January 2010 15:16:04 Richard Jones wrote:
              > On Tue, Jan 05, 2010 at 02:58:29PM -0000, grantrettke wrote:
              > > --- In ocaml_beginners@yahoogroups.com, Richard Jones <rich@...> wrote:
              > > > there are *lots* of examples in OCaml itself and in lablgtk2
              > >
              > > Would you say that lablgtk is a good source of idiomatic OCaml for
              > > folks to study?
              >
              > Heh, perhaps :-(
              >
              > On the OCaml side, it's an example of very advanced use of OCaml
              > features, a lot of those features were actually written by the author
              > of lablgtk, added to OCaml itself to support lablgtk. So labels
              > obviously (hence "labl"), but also very advanced use of polymorphic
              > variants and co/contra-variant types. It probably contains the most
              > advanced use of the OCaml type system in real code.
              >
              > On the C side, it's a big mess of autogenerated code and C macros.
              >
              > I wouldn't necessarily recommend it to beginners.

              That kind of code is often full of bugs. When I tried to use LablGL (which is
              from the same OCaml developer) I found that the only non-trivial bindings
              (glutess) were completely broken and required a total rewrite. I never
              managed to rewrite them properly and they have remained in an incomplete
              state to this day. Similar bugs were found in the FFI tool ocamlidl (by
              Xavier Leroy, OCaml's dev lead).

              Given that the people who developed OCaml often make serious mistakes when
              interoperating with it, I would advise extreme caution. Convert your flags
              into a 31-bit OCaml int (if they'll fit) and do everything else on the OCaml
              side. If at all possible, avoid allocating into the OCaml heap from the C
              side.

              --
              Dr Jon Harrop, Flying Frog Consultancy Ltd.
              http://www.ffconsultancy.com/?e
            Your message has been successfully submitted and would be delivered to recipients shortly.