[C2hs] Marshal Haskell list into monolithic arrays
Duncan Coutts
duncan.coutts at worc.ox.ac.uk
Thu Aug 17 19:16:12 EDT 2006
On Thu, 2006-08-17 at 21:49 +0000, zhu Jun wrote:
> I freed 'mfs' and 'fsP2' before return also.
>
> >lowPassFilter :: [CFloat] -> CFloat
> >lowPassFilter fs = (unsafePerformIO (lowPassFilterWrap fs))
> > where lowPassFilterWrap :: [CFloat] -> IO CFloat
> > lowPassFilterWrap fs = do
> > mfs <- newArray fs
> > fsP2 <- malloc
> > r <- f_lowPassFilter mfs fsP2
> > returnF <- peek fsP2
> > free mfs
> > free fsP2
> > return returnF
By the way, instead of using this pattern:
thing <- malloc
...
free thing
you can use the with* style functions:
alloca $ \thing -> do
...
because that automatically deals with free, so you don't forget. It also
deals with freeing even if there is an exception.
See the documentation for the various Foreign.* modules:
http://haskell.org/ghc/docs/latest/html/libraries/base/Foreign-Marshal-Alloc.html
http://haskell.org/ghc/docs/latest/html/libraries/base/Foreign-Marshal-Array.html
In your case you might replace newArray and malloc by withArray and
alloca.
Duncan
More information about the C2hs
mailing list