<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Haskell and C&#8212;structs</title>
	<atom:link href="http://therning.org/magnus/archives/315/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus/archives/315</link>
	<description>Incoherent mumblings</description>
	<pubDate>Tue, 06 Jan 2009 03:24:36 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Creighton Hogg</title>
		<link>http://therning.org/magnus/archives/315#comment-153262</link>
		<dc:creator>Creighton Hogg</dc:creator>
		<pubDate>Thu, 09 Oct 2008 18:49:02 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/315#comment-153262</guid>
		<description>&lt;p&gt;Wow, a year later and I still found this post rather useful.  I had been trying to make sense of how I should be using the (#peek) &#38; (#poke) directives in hsc2hs, and this was very helpful.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Wow, a year later and I still found this post rather useful.  I had been trying to make sense of how I should be using the (#peek) &amp; (#poke) directives in hsc2hs, and this was very helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mauke</title>
		<link>http://therning.org/magnus/archives/315#comment-77836</link>
		<dc:creator>mauke</dc:creator>
		<pubDate>Thu, 23 Aug 2007 20:49:43 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/315#comment-77836</guid>
		<description>&lt;p&gt;I use this trick for computing the alignment:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#include &#60;stddef.h&#62;
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)

instance Storable Foo where
    sizeOf    _ = #size struct foo
    alignment _ = #alignment struct foo
    {- ... -}
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>I use this trick for computing the alignment:</p>
<pre><code>#include &lt;stddef.h&gt;
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)

