Reorganisation of the source tree
[ghc-hetmet.git] / docs / comm / rts-libs / foreignptr.html
diff --git a/docs/comm/rts-libs/foreignptr.html b/docs/comm/rts-libs/foreignptr.html
new file mode 100644 (file)
index 0000000..febe9fe
--- /dev/null
@@ -0,0 +1,68 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+  <head>
+    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
+    <title>The GHC Commentary - why we have <tt>ForeignPtr</tt></title>
+  </head>
+
+  <body BGCOLOR="FFFFFF">
+
+    <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
+
+    <pre>
+data Ptr a = Ptr Addr#
+</pre>
+
+    <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
+    altogether.
+
+    <p><tt>ForeignPtr</tt> is defined like this
+
+    <pre>
+data ForeignPtr a = ForeignPtr ForeignObj#
+</pre>
+
+    <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.
+    <tt>MVar.addMVarFinalizer</tt>.
+
+    <p>So a nicer interface might be something like
+
+<pre>
+class Finalizable a where
+   addFinalizer :: a -> IO () -> IO ()
+
+instance Finalizable (ForeignPtr a) where ...
+instance Finalizable (MVar a) where ...
+</pre>
+
+    <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.
+
+    <p><small>
+<!-- hhmts start -->
+Last modified: Wed Sep 26 09:49:37 BST 2001
+<!-- hhmts end -->
+    </small>
+  </body>
+</html>