[Haskell-cafe] FFI and modifying haskell memory

Sven Panne Sven.Panne at aedion.de
Sat Oct 29 08:13:53 EDT 2005


Am Montag, 24. Oktober 2005 17:20 schrieb Joel Reymont:
> Is "with" better than allocaBytes?

"with" is just a utility function around "alloca" and "poke", where "alloca" 
is another utility function around "allocaBytes". Here the code from the 
repository:

   with val f  =
     alloca $ \ptr -> do
       poke ptr val
       res <- f ptr
       return res

(Hmmm, why not simplify the last two lines to just "f ptr"?) GHC does some 
tricky things you probably don't want to know about :-) to do something 
better for "alloca" than an exception-protected "malloc"/"free" pair.

In a nutshell: "with" is not better than "allocaBytes", it is something 
different. "with" can be used to pass a Storable Haskell value in a temporary 
memory buffer to a function, while "allocaBytes" only does plain temporary 
memory allocation.

Cheers,
   S.


More information about the Haskell-Cafe mailing list