<p>Sending to the list as well.</p>
<div class="gmail_quote">On Jun 19, 2011 9:58 AM, &quot;Antoine Latter&quot; &lt;<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>&gt; wrote:<br type="attribution">&gt; On Jun 19, 2011 7:49 AM, &quot;Bas van Dijk&quot; &lt;<a href="mailto:v.dijk.bas@gmail.com">v.dijk.bas@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>&gt;&gt; On 19 June 2011 14:13, wren ng thornton &lt;<a href="mailto:wren@freegeek.org">wren@freegeek.org</a>&gt; wrote:<br>&gt;&gt; &gt; The documentation claims that<br>&gt;&gt; &gt; the finalizers are run only once the ForeignPtr becomes unreachable,<br>
&gt; which<br>&gt;&gt; &gt; implies that keeping the ForeignPtr is sufficient to keep the foreign<br>&gt;&gt; &gt; object alive.<br>&gt;&gt;<br>&gt;&gt; Right, that was also my thinking when designing the usb library. What<br>
&gt;&gt; made me doubt this was the implementation of storable vectors:<br>&gt;&gt;<br>&gt;&gt;<br>&gt; <a href="http://hackage.haskell.org/packages/archive/vector/0.7.1/doc/html/src/Data-Vector-Storable.html">http://hackage.haskell.org/packages/archive/vector/0.7.1/doc/html/src/Data-Vector-Storable.html</a><br>
&gt;&gt;<br>&gt;&gt; Note that a storable vector stores both a foreign pointer and a<br>&gt;&gt; pointer (presumably so that you don&#39;t need to add an offset to the<br>&gt;&gt; pointer, inside the foreign pointer, each time you use it):<br>
&gt;&gt;<br>&gt;&gt; data Vector a = Vector !(Ptr a) !Int !(ForeignPtr a)<br>&gt;&gt;<br>&gt;&gt; Now, if we follow the above semantics we don&#39;t need to call<br>&gt;&gt; withForeignPtr when we need to read the memory because the foreign<br>
&gt;&gt; pointer is referenced by the vector.<br>&gt;&gt; However the vector library still calls withForeignPtr. For example:<br>&gt;&gt;<br>&gt;&gt;  basicUnsafeIndexM (Vector p _ fp) i = return<br>&gt;&gt;                                      . unsafeInlineIO<br>
&gt;&gt;                                      $ withForeignPtr fp $ \_ -&gt;<br>&gt;&gt;                                        peekElemOff p i<br>&gt;&gt;<br>&gt;&gt; So is the withForeignPtr redundant so that this function can be<br>
&gt;&gt; rewritten to just:<br>&gt;&gt;<br>&gt;&gt;  basicUnsafeIndexM (Vector p _ _) i = return<br>&gt;&gt;                                      . unsafeInlineIO<br>&gt;&gt;                                        peekElemOff p i<br>
&gt;&gt;<br>&gt; <br>&gt; I&#39;d be afraid of optimizations getting ride of the constructor, and since<br>&gt; I&#39;m not using all of the fields I would no longer have a reference to the<br>&gt; foreign ptr.<br>&gt; <br>
&gt; Since withForeignPtr touches the foreign pointer the optimizations won&#39;t<br>&gt; make it go away no matter how it transfoms the function.<br>&gt; <br>&gt; Antoine<br>&gt; <br>&gt;&gt; Regards,<br>&gt;&gt;<br>&gt;&gt; Bas<br>
&gt;&gt;<br>&gt;&gt; _______________________________________________<br>&gt;&gt; Haskell-Cafe mailing list<br>&gt;&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>&gt;&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div>