<div dir="ltr">Jeremy,<div><br></div><div>The allocate/coerce idea sounds great, however that won't play nicely with my cross-platform needs... (unless I am misunderstanding something).</div><div><br></div><div>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.</div><div><br></div><div>Thank you.</div><div><br></div><div>Trevor</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 20, 2014 at 4:25 AM, Jeremy Yallop <span dir="ltr"><<a href="mailto:yallop@gmail.com" target="_blank">yallop@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 18 October 2014 12:49, Trevor Smith <<a href="mailto:trevorsummerssmith@gmail.com">trevorsummerssmith@gmail.com</a>> wrote:<br>
> Your suggestion makes sense. If I were to do that -- any suggestions of how<br>
> to deal with freeing that memory? Ideally, without adding a lifecycle "free"<br>
> call to the user library. My thoughts would be to store the cstruct<br>
> reference (malloced by C) in a ref in OCaml, then attach a finalizer to the<br>
> OCaml ref that would call the corresponding C free.<br>
<br>
</span>That approach should work, I think.<br>
<br>
Another possibility to allocate the struct using Ctypes.allocate or<br>
Ctypes.allocate_n -- e.g. by allocating an array of char and coercing<br>
the result:<br>
<br>
val allocate : ?finalise:('a ptr -> unit) -> 'a typ -> 'a -> 'a ptr<br>
<a href="http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate" target="_blank">http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate</a><br>
<br>
val allocate_n : ?finalise:('a ptr -> unit) -> 'a typ -> count:int -> 'a ptr<br>
<a href="http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate_n" target="_blank">http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALallocate_n</a><br>
<br>
val coerce : 'a typ -> 'b typ -> 'a -> 'b<br>
<a href="http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALcoerce" target="_blank">http://ocamllabs.github.io/ocaml-ctypes/Ctypes.html#VALcoerce</a><br>
<br>
The memory allocated by 'allocate' will be freed automatically when<br>
there are no OCaml references to the object. The optional 'finalise'<br>
argument is for associating additional cleanup functions, not for<br>
freeing the memory. You may be able to use it to call uv_loop_close,<br>
for example.<br>
<span class=""><br>
> Aside: I wonder if ctypes could be extended to deal with this cross platform<br>
> issue fully in ctypes-land? Have others thought about this?<br>
<br>
</span>Yes: we're planning to add support for obtaining struct layout from C,<br>
much as generated functions stubs are checked against C declarations:<br>
<br>
<a href="https://github.com/ocamllabs/ocaml-ctypes/issues/62" target="_blank">https://github.com/ocamllabs/ocaml-ctypes/issues/62</a><br>
<br>
The feature is taking a little while longer than anticipated to land,<br>
unfortunately.<br>
<span class=""><br>
> Also: are the maintainers open to documentation contributions? I noticed<br>
> that a lot of these questions on the FAQ are currently unanswered. I would<br>
> be happy to throw a couple commits with documentation suggestions for the<br>
> problems that I am running into.<br>
<br>
</span>Yes: documentation contributions are very welcome.<br>
</blockquote></div><br></div>