instance Storable Foo where
    sizeOf    _ = #size struct foo
    alignment _ = #alignment struct foo
    {- ... -}
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: DeeJay</title>
		<link>http://therning.org/magnus/archives/315#comment-77254</link>
		<dc:creator>DeeJay</dc:creator>
		<pubDate>Wed, 15 Aug 2007 19:59:34 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/315#comment-77254</guid>
		<description>&lt;p&gt;I am very confident that as long as you define the alignment of a struct in terms of the alignment of basic types within the struct, rather than a constant/literal then you should be fine. E.g. an Int maybe 4 byte aligned on a 32bit architecture and 8 byte aligned on a 64bit architecture (I don't know) but as long as you define the alignment in terms of something else you should be correct. Having an alignment that is too small is potentially disastrous for some architectures, however having an alignment that is too big will not cause any problems other than wasting space (fragmentation) and less efficient use of the cache. As alignment values are powers of two, any larger alignment will be an integer multiple of any smaller alignment.&lt;/p&gt;

&lt;p&gt;Anyway after further exploration down the rabbit hole that is GHC's source code I am pretty sure that the alignment field in the type class Storable is not actually used in the implementation of allocating memory. It seems to be purposed as information that can be queried by the programmer and little more. So having an alignment of &lt;code&gt;1&lt;/code&gt; does no more than mislead a programmer... not the low level allocation routines.&lt;/p&gt;

&lt;p&gt;For what it is worth, here is an interesting approach to &lt;a href="http://www.monkeyspeak.com/alignment/" rel="nofollow"&gt;computing alignment using C++ templates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Alignment becomes of particular concern when dealing with SSE on x86 and AltiVec on PowerPC. As these instructions work on 128bit/16byte data chunks, they must be 16 byte aligned. I'm assuming that GHC doesn't support these instructions, however if you wanted to add it, you would have to make sure that the allocation routines respected these constraints... and thus the flip-side of my above observation is that defining &lt;code&gt;alignment&lt;/code&gt; to be 16 wouldn't cut it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I am very confident that as long as you define the alignment of a struct in terms of the alignment of basic types within the struct, rather than a constant/literal then you should be fine. E.g. an Int maybe 4 byte aligned on a 32bit architecture and 8 byte aligned on a 64bit architecture (I don&#8217;t know) but as long as you define the alignment in terms of something else you should be correct. Having an alignment that is too small is potentially disastrous for some architectures, however having an alignment that is too big will not cause any problems other than wasting space (fragmentation) and less efficient use of the cache. As alignment values are powers of two, any larger alignment will be an integer multiple of any smaller alignment.</p>
<p>Anyway after further exploration down the rabbit hole that is GHC&#8217;s source code I am pretty sure that the alignment field in the type class Storable is not actually used in the implementation of allocating memory. It seems to be purposed as information that can be queried by the programmer and little more. So having an alignment of <code>1</code> does no more than mislead a programmer&#8230; not the low level allocation routines.</p>
<p>For what it is worth, here is an interesting approach to <a href="http://www.monkeyspeak.com/alignment/" rel="nofollow">computing alignment using C++ templates</a>.</p>
<p>Alignment becomes of particular concern when dealing with SSE on x86 and AltiVec on PowerPC. As these instructions work on 128bit/16byte data chunks, they must be 16 byte aligned. I&#8217;m assuming that GHC doesn&#8217;t support these instructions, however if you wanted to add it, you would have to make sure that the allocation routines respected these constraints&#8230; and thus the flip-side of my above observation is that defining <code>alignment</code> to be 16 wouldn&#8217;t cut it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Magnus</title>
		<link>http://therning.org/magnus/archives/315#comment-76702</link>
		<dc:creator>Magnus</dc:creator>
		<pubDate>Thu, 09 Aug 2007 06:35:31 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/315#comment-76702</guid>
		<description>&lt;p&gt;DeeJay,&lt;/p&gt;

&lt;p&gt;Thanks for that excellent comment.  My original code still has some question marks after the alignment since I wasn't sure of it.  I'll modify the post to correct for my new-found knowledge.&lt;/p&gt;

&lt;p&gt;One thing I'm still wondering though, is it &lt;em&gt;always&lt;/em&gt; that easy, just pick the size of the largest (built-in) data type within the struct, or will the CPU's address size or word size ever play a role?  I guess what I'm really wondering is if I have to take special care to write code to run on both 32-bit and 64-bit platforms?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>DeeJay,</p>
<p>Thanks for that excellent comment.  My original code still has some question marks after the alignment since I wasn&#8217;t sure of it.  I&#8217;ll modify the post to correct for my new-found knowledge.</p>
<p>One thing I&#8217;m still wondering though, is it <em>always</em> that easy, just pick the size of the largest (built-in) data type within the struct, or will the CPU&#8217;s address size or word size ever play a role?  I guess what I&#8217;m really wondering is if I have to take special care to write code to run on both 32-bit and 64-bit platforms?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DeeJay</title>
		<link>http://therning.org/magnus/archives/315#comment-76700</link>
		<dc:creator>DeeJay</dc:creator>
		<pubDate>Thu, 09 Aug 2007 05:56:48 +0000</pubDate>
		<guid isPermaLink="false">http://therning.org/magnus/archives/315#comment-76700</guid>
		<description>&lt;p&gt;Thanks for the informative post. When messing around with the foreign function interface some time ago I certainly could have used this information. I spent quite some time reading up on structure packing and alignment. There seems to be a fairly standard way of packing structures, however I'm sure there are plenty of exceptions and oddities. The hsc2hs special constructs take the pain out of &lt;em&gt;poking&lt;/em&gt; and &lt;em&gt;peeking&lt;/em&gt; data to and from structs. Otherwise you would have to determine the offsets manually (taking padding into account) and code that explicitly in Haskell. The hsc2hs special constructs just use some C in the process of the Haskell code generation to compute the offsets.&lt;/p&gt;

&lt;p&gt;So your implementation of &lt;code&gt;sizeOf&lt;/code&gt;, &lt;code&gt;peek&lt;/code&gt; and &lt;code&gt;poke&lt;/code&gt; are fine, however defining &lt;code&gt;alignment&lt;/code&gt; to be &lt;code&gt;1&lt;/code&gt; is not! An incorrect alignment will work for x86 however a program will suffer a significant performance penalty and in other architectures it will raise an exception in the processor!&lt;/p&gt;

&lt;p&gt;Wikipedia has an entry on &lt;a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow"&gt;struct alignment&lt;/a&gt; which references &lt;a href="http://msdn2.microsoft.com/en-us/library/ms253949.aspx" rel="nofollow"&gt;a short definition and example&lt;/a&gt; of alignment on MSDN, an article on IBM developerworks discussing the &lt;a href="http://www-128.ibm.com/developerworks/library/pa-dalign/" rel="nofollow"&gt;performance issues with alignment&lt;/a&gt; as well as a further &lt;a href="http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm" rel="nofollow"&gt;article about alignment&lt;/a&gt;. In addition to its article, MSDN has a collection of &lt;a href="http://msdn2.microsoft.com/en-us/library/71kf49f1.aspx" rel="nofollow"&gt;alignment examples&lt;/a&gt;. As an aside here is an article discussing the &lt;a href="http://www.goingware.com/tips/getting-started/alignment.html" rel="nofollow"&gt;implications of structure padding&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To summarize: Your particular struct should have an alignment of 4 not 1. An int (32bit) has a size of 4 bytes and an alignment of 4 (You can verify this with ghci). Therefore an int must be placed at a memory location which is divisible by 4. When an int is within a structure, the structure is padded to ensure that the int starts at a 4 byte boundary relative to the start of the structure. However if the structure were to be misaligned in memory the int would not aligned in the global sense. Therefore the structure should also have an alignment of 4. To correct your specific example:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code&gt;alignment _ = alignment (undefined::CInt)&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In general the struct should have the same alignment as the largest data type within the struct. So for example a double is 8 byte aligned on Windows and 4 byte aligned on Linux and OS X, so a struct with a double or a 64 bit integer is likely to be the largest data field and thus the struct will have a matching alignment. While all of the links above discuss structure padding rather than structure alignment explicitly, I believe my rule is correct and that the same logic holds for C unions.&lt;/p&gt;

&lt;p&gt;Finally, GCC has an &lt;a href="http://gcc.gnu.org/onlinedocs/gcc/Alignment.html" rel="nofollow"&gt;extension&lt;/a&gt; which allows one to programmatically query the alignment for data types. You can use this to experiment with more complex structs such as the examples that MSDN provide and verify my claims.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for the informative post. When messing around with the foreign function interface some time ago I certainly could have used this information. I spent quite some time reading up on structure packing and alignment. There seems to be a fairly standard way of packing structures, however I&#8217;m sure there are plenty of exceptions and oddities. The hsc2hs special constructs take the pain out of <em>poking</em> and <em>peeking</em> data to and from structs. Otherwise you would have to determine the offsets manually (taking padding into account) and code that explicitly in Haskell. The hsc2hs special constructs just use some C in the process of the Haskell code generation to compute the offsets.</p>
<p>So your implementation of <code>sizeOf</code>, <code>peek</code> and <code>poke</code> are fine, however defining <code>alignment</code> to be <code>1</code> is not! An incorrect alignment will work for x86 however a program will suffer a significant performance penalty and in other architectures it will raise an exception in the processor!</p>
<p>Wikipedia has an entry on <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="nofollow">struct alignment</a> which references <a href="http://msdn2.microsoft.com/en-us/library/ms253949.aspx" rel="nofollow">a short definition and example</a> of alignment on MSDN, an article on IBM developerworks discussing the <a href="http://www-128.ibm.com/developerworks/library/pa-dalign/" rel="nofollow">performance issues with alignment</a> as well as a further <a href="http://www.eventhelix.com/RealtimeMantra/ByteAlignmentAndOrdering.htm" rel="nofollow">article about alignment</a>. In addition to its article, MSDN has a collection of <a href="http://msdn2.microsoft.com/en-us/library/71kf49f1.aspx" rel="nofollow">alignment examples</a>. As an aside here is an article discussing the <a href="http://www.goingware.com/tips/getting-started/alignment.html" rel="nofollow">implications of structure padding</a>.</p>
<p>To summarize: Your particular struct should have an alignment of 4 not 1. An int (32bit) has a size of 4 bytes and an alignment of 4 (You can verify this with ghci). Therefore an int must be placed at a memory location which is divisible by 4. When an int is within a structure, the structure is padded to ensure that the int starts at a 4 byte boundary relative to the start of the structure. However if the structure were to be misaligned in memory the int would not aligned in the global sense. Therefore the structure should also have an alignment of 4. To correct your specific example:</p>
<blockquote>
<p><code>alignment _ = alignment (undefined::CInt)</code></p>
</blockquote>
<p>In general the struct should have the same alignment as the largest data type within the struct. So for example a double is 8 byte aligned on Windows and 4 byte aligned on Linux and OS X, so a struct with a double or a 64 bit integer is likely to be the largest data field and thus the struct will have a matching alignment. While all of the links above discuss structure padding rather than structure alignment explicitly, I believe my rule is correct and that the same logic holds for C unions.</p>
<p>Finally, GCC has an <a href="http://gcc.gnu.org/onlinedocs/gcc/Alignment.html" rel="nofollow">extension</a> which allows one to programmatically query the alignment for data types. You can use this to experiment with more complex structs such as the examples that MSDN provide and verify my claims.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
