X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=docs%2Fcomm%2Frts-libs%2Fforeignptr.html;fp=docs%2Fcomm%2Frts-libs%2Fforeignptr.html;h=febe9fe422ba34ea61428c2b7ebcd3eab549cc7d;hp=0000000000000000000000000000000000000000;hb=0065d5ab628975892cea1ec7303f968c3338cbe1;hpb=28a464a75e14cece5db40f2765a29348273ff2d2 diff --git a/docs/comm/rts-libs/foreignptr.html b/docs/comm/rts-libs/foreignptr.html new file mode 100644 index 0000000..febe9fe --- /dev/null +++ b/docs/comm/rts-libs/foreignptr.html @@ -0,0 +1,68 @@ + + +
+ +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 + + + +