Re: [C2hs] Marshal Haskell list into monolithic arrays

From: Udo Stenzel
To: zhu Jun Subject: Re: [C2hs] Marshal Haskell list into monolithic arrays Date: Thu, 17 Aug 2006 17:17:21 +0200 zhu Jun wrote:
There is a 'newArray' operation in MarshallArray to marshal Haskell
Hi Udo and Duncan, Yes, I made a mistake in C syntax. Now, I call the function in Haskell like this: ------------------------- 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 return returnF foreign import ccall "static fmr.h lowPassFilter" f_lowPassFilter :: Ptr (CFloat) -> Ptr (CFloat) -> IO (()) --------------------------- and the C function is defined as: //////////////////////// void lowPassFilter(float *buff, float *sum) { // Work part for (i = 0; i < NUM_TAPS; i++) { printf("buff %d = %f \n", i, *(buff++)); *sum += *(buff++) * coeff[i]; } } //////////////////////// It works well! Thank you so much! Best wishes, Yours, Jun list,
The C function looks like: //////////////////// void lowPassFilter(float *fs[10], float *sum)
This function doesn't take (a pointer to) an array of floats, but (a pointer to) an array of pointers to floats, which I doubt was your intention. You need help programming in C, not help using C2HS. Try a book or ask in news:comp.lang.c
Udo.
<< signature.asc >>

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
Best wishes, Yours, Jun Direct: +46 8 790 4150 Fax: +46 8 751 1793 Post Address: KTH/IMIT/ LECS, Electrum 229, SE-164 40 Kista, Stockholm, Sweden Visiting address: Forum-Building, Isafjordsgatan 39, 8th floor, elevator C
From: "zhu Jun"
To: u.stenzel@web.de CC: C2hs@haskell.org Subject: Re: [C2hs] Marshal Haskell list into monolithic arrays Date: Thu, 17 Aug 2006 21:39:27 +0000 Hi Udo and Duncan,
Yes, I made a mistake in C syntax. Now, I call the function in Haskell like this: ------------------------- 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 return returnF
foreign import ccall "static fmr.h lowPassFilter" f_lowPassFilter :: Ptr (CFloat) -> Ptr (CFloat) -> IO (())
--------------------------- and the C function is defined as: //////////////////////// void lowPassFilter(float *buff, float *sum) {
// Work part for (i = 0; i < NUM_TAPS; i++) { printf("buff %d = %f \n", i, *(buff++)); *sum += *(buff++) * coeff[i]; } } ////////////////////////
It works well! Thank you so much!
Best wishes,
Yours, Jun
From: Udo Stenzel
To: zhu Jun Subject: Re: [C2hs] Marshal Haskell list into monolithic arrays Date: Thu, 17 Aug 2006 17:17:21 +0200 There is a 'newArray' operation in MarshallArray to marshal Haskell
zhu Jun wrote: list,
The C function looks like: //////////////////// void lowPassFilter(float *fs[10], float *sum)
This function doesn't take (a pointer to) an array of floats, but (a pointer to) an array of pointers to floats, which I doubt was your intention. You need help programming in C, not help using C2HS. Try a book or ask in news:comp.lang.c
Udo.
<< signature.asc >>
_______________________________________________ C2hs mailing list C2hs@haskell.org http://www.haskell.org/mailman/listinfo/c2hs

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... http://haskell.org/ghc/docs/latest/html/libraries/base/Foreign-Marshal-Array... In your case you might replace newArray and malloc by withArray and alloca. Duncan
participants (2)
-
Duncan Coutts
-
zhu Jun