[Haskell] Re: using the ffi with callbacks

Simon Marlow simonmarhaskell at gmail.com
Thu Jul 20 08:12:20 EDT 2006


Evan Martin wrote:
> Suppose I have a C function like this:
>  void register_callback(
>    void (*callback_fcn)(void *data),
>    void *callback_data,
>    void (*free_fcn)(void *data));
> I think this is pretty common in C libraries.  The idea is that you
> can register a callback along with a pointer to some data to pass to
> it, and when the C code is done with the callback+data it'll call your
> free_fcn, again passing the same data.
> 
> I'd like to wrap this in Haskell, allowing me to pass in a Haskell
> function as a callback.
> The tricky part is that to pass in Haskell functions, I need to use
> the FFI "wrapper" import, which means I need to later free them.  But
> the only place I can free them is within the "free" callback, and I've
> just discovered this isn't allowed!
...
> However, I discovered this thread, which indicates that having a
> function free its FunPtr is not allowed:
> http://www.haskell.org//pipermail/glasgow-haskell-users/2006-March/009907.html 

My take on this is that it should be allowed, and when we revise the FFI 
definition for Haskell' I think we should pin this down.  Wolfgang Thaller 
argued in that thread that we can reasonably expect to be able to implement it 
for any architecture, and in fact GHC already does implement it for all 
architectures (except for IA64, IIRC).

Cheers,
	Simon


More information about the Haskell mailing list