[ocaml-ctypes] Spinning process on osx

Jeremy Yallop yallop at gmail.com
Thu Jan 30 13:35:03 GMT 2014


On 30 January 2014 12:59, Daniel Bünzli <daniel.buenzli at erratique.ch> wrote:
> Le jeudi, 30 janvier 2014 à 11:39, Jeremy Yallop a écrit :
>> Here's an example that checks function availability during stub generation
>> and generates either a working stub or a function that raises an
>> exception.
>
> In fact you really need to look them up dynamically (e.g. if you want to distribute binaries). What do you think about this approach (which is basically the technique I wrote about here [1]). Would I be able doable with the current stub generation ?
>
> 1) Lookup function pointer with Dl.dlsym. which results in an otptional fun ptr.
> 2) Generate stub that has a fun ptr as a first argument, in the stub if fun ptr is None, raise, otherwise invoke.

Yes, I think that could be made to work.  If you have C99- or
GCC-style variadic macros, then you could probably generate one stub
per type wrapping a function application macro:

   #define funcall(f, args...) f(args)

Each generated stub would then accept a function pointer and its
arguments, unwrap them at the appropriate type, and apply the
function.  You could keep the check for whether the function pointer
is null in OCaml (my preference), or extend the macro to perform null
checking as well.

Using Foreign.funptr for the function argument would work, but then
you'd still have the dependency on libffi.  It should be possible to
write a simpler version of funptr that only wraps C function pointers
(e.g. as returned from dlsym) rather than OCaml functions, which would
make it possible to eliminate libffi altogether.  Let me know if some
example code would be useful.


More information about the Ctypes mailing list