<div>a couple of concrete examples:</div>
<div>&nbsp;</div>
<div>typedef struct {char a; int b; char str[8]} type1;</div>
<div>&nbsp;</div>
<div>typedef struct {long c; char d} type2;</div>
<div>&nbsp;</div>
<div>So to pthread_create (just an example function) we could be passing a struct of type1 or a struct of type2 .. i.e. arbitrary length and content ... I am trying to better understand this. I see some of the poke functions mentioned in the FFI. Which one are you alluding to?</div>

<div>&nbsp;</div>
<div>Regards, Vasili<br><br>&nbsp;</div>
<div><span class="gmail_quote">On 2/8/08, <b class="gmail_sendername">Adam Langley</b> &lt;<a href="mailto:agl@imperialviolet.org">agl@imperialviolet.org</a>&gt; wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">On Feb 8, 2008 9:13 AM, Galchin Vasili &lt;<a href="mailto:vigalchin@gmail.com">vigalchin@gmail.com</a>&gt; wrote:<br>
&gt; Let&#39;s take a concrete but &quot;made up&quot; case .. suppose we want to call through<br>&gt; to pthread_create and pass the (void *) argument to pthread_create which in<br>&gt; turn gets interpreted by the pthread that is launched. How would one<br>
&gt; populate the C struct that is passed to the launched pthread keeping in mind<br>&gt; that this C struct is variable in length? From the FFI how would one model<br>&gt; this C struct?<br><br>It tough to be helpful with such a generic request. Here are some<br>
options that you can consider:<br><br>1) Write a wrapper function in C which has a nicer FFI interface to<br>call from Haskell. Using cabal this is pretty painless and, if the<br>interface suits it, it probably the easiest solution.<br>
2) Call pthread_create directly with the FFI. You can give the FFI<br>function a Haskell type with &#39;Ptr ()&#39; or &#39;Ptr X&#39;, it doesn&#39;t really<br>matter. However the type system serves you best, do it that way. This<br>
means that you need to populate the struct yourself in Haskell. The<br>issue with this is that the local system defines lots of things like<br>padding and alignment which mean that the layout of the structure in<br>memory is complex and platform specific. Tools like hsc2hs[1] or c2hs<br>
will be very helpful here. Dealing with the variable length probably<br>isn&#39;t an issue. Usually variable length structures have a fixed header<br>and a variable tail, where the tail is an array of primitives. You can<br>
malloc the correct sized region, use one of the previous tools to fill<br>in the fixed header and then use poke to complete the tail.<br><br>I might be able to be more helpful if you give the actual struct and<br>function prototype that you&#39;re trying to wrap.<br>
<br>Cheers<br><br><br><br>[1] <a href="http://therning.org/magnus/archives/tag/hsc2hs">http://therning.org/magnus/archives/tag/hsc2hs</a><br>[2] <a href="http://www.cse.unsw.edu.au/~chak/haskell/c2hs/">http://www.cse.unsw.edu.au/~chak/haskell/c2hs/</a><br>
<br>--<br>Adam Langley&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="mailto:agl@imperialviolet.org">agl@imperialviolet.org</a><br><a href="http://www.imperialviolet.org">http://www.imperialviolet.org</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 650-283-9641<br>
</blockquote></div><br>