reallocBytes, -- :: Ptr a -> Int -> IO (Ptr a)
free, -- :: Ptr a -> IO ()
- finalizerFree -- :: FunPtr (Ptr a -> IO ())
+ finalizerFree -- :: FinalizerPtr a
) where
import Data.Maybe
import Foreign.Ptr ( Ptr, nullPtr, FunPtr )
+import Foreign.ForeignPtr ( FinalizerPtr )
import Foreign.C.Types ( CSize )
import Foreign.Storable ( Storable(sizeOf) )
-- | A pointer to a foreign function equivalent to 'free', which may be used
-- as a finalizer for storage allocated with 'malloc' or 'mallocBytes'.
-foreign import ccall unsafe "stdlib.h &free"
- finalizerFree :: FunPtr (Ptr a -> IO ())
+foreign import ccall unsafe "stdlib.h &free" finalizerFree :: FinalizerPtr a
module GHC.ForeignPtr
(
ForeignPtr(..),
+ FinalizerPtr,
newForeignPtr,
mallocForeignPtr,
mallocForeignPtrBytes,
#include "Dynamic.h"
INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
-newForeignPtr :: Ptr a -> FunPtr (Ptr a -> IO ()) -> IO (ForeignPtr a)
+-- |A Finaliser is represented as a pointer to a foreign function that, at
+-- finalisation time, gets as an argument a plain pointer variant of the
+-- foreign pointer that the finalizer is associated with.
+--
+type FinalizerPtr a = FunPtr (Ptr a -> IO ())
+
+newForeignPtr :: Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
-- ^Turns a plain memory reference into a foreign object by
--- associating a finaliser - a foreign function given by the @FunPtr@
--- - with the reference. The finaliser will be executed after the
--- last reference to the foreign object is dropped. Note that there
--- is no guarantee on how soon the finaliser is executed after the
--- last reference was dropped; this depends on the details of the
--- Haskell storage manager. The only guarantee is that the finaliser
--- runs before the program terminates.
+-- associating a finaliser with the reference. The finaliser will be executed
+-- after the last reference to the foreign object is dropped. Note that there
+-- is no guarantee on how soon the finaliser is executed after the last
+-- reference was dropped; this depends on the details of the Haskell storage
+-- manager. The only guarantee is that the finaliser runs before the program
+-- terminates.
newForeignPtr p finalizer
= do fObj <- mkForeignPtr p
addForeignPtrFinalizer fObj finalizer
(# s, MallocPtr mbarr# r #)
}
-addForeignPtrFinalizer :: ForeignPtr a -> FunPtr (Ptr a -> IO ()) -> IO ()
+addForeignPtrFinalizer :: ForeignPtr a -> FinalizerPtr a -> IO ()
-- ^This function adds a finaliser to the given foreign object. The
-- finalizer will run /before/ all other finalizers for the same
-- object which have already been registered.
else return ()
foreign import ccall "dynamic"
- mkFinalizer :: FunPtr (Ptr a -> IO ()) -> Ptr a -> IO ()
+ mkFinalizer :: FinalizerPtr a -> Ptr a -> IO ()
foreignPtrFinalizer :: IORef [IO ()] -> Ptr a -> IO ()
foreignPtrFinalizer r p = do