nntpnews.net

Global Usenet Archiver


Register

[Haskell-beginners] How to initialize a C struct with FFI

Reply

  #1  
Old 06-02-10, 12:28 PM
Marco De Oliveira
 
Posts: n/a
Default [Haskell-beginners] How to initialize a C struct with FFI

Hi,

I try to initialize a C struct using the FFI API and ghc stays blocked when
I execute my code (and I got sometimes the error "out of memory").
I do not understand, I do not find any mistake in my code:

{-# OPTIONS -XTypeSynonymInstances #-}

import Foreign.C.Types
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable


type CStruct = (CULong, CULong)

instance Storable CStruct where
sizeOf _ = 2*sizeOf (undefined::CULong)
alignment _ = alignment (undefined::CULong)

test = alloca (\pStruct -> do
poke pStruct ( (fromIntegral 1)::CULong, (fromIntegral 1)::CULong )
)

Thanks for your help.

Marco

_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]

Reply With Quote
  #2  
Old 06-02-10, 12:48 PM
Tim Northover
 
Posts: n/a
Default Re: [Haskell-beginners] How to initialize a C struct with FFI

Marco De Oliveira <deolivem@gmail.com> writes:

> Hi,
>
> I try to initialize a C struct using the FFI API and ghc stays blocked when
> I execute my code (and I got sometimes the error "out of memory").


The poke function is part of the Storable class so you need to define it
for your struct. Haskell is probably going through an infinite loop of
attempts to define poke in terms of pokeElemOff etc and back again.

Tim.
Reply With Quote
  #3  
Old 06-02-10, 07:58 PM
Maciej Piechotka
 
Posts: n/a
Default [Haskell-beginners] Re: How to initialize a C struct with FFI

On Sat, 2010-02-06 at 12:28 +0100, Marco De Oliveira wrote:
> Hi,
>
> I try to initialize a C struct using the FFI API and ghc stays blocked
> when I execute my code (and I got sometimes the error "out of
> memory").
> I do not understand, I do not find any mistake in my code:
>
> {-# OPTIONS -XTypeSynonymInstances #-}
>
> import Foreign.C.Types
> import Foreign.Marshal.Alloc
> import Foreign.Ptr
> import Foreign.Storable
>


It can be written as:
import Foreign
import Foreign.C

>
> type CStruct = (CULong, CULong)
>


I guess that you may want to use newtype or data rather then type to
avoid clashes (and have just 98 + FFI):

newtype CStruct = CStruct (CULong, CULong)
data CStruct = CStruct CULong CULong

> instance Storable CStruct where
> sizeOf _ = 2*sizeOf (undefined::CULong)
> alignment _ = alignment (undefined::CULong)
>


To begin with - where's peek and poke?

> test = alloca (\pStruct -> do
> poke pStruct ( (fromIntegral 1)::CULong, (fromIntegral
> 1)::CULong )
> )


If you just do this at the beginning you may want to use with (notation
as with newtype/data):

with (CStruct (1, 1)) (\ptr -> ...)


Also is main = test?
> Thanks for your help.
>
> Marco


Regards


_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
  #4  
Old 07-02-10, 12:04 AM
Marco De Oliveira
 
Posts: n/a
Default Re: [Haskell-beginners] Re: How to initialize a C struct with FFI

Hi Maciej,

Thanks to have take some time to read this code.

I rewrite using your comments:

import Foreign
import Foreign.C

data CStruct = CStruct (CULong, CULong)

instance Storable CStruct where
sizeOf _ = 2*sizeOf (undefined::CULong)
alignment _ = alignment (undefined::CULong)

test = with (CStruct (1,1)) (\ptr -> return ())

The class Storable does not need to make your own peek and poke method (the
class provide a default implementation).
But with, i have still the behavior when I try this code in ghci.

Please, can you test this code and tell me why ghci stay blocked after
executing the method test.

BR

Marco

2010/2/6 Maciej Piechotka <uzytkownik2@gmail.com>

> On Sat, 2010-02-06 at 12:28 +0100, Marco De Oliveira wrote:
> > Hi,
> >
> > I try to initialize a C struct using the FFI API and ghc stays blocked
> > when I execute my code (and I got sometimes the error "out of
> > memory").
> > I do not understand, I do not find any mistake in my code:
> >
> > {-# OPTIONS -XTypeSynonymInstances #-}
> >
> > import Foreign.C.Types
> > import Foreign.Marshal.Alloc
> > import Foreign.Ptr
> > import Foreign.Storable
> >

>
> It can be written as:
> import Foreign
> import Foreign.C
>
> >
> > type CStruct = (CULong, CULong)
> >

>
> I guess that you may want to use newtype or data rather then type to
> avoid clashes (and have just 98 + FFI):
>
> newtype CStruct = CStruct (CULong, CULong)
> data CStruct = CStruct CULong CULong
>
> > instance Storable CStruct where
> > sizeOf _ = 2*sizeOf (undefined::CULong)
> > alignment _ = alignment (undefined::CULong)
> >

>
> To begin with - where's peek and poke?
>
> > test = alloca (\pStruct -> do
> > poke pStruct ( (fromIntegral 1)::CULong, (fromIntegral
> > 1)::CULong )
> > )

>
> If you just do this at the beginning you may want to use with (notation
> as with newtype/data):
>
> with (CStruct (1, 1)) (\ptr -> ...)
>
>
> Also is main = test?
> > Thanks for your help.
> >
> > Marco

>
> Regards
>
>
> _______________________________________________
> Beginners mailing list
> [email]Beginners@haskell.org[/email]
> [url]http://www.haskell.org/mailman/listinfo/beginners[/url]
>


_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]

Reply With Quote
  #5  
Old 07-02-10, 12:45 AM
Maciej Piechotka
 
Posts: n/a
Default [Haskell-beginners] Re: Re: How to initialize a C struct with FFI

On Sun, 2010-02-07 at 00:04 +0100, Marco De Oliveira wrote:
> Hi Maciej,
>
> Thanks to have take some time to read this code.
>
> I rewrite using your comments:
>
> import Foreign
> import Foreign.C
>
> data CStruct = CStruct (CULong, CULong)
>


Hmm. You combined both newtype and data. While technically it is correct
you have now one level more for program to consider.

> instance Storable CStruct where
> sizeOf _ = 2*sizeOf (undefined::CULong)
> alignment _ = alignment (undefined::CULong)
>
> test = with (CStruct (1,1)) (\ptr -> return ())
>
> The class Storable does not need to make your own peek and poke method
> (the class provide a default implementation).


Not quite. It does in terms of peek/pokeElemOff. Which has in terms of
peek/pokeByteOff which has implementation in terms of peek/poke itself.

Therefore you have infinite recursion - poke calls pokeElemOff which
calls pokeByteOff which calls poke etc.

Even the documentation specifies this:
"Minimal complete definition: sizeOf, alignment, one of peek,
peekElemOff and peekByteOff, and one of poke, pokeElemOff and
pokeByteOff."[1]

Sorry - I forgot about this (I only noticed that something is missing).

> But with, i have still the behavior when I try this code in ghci.
>


My other suggestions only caused to avoid extensions/have type safety
and shorten the function respectively - they did not alter behaviour.

Regards

[1]
[url]http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Foreign-Storable.html#t%3AStorable[/url]


_______________________________________________
Beginners mailing list
[email]Beginners@haskell.org[/email]
[url]http://www.haskell.org/mailman/listinfo/beginners[/url]
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-beginners] Tips for a FFI-wrapped C library? Patrick LeBoutillier fa.haskell 2 01-12-09 10:52 AM
[Haskell-beginners] Windows API and FFI iæfai fa.haskell 1 20-11-09 09:11 AM
Ruby FFI: fixed size char array inside struct daniel åkerud comp.lang.ruby 1 28-10-09 02:26 PM
ffi, struct pointer question Daniel Berger comp.lang.ruby 2 02-08-09 04:07 PM
[Haskell-beginners] FFI, Foreign.Marshal.Array, etc. Alexey Beshenov fa.haskell 2 27-11-08 12:41 AM


All times are GMT +1. The time now is 11:55 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here