Attached is a patch to amend the documentation. The diff is a bit hard to read because I used vim to reflow the text after changing it. The final result looks good, but the diff looks bad. Sorry about that!<div><br></div>
<div>Jason<br><div><br><br><div class="gmail_quote">On Tue, Mar 29, 2011 at 10:45 AM, Jason Dagit <span dir="ltr"><<a href="mailto:dagitj@gmail.com">dagitj@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Sending this question to Haskell-Cafe may have been a mistake on my part. Is the library list the right place for this?<div><br></div><div>Since posting this, I'm now convinced that the documentation in the haddocks are wrong. It seems that alloca is designed to never return nullPtr whether that means crashing or throwing an exception.</div>
<div><br></div><div>Do I just update the haddocks and submit a patch some where?<div><div></div><div class="h5"><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Jason Dagit</b> <span dir="ltr"><<a href="mailto:dagitj@gmail.com" target="_blank">dagitj@gmail.com</a>></span><br>
Date: Mon, Mar 28, 2011 at 9:43 PM<br>Subject: How to best deal with nullPtr from alloca and friends?<br>To: Haskell Cafe <<a href="mailto:haskell-cafe@haskell.org" target="_blank">haskell-cafe@haskell.org</a>><br>
<br><br>I was reading up on the documentation for alloca and friends[1], which says, "<span style="font-family:sans-serif;font-size:13px;line-height:11px">If any of the allocation functions fails, a value of <code style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;font-family:monospace;line-height:16px"><a href="http://www.haskell.org/ghc/docs/7.0.2/html/libraries/base-4.3.1.0/Foreign-Ptr.html#v:nullPtr" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;text-decoration:none;color:rgb(196, 69, 29)" target="_blank">nullPtr</a></code> is produced."</span><div>
<span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px">It seems like every example of FFI code that I find which uses alloca ignores the possibility of a nullPtr[2, 3, 4].</span></div>
<div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px">So then I started trying out examples with the help of dmwit and others from #haskell.</span></div>
<div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px">It seems that actually, alloca and friends throw exceptions:</span></div>
<div><font face="sans-serif"><span style="line-height:11px">dmwit> main = allocaArray (2^30) (\ptr -> print ((nullPtr :: Ptr Double) == ptr))</span></font></div><div>
<span style="font-family:sans-serif;font-size:13px;line-height:11px"><div><dmwit> lispy: alloca also throws an exception.</div><div><dmwit> lispy: Or rather, allocaBytes throws an exception, and alloca is implemented in terms of allocaBytes, so I'm *guessing* that alloca throws an exception.</div>
</span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px">I'm on a 64bit version of windows here with more than 4GB of memory to spare for the GHC process. Unfortunately, allocaBytes takes an Int so I can't test it with a request larger than the amount of physical ram I have.</span></div>
<div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px">Just playing around with different arguments to allocaBytes I can get different behavior:</span></div>
<div><font face="sans-serif"><span style="line-height:11px"> <= 2^30 bytes, works perfectly</span></font></div><div><font face="sans-serif"><span style="line-height:11px"> == 2^30 + 2^20 bytes, I get an "<interactive>: out of memory" message and ghci terminates</span></font></div>
<div><font face="sans-serif"><span style="line-height:11px"> == 2^31-1, I get a crash where windows pops up a little dialog saying my program (ghci) has crashed.</span></font></div>
<div><font face="sans-serif"><span style="line-height:11px"><br></span></font></div><div><font face="sans-serif"><span style="line-height:11px">The behavior seems to be inconsistent with the documentation.</span></font></div>
<div><font face="sans-serif"><span style="line-height:11px"><br></span></font></div><div><font face="sans-serif"><span style="line-height:11px">What is the correct behavior for alloca and friends and should I be checking for nullPtr?</span></font></div>
<div><font face="sans-serif"><span style="line-height:11px"><br></span></font></div><div><font face="sans-serif"><span style="line-height:11px">Thanks,</span></font></div>
<div><font face="sans-serif"><span style="line-height:11px">Jason </span></font></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br>
</span></div><div><span style="font-family:sans-serif;font-size:13px;line-height:11px"><br></span></div><div><font face="sans-serif"><span style="line-height:11px">[1] </span></font><a href="http://www.haskell.org/ghc/docs/7.0.2/html/libraries/base-4.3.1.0/Foreign-Marshal-Alloc.html" target="_blank">http://www.haskell.org/ghc/docs/7.0.2/html/libraries/base-4.3.1.0/Foreign-Marshal-Alloc.html</a></div>
<div>[2] <a href="http://en.wikibooks.org/wiki/Haskell/FFI" target="_blank">http://en.wikibooks.org/wiki/Haskell/FFI</a></div><div>[3] <a href="http://www.haskell.org/haskellwiki/HSFFIG/Examples" target="_blank">http://www.haskell.org/haskellwiki/HSFFIG/Examples</a></div>
<div>[4] <a href="http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html" target="_blank">http://book.realworldhaskell.org/read/interfacing-with-c-the-ffi.html</a></div>
</div><br></div></div></div>
</blockquote></div><br></div></div>