From 159b1202de5b64a754499c452cc5f6142c7f349b Mon Sep 17 00:00:00 2001 From: simonmar Date: Wed, 26 Sep 2001 08:59:42 +0000 Subject: [PATCH] [project @ 2001-09-26 08:59:42 by simonmar] Add the text about ForeignPtr vs. Ptr --- ghc/docs/comm/index.html | 1 + ghc/docs/comm/rts-libs/foreignptr.html | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 ghc/docs/comm/rts-libs/foreignptr.html diff --git a/ghc/docs/comm/index.html b/ghc/docs/comm/index.html index ca3b283..eb9f4b5 100644 --- a/ghc/docs/comm/index.html +++ b/ghc/docs/comm/index.html @@ -69,6 +69,7 @@
  • Cunning Prelude Code
  • Array Libraries [not available yet] +
  • On why we have ForeignPtr

    diff --git a/ghc/docs/comm/rts-libs/foreignptr.html b/ghc/docs/comm/rts-libs/foreignptr.html new file mode 100644 index 0000000..febe9fe --- /dev/null +++ b/ghc/docs/comm/rts-libs/foreignptr.html @@ -0,0 +1,68 @@ + + + + + The GHC Commentary - why we have <tt>ForeignPtr</tt> + + + + +

    On why we have ForeignPtr

    + +

    Unfortunately it isn't possible to add a finalizer to a normal + Ptr a. 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. + +

    The Ptr type is really just a boxed address - it's + defined like + +

    +data Ptr a = Ptr Addr#
    +
    + +

    where Addr# is an unboxed native address (just a 32- + or 64- bit word). Putting a finalizer on a Ptr is + dangerous, because the compiler's optimiser might remove the box + altogether. + +

    ForeignPtr is defined like this + +

    +data ForeignPtr a = ForeignPtr ForeignObj#
    +
    + +

    where ForeignObj# 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 ForeignObj# (but not + to the ForeignPtr!). + +

    There are several primitive objects to which we can attach + finalizers: MVar#, MutVar#, ByteArray#, + etc. We have special functions for some of these: eg. + MVar.addMVarFinalizer. + +

    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 ...
    +
    + +

    So you might ask why we don't just get rid of Ptr and + rename ForeignPtr to Ptr. The reason for that + is just efficiency, I think. + +

    + +Last modified: Wed Sep 26 09:49:37 BST 2001 + + + + -- 1.7.10.4