Commit 159b1202 authored by simonmar's avatar simonmar
Browse files

[project @ 2001-09-26 08:59:42 by simonmar]

Add the text about ForeignPtr vs. Ptr
parent 7a767699
......@@ -69,6 +69,7 @@
<li><a href="rts-libs/prelude.html">Cunning Prelude Code</a>
<li><!-- <a href="rts-libs/arrays.html"> -->Array Libraries</a>
<small>[not available yet]</small>
<li><a href="rts-libs/foreignptr.html">On why we have <tt>ForeignPtr</tt></a>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<title>The GHC Commentary - why we have <tt>ForeignPtr</tt></title>
<h1>On why we have <tt>ForeignPtr</tt></h1>
<p>Unfortunately it isn't possible to add a finalizer to a normal
<tt>Ptr a</tt>. We already have a generic finalization mechanism:
see the Weak module in package lang. But the only reliable way to
use finalizers is to attach one to an atomic heap object - that
way the compiler's optimiser can't interfere with the lifetime of
the object.
<p>The <tt>Ptr</tt> type is really just a boxed address - it's
defined like
data Ptr a = Ptr Addr#
<p>where <tt>Addr#</tt> is an unboxed native address (just a 32-
or 64- bit word). Putting a finalizer on a <tt>Ptr</tt> is
dangerous, because the compiler's optimiser might remove the box
<p><tt>ForeignPtr</tt> is defined like this
data ForeignPtr a = ForeignPtr ForeignObj#
<p>where <tt>ForeignObj#</tt> is a *boxed* address, it corresponds
to a real heap object. The heap object is primitive from the
point of view of the compiler - it can't be optimised away. So it
works to attach a finalizer to the <tt>ForeignObj#</tt> (but not
to the <tt>ForeignPtr</tt>!).
<p>There are several primitive objects to which we can attach
finalizers: <tt>MVar#</tt>, <tt>MutVar#</tt>, <tt>ByteArray#</tt>,
etc. We have special functions for some of these: eg.
<p>So a nicer interface might be something like
class Finalizable a where
addFinalizer :: a -> IO () -> IO ()
instance Finalizable (ForeignPtr a) where ...
instance Finalizable (MVar a) where ...
<p>So you might ask why we don't just get rid of <tt>Ptr</tt> and
rename <tt>ForeignPtr</tt> to <tt>Ptr</tt>. The reason for that
is just efficiency, I think.
<!-- hhmts start -->
Last modified: Wed Sep 26 09:49:37 BST 2001
<!-- hhmts end -->
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment