summaryrefslogtreecommitdiff
path: root/ghc/docs/comm/rts-libs/foreignptr.html
blob: febe9fe422ba34ea61428c2b7ebcd3eab549cc7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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>