[Haskell] using the ffi with callbacks
Anatoly Zaretsky
anatoly.zaretsky at gmail.com
Thu Jul 20 18:30:47 EDT 2006
On 7/20/06, Evan Martin <martine at danga.com> wrote:
> To elaborate, the code setting this up looks something like this:
> callback_fcn <- ... -- get a FunPtr using "wrapper" from the ffi
> free_fcn <- ... -- as above
> -- the callback data is just stuff that needs freeing
> callback_data <- newStablePtr (callback_fcn, free_fcn)
> register_callback callback_fcn callback_data free_fcn
> And my plan was: within the function free_fcn wraps, free
> callback_fcn, free the StablePtr, and then finally free free_fcn
> itself.
As Taral mentioned there's no need in creating different free_fcn's
for each new callback.
The following may be considered as a linker hack but I see no reason
why it could not work:
> freeCallback :: StablePtr (FunPtr a) -> IO ()
> freeCallback sPtr = do
> fPtr <- deRefStablePtr sPtr
> freeStablePtr sPtr
> freeHaskellFunPtr fPtr
>
> foreign export ccall "_hs_some_really_private_symbol"
> freeCallback :: StablePtr (FunPtr a) -> IO ()
> foreign import ccall "&_hs_some_really_private_symbol"
> free_fcn :: FunPtr (StablePtr (FunPtr a) -> IO ())
And then you can use it like this:
...
callback_data <- newStablePtr callback_fcn
register_callback callback_fcn callback_data free_fcn
--
Tolik
More information about the Haskell
mailing list