[ocaml-ctypes] Defines, and Structs with platform-specific fields

Trevor Smith trevorsummerssmith at gmail.com
Fri Oct 17 18:53:54 BST 2014

Thank you for your reply Daniel.

About the structs: perhaps I have a fundamental misconception about how
ctypes works. Libuv functions expect that the caller allocate the memory
for the struct. My understanding was that adding fields to the struct in
ctypes would affect the memory that struct takes up. Therefore, the
different platforms would actually need these separate fields**.

**Some library maintainers get around this by allocating an area for
extension of a fixed size. This is not the case with libuv.

You are correct though that I don't need to expose many (actually probably
none) of the Loop's internal fields.



On Friday, October 17, 2014, Daniel Bünzli <daniel.buenzli at erratique.ch>

> Le vendredi, 17 octobre 2014 à 13:35, Trevor Smith a écrit :
> > Hello,
> >
> > I am working on a ctypes binding to libuv [
> https://github.com/joyent/libuv], and I have two questions:
> >
> > 1) Is the recommended way to interact with C-defines the way described
> in this thread?
> http://lists.ocaml.org/pipermail/ctypes/2014-February/000064.html
> I think so. If you want to pattern match you can have a look here:
> https://github.com/dbuenzli/tsdl/tree/master/support
> https://github.com/dbuenzli/tsdl/blob/master/myocamlbuild.ml#L39
> > 2) What is the recommended way to deal with struct fields that vary
> depending upon platform?
> >
> > Libuv is a cross platform library, and has platform-specific fields for
> its structs. For example see the struct uv_loop_s.
> I'm not familiar with the API but having a look at:
> https://github.com/joyent/libuv/blob/v1.0.0-rc1/include/uv.h#L1381
> Then I wouldn't bother at all about the private fields, just describe the
> constant part of the struct (and do you even need to ? It seems only
> stop_flag is interesting here, you could aswell treat the struct as an
> abstract data type). Now if really needed I would define C accessors for
> each private field and raise if the field is not available for the platform
> and ctypes-bind to these accessors.
> Best,
> Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20141017/0af4b7b4/attachment.html>

More information about the Ctypes mailing list