From lilozhu at hotmail.com Fri Aug 4 12:17:06 2006 From: lilozhu at hotmail.com (zhu Jun) Date: Fri Aug 4 12:06:18 2006 Subject: [C2hs] No automatic support for marshaling of structures? Message-ID: Hi All, When I was trying to marshal a C structure in Haskell, I met such error: "Illegal structure or union type! There is no automatic support for marshaling of structures and unions". However, if I try to marshal the pointer to a structure, it works well. Could anyone give me any hints? Thank you! The structure I define in C is: ------------- typedef point{ int x,y; }; -------------- And the marshaling code in C2HS is: -------------- newtype Point = Point {#type point#} -------------- Best wishes, Jun Direct: +46 8 790 4150 Fax: +46 8 751 1793 Post Address: KTH/IMIT/ LECS, Electrum 229, SE-164 40 Kista, Sweden Visiting address: Forum-Building, Isafjordsgatan 39, 8th floor, elevator C From chak at cse.unsw.edu.au Fri Aug 4 13:50:16 2006 From: chak at cse.unsw.edu.au (Manuel M T Chakravarty) Date: Fri Aug 4 13:40:23 2006 Subject: [C2hs] No automatic support for marshaling of structures? In-Reply-To: References: Message-ID: <1154713816.6086.398.camel@trinity.localdomain> Hi Jun, > When I was trying to marshal a C structure in Haskell, I met such error: > "Illegal structure or union type! There is no automatic support for > marshaling of structures and unions". However, if I try to marshal the > pointer to a structure, it works well. As the error message says there is no automatic facility for marshalling structs and unions - in fact, there is no one best way to do it. The easiest solution is to define a Haskell version of your C structure > The structure I define in C is: > ------------- > typedef point{ > int x,y; > }; > -------------- So, you would define data Point = Point {x :: Int, y :: Int} Then, define an instance of the type class Storable for Point. (Storable is a class from the FFI addendum, see the GHC Haddock documentation for details.) Afterwards you can use c2hs get and set hooks to marshal between the C and the Haskell representation. Manuel From lilozhu at hotmail.com Thu Aug 17 10:20:58 2006 From: lilozhu at hotmail.com (zhu Jun) Date: Thu Aug 17 10:09:32 2006 Subject: [C2hs] Marshal Haskell list into monolithic arrays Message-ID: Hi all, There is a 'newArray' operation in MarshallArray to marshal Haskell list, with the definination of 'newArray :: Storable a => [a] -> IO (Ptr a)'. However, when I pass the 'Ptr a' to a C function, only the the first element of the list is available in C. Could anyone give some hints? The C function looks like: //////////////////// void lowPassFilter(float *fs[10], float *sum) { // Only *fs[0] is available here, why? } ////////////// with the import call in Haskell like this: foreign import ccall safe "lowPassFilter" lowPassFilter :: Ptr (CFloat) -> Ptr (CFloat) -> IO (()) Should I use ' lowPassFilter :: Ptr ([CFloat]) -> Ptr (CFloat) -> IO (())', and define 'float fs[10]' to be Storable in Haskell? 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 duncan.coutts at worc.ox.ac.uk Thu Aug 17 10:39:39 2006 From: duncan.coutts at worc.ox.ac.uk (Duncan Coutts) Date: Thu Aug 17 10:30:00 2006 Subject: [C2hs] Marshal Haskell list into monolithic arrays In-Reply-To: References: Message-ID: <1155825579.26483.44.camel@localhost> On Thu, 2006-08-17 at 14:20 +0000, zhu Jun wrote: > Hi all, > > There is a 'newArray' operation in MarshallArray to marshal Haskell list, > with the definination of 'newArray :: Storable a => [a] -> IO (Ptr a)'. > However, when I pass the 'Ptr a' to a C function, only the the first > element of the list is available in C. > Could anyone give some hints? > > The C function looks like: > //////////////////// > void lowPassFilter(float *fs[10], float *sum) > { > // Only *fs[0] is available here, why? > } > ////////////// > with the import call in Haskell like this: > foreign import ccall safe "lowPassFilter" lowPassFilter :: Ptr (CFloat) -> > Ptr (CFloat) -> IO (()) How are you calling this? Show us the code where you use lowPassFilter and newArray. > Should I use ' lowPassFilter :: Ptr ([CFloat]) -> Ptr (CFloat) -> IO (())', > and define 'float fs[10]' to be Storable in Haskell? No. Duncan From lilozhu at hotmail.com Thu Aug 17 17:39:27 2006 From: lilozhu at hotmail.com (zhu Jun) Date: Thu Aug 17 17:27:57 2006 Subject: [C2hs] Marshal Haskell list into monolithic arrays In-Reply-To: <20060817151721.GC1551@web.de> Message-ID: 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 > >zhu Jun wrote: > > There is a 'newArray' operation in MarshallArray to marshal Haskell 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 >> From lilozhu at hotmail.com Thu Aug 17 17:49:08 2006 From: lilozhu at hotmail.com (zhu Jun) Date: Thu Aug 17 17:37:39 2006 Subject: [C2hs] Marshal Haskell list into monolithic arrays In-Reply-To: Message-ID: 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 >> >>zhu Jun wrote: >> > There is a 'newArray' operation in MarshallArray to marshal >>Haskell >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 From duncan.coutts at worc.ox.ac.uk Thu Aug 17 19:16:12 2006 From: duncan.coutts at worc.ox.ac.uk (Duncan Coutts) Date: Thu Aug 17 19:06:31 2006 Subject: [C2hs] Marshal Haskell list into monolithic arrays In-Reply-To: References: Message-ID: <1155856572.1061.18.camel@localhost> 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 From kolmodin at dtek.chalmers.se Tue Aug 29 13:26:13 2006 From: kolmodin at dtek.chalmers.se (Lennart Kolmodin) Date: Tue Aug 29 13:14:10 2006 Subject: [C2hs] "Structs" test fails on ppc Message-ID: <44F478B5.8040705@dtek.chalmers.se> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Got a bug report as we tried to mark c2hs stable in Gentoo Linux. c2hs is failing one of the tests on ppc: root# pwd /var/tmp/portage/c2hs-0.14.5/work/c2hs-0.14.5/c2hs/tests root# make ... various tests passing .... - ---=== Output for `structs': structs: smallInt /= -1: Panic! make: *** [structs.run] Error 1 As reported by Stephanie (Wormo) on http://bugs.gentoo.org/show_bug.cgi?id=144678#c9 wormo@gentoo.org Posted the bug here since Trac seems to be a bit... well.. spammed. Cheers, Lennart Kolmodin -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFE9Hi14txYG4KUCuERAhGNAJ9orKUN9nNJmwHtkUgsz0UM6urywACgmeQN J/xzfmptqg2xmoK8ephdAL8= =zdDK -----END PGP SIGNATURE-----