
Hello, Examples of instances of the Storable class please. Thank you, Vasili

This uses hsc2hs. As far as I can tell, alignment is unused. I've never had an error from using 'undefined' there.
#include "c_interface.h"
instance Storable Color where sizeOf _ = #size Color alignment _ = undefined peek = peek_color poke = poke_color
peek_color colorp = do r <- (#peek Color, r) colorp :: IO CUChar g <- (#peek Color, g) colorp :: IO CUChar b <- (#peek Color, b) colorp :: IO CUChar a <- (#peek Color, a) colorp :: IO CUChar return $ Color (d r) (d g) (d b) (d a) where d uchar = fromIntegral uchar / 255.0
poke_color colorp (Color r g b a) = do (#poke Color, r) colorp (c r) (#poke Color, g) colorp (c g) (#poke Color, b) colorp (c b) (#poke Color, a) colorp (c a) where c double = Util.c_uchar (floor (double*255))

On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge
This uses hsc2hs. As far as I can tell, alignment is unused. I've never had an error from using 'undefined' there.
Some architectures require all sorts of wacky alignments. E.g. floats may need to be 4 byte aligned, so it's very important that you don't poke a single byte, and then a float, you need to pad with 3 empty bytes first (assuming the whole structure starts at a 4 byte alignment). -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862

On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge
wrote: This uses hsc2hs. As far as I can tell, alignment is unused. I've never had an error from using 'undefined' there.
Some architectures require all sorts of wacky alignments. E.g. floats may need to be 4 byte aligned, so it's very important that you don't poke a single byte, and then a float, you need to pad with 3 empty bytes first (assuming the whole structure starts at a 4 byte alignment).
Does that mean that if ghc is on a sufficiently wacky architecture it will evaluate "alignment" and throw undefined? And if that's the case it would seem that you can't write "alignment" without introducing platform dependencies. I've also heard that even on x86, if you don't have things aligned properly, structure access will be slow. But I'd assume since I'm poking into a C structure, either the C compiler will have taken care of alignment or its definition in C will have to include some manual padding, and so the haskell side doesn't have to worry about it at all. The FFI doc doesn't really talk about the alignment method at all, so I don't really understand how to write one or how it's used.

On Tue, Apr 22, 2008 at 10:48 PM, Evan Laforge
On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge
wrote: This uses hsc2hs. As far as I can tell, alignment is unused. I've never had an error from using 'undefined' there.
Some architectures require all sorts of wacky alignments. E.g. floats may need to be 4 byte aligned, so it's very important that you don't poke a single byte, and then a float, you need to pad with 3 empty bytes first (assuming the whole structure starts at a 4 byte alignment).
Does that mean that if ghc is on a sufficiently wacky architecture it will evaluate "alignment" and throw undefined? And if that's the case it would seem that you can't write "alignment" without introducing platform dependencies.
I've also heard that even on x86, if you don't have things aligned properly, structure access will be slow. But I'd assume since I'm poking into a C structure, either the C compiler will have taken care of alignment or its definition in C will have to include some manual padding, and so the haskell side doesn't have to worry about it at all.
The FFI doc doesn't really talk about the alignment method at all, so I don't really understand how to write one or how it's used.
Well I know that on the Xbox 360 (PowerPC with some extensions basically) alignment problems will sometimes cause slowness, and sometimes a crash depending on the data type. I can't remember which ones are which though, and usually the compiler takes care of it, but in cases such as this when you're manually poking bytes into a buffer, you do need to be very careful and make sure that anything actually using these values will either have the data in the correct alignment right away, or will copy it into an aligned variable before doing anything with it. -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862

Hello Evan, Wednesday, April 23, 2008, 1:48:30 AM, you wrote:
The FFI doc doesn't really talk about the alignment method at all, so I don't really understand how to write one or how it's used.
write: easy. just specify how much data shoulkd be aligned. for primitive datatypes this usually equals to datasize, for complex structures this should be the same as maximum alignment of elements involved: instance Storable Float alignment = 4 instance Storable (a,b) alignment = maximum [alignment a, alignment b] use: just align memory blocks allocated to store this datatype. usual alignment technique is: alloc a = (allocBytes (sizeOf a + alignment a - 1) + (alignment a - 1)) .&. (alignment a - 1) well, many standard allocators such as C malloc, actually provide you blocks with a maximum alignment required to store any (primitive) type, so you don't need to worry about it -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hello,
Below is a snippet of my code .... please look for the "poke" call
with "<<<<<<<<<<<<<". This causes a "hang" when I run a test case. If I
comment out the "poke" call then test case runs.
Vasili
-- |Correspond to some of the int flags from C's fcntl.h.
data MQFlags =
MQFlags {
append1 :: Bool, -- ^ O_APPEND
exclusive1 :: Bool, -- ^ O_EXCL
noctty1 :: Bool, -- ^ O_NOCTTY
nonBlock1 :: Bool, -- ^ O_NONBLOCK
trunc1 :: Bool -- ^ O_TRUNC
}
-- |Default values for the 'MQFlags' type. False for each of
-- append, exclusive, noctty, nonBlock, and trunc.
-- TBD WNH MQdefaultFileFlags :: MQFlags
defaultMQFlags =
MQFlags {
append1 = False,
exclusive1 = False,
noctty1 = False,
nonBlock1 = False,
trunc1 = False
}
data MQAttributes =
MQAttributes {
flags :: MQFlags,
maxMsgNum :: Int,
maxMsgSize :: Int,
curNumMsgs :: Int
}
instance Storable MQAttributes where
sizeOf (MQAttributes flags maxMsgNum maxMsgSize curNumMsgs) = 4
instance Storable MQFlags where
sizeOf (MQFlags append1 exclusive1 noctty1 nonBlock1 trunc1) = 1
-- |Open and optionally create this message queue. See 'System.Posix.Files'
-- for information on how to use the 'FileMode' type.
mqOpen :: String
-> MQOpenMode
-> Maybe FileMode -- ^Just x => creates the file with the given
modes, Nothing => the file must exist.
-> MQFlags
-> MQAttributes
-> IO Fd
mqOpen name how maybe_mode (MQFlags append1 exclusive1 noctty1
nonBlock1 truncate1) (MQAttributes flags
maxMsgNum maxMsgSize curNumMsgs) = do
withCString name $ \ p_name -> do
putStrLn ("name -> " ++ name)
allocaBytes (#const sizeof(struct mq_attr)) $ \ p_attrs -> do
(#poke struct mq_attr, mq_flags) p_attrs flags
<<<<<<<<<<<<<<<<<<<
On Tue, Apr 22, 2008 at 5:05 PM, Bulat Ziganshin
Hello Evan,
Wednesday, April 23, 2008, 1:48:30 AM, you wrote:
The FFI doc doesn't really talk about the alignment method at all, so I don't really understand how to write one or how it's used.
write: easy. just specify how much data shoulkd be aligned. for primitive datatypes this usually equals to datasize, for complex structures this should be the same as maximum alignment of elements involved:
instance Storable Float alignment = 4
instance Storable (a,b) alignment = maximum [alignment a, alignment b]
use: just align memory blocks allocated to store this datatype. usual alignment technique is:
alloc a = (allocBytes (sizeOf a + alignment a - 1) + (alignment a - 1)) .&. (alignment a - 1)
well, many standard allocators such as C malloc, actually provide you blocks with a maximum alignment required to store any (primitive) type, so you don't need to worry about it
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Galchin, Vasili wrote:
Hello,
Examples of instances of the Storable class please.
I believe this is linked from the Haskell Wiki: http://therning.org/magnus/archives/315 There are a few comments on the alignment as well. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus.therning@gmail.com http://therning.org/magnus What if I don't want to obey the laws? Do they throw me in jail with the other bad monads? -- Daveman
participants (5)
-
Bulat Ziganshin
-
Evan Laforge
-
Galchin, Vasili
-
Magnus Therning
-
Sebastian Sylvan