[Haskell-cafe] Is c2hs able to generate FunPtr rather than just the function?

Rafal Kolanski xs at xaph.net
Mon Oct 5 01:18:30 EDT 2009

Hi Everyone,

I'm trying to figure out an elegant way of letting the Haskell garbage 
collector deal with structures allocated from C.

So I have the C pair:
fluid_event_t* new_fluid_event(void);
void delete_fluid_event(fluid_event_t* evt);

I've handled the new_fluid_event thing nicely, ForeignPtr and all. Now 
if I want to add a finalizer to the ForeignPtr via newForeignPtr, and 
for that I need a FunPtr.

So what I need is:

foreign import ccall safe "fluidsynth.h &delete_fluid_event"
   deleteFluidEvent'' :: FunPtr (((Ptr (Event)) -> (IO ())))

(if I use that as a finalizer, everything works nicely)

But the closest I can get to generating something similar with c2hs is:

{# fun delete_fluid_event as ^ {fromEvent `Event'} -> `()' #}

which results in:

foreign import ccall safe "Event.chs.h delete_fluid_event"
   deleteFluidEvent'_ :: ((Ptr (Event)) -> (IO ()))

Is there a neat way to have c2hs generate the FunPtr version for me?


Rafal Kolanski.

More information about the Haskell-Cafe mailing list