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

Trevor Smith trevorsummerssmith at gmail.com
Mon Oct 20 12:29:40 BST 2014


Jeremy,

The allocate/coerce idea sounds great, however that won't play nicely with
my cross-platform needs... (unless I am misunderstanding something).

Do you have a vague-eta for the struct-layout-from-C-work? Depending on the
timeline, perhaps it is better for me to finish up the library for unix
then deal with porting it after that feature is ready.

Thank you.

Trevor

On Mon, Oct 20, 2014 at 4:25 AM, Jeremy Yallop <yallop at gmail.com> wrote:

> On 18 October 2014 12:49, Trevor Smith <trevorsummerssmith at gmail.com>
> wrote:
> > Your suggestion makes sense. If I were to do that -- any suggestions of
> how
> > to deal with freeing that memory? Ideally, without adding a lifecycle
> "free"
> > call to the user library. My thoughts would be to store the cstruct
> > reference (malloced by C) in a ref in OCaml, then attach a finalizer to
> the
> > OCaml ref that would call the corresponding C free.
>
> That approach should work, I think.
>
> Another possibility to allocate the struct using Ctypes.allocate or
> Ctypes.allocate_n -- e.g. by allocating an array of char and coercing
> the result:
>
>     val allocate : ?finalise:('a ptr -> unit) -> 'a typ -> 'a -> 'a ptr
>     http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate
>
>     val allocate_n : ?finalise:('a ptr -> unit) -> 'a typ -> count:int ->
> 'a ptr
>     http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate_n
>
>     val coerce : 'a typ -> 'b typ -> 'a -> 'b
>     http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALcoerce
>
> The memory allocated by 'allocate' will be freed automatically when
> there are no OCaml references to the object.  The optional 'finalise'
> argument is for associating additional cleanup functions, not for
> freeing the memory.  You may be able to use it to call uv_loop_close,
> for example.
>
> > Aside: I wonder if ctypes could be extended to deal with this cross
> platform
> > issue fully in ctypes-land? Have others thought about this?
>
> Yes: we're planning to add support for obtaining struct layout from C,
> much as generated functions stubs are checked against C declarations:
>
>     https://github.com/ocamllabs/ocaml-ctypes/issues/62
>
> The feature is taking a little while longer than anticipated to land,
> unfortunately.
>
> > Also: are the maintainers open to documentation contributions? I noticed
> > that a lot of these questions on the FAQ are currently unanswered. I
> would
> > be happy to throw a couple commits with documentation suggestions for the
> > problems that I am running into.
>
> Yes: documentation contributions are very welcome.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20141020/bb55483c/attachment.html>


More information about the Ctypes mailing list