From 7196f1ea1790dc0ef7986ae6757bdcf46a286e3f Mon Sep 17 00:00:00 2001 From: chak Date: Thu, 22 May 2003 08:24:33 +0000 Subject: [PATCH] [project @ 2003-05-22 08:24:32 by chak] Added a `FinalizerPtr' synonym as in the FFI Addendum (RC 10). ** ATTENTION ** This will break the "#ifdef __NHC__" and "#ifdef __HUGS__" variants of `ForeignPtr'. See `GHC/ForeignPtr.hs' for the definitions. --- Foreign/ForeignPtr.hs | 1 + Foreign/Marshal/Alloc.hs | 6 +++--- GHC/ForeignPtr.hs | 26 ++++++++++++++++---------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Foreign/ForeignPtr.hs b/Foreign/ForeignPtr.hs index 4870a46..fce4a13 100644 --- a/Foreign/ForeignPtr.hs +++ b/Foreign/ForeignPtr.hs @@ -19,6 +19,7 @@ module Foreign.ForeignPtr ( -- * Finalised data pointers ForeignPtr + , FinalizerPtr , newForeignPtr , addForeignPtrFinalizer , withForeignPtr diff --git a/Foreign/Marshal/Alloc.hs b/Foreign/Marshal/Alloc.hs index a0e375c..65294ce 100644 --- a/Foreign/Marshal/Alloc.hs +++ b/Foreign/Marshal/Alloc.hs @@ -25,11 +25,12 @@ module Foreign.Marshal.Alloc ( 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) ) @@ -150,5 +151,4 @@ foreign import ccall unsafe "stdlib.h free" _free :: Ptr a -> IO () -- | 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 diff --git a/GHC/ForeignPtr.hs b/GHC/ForeignPtr.hs index cefc899..6b19970 100644 --- a/GHC/ForeignPtr.hs +++ b/GHC/ForeignPtr.hs @@ -16,6 +16,7 @@ module GHC.ForeignPtr ( ForeignPtr(..), + FinalizerPtr, newForeignPtr, mallocForeignPtr, mallocForeignPtrBytes, @@ -71,15 +72,20 @@ instance Show (ForeignPtr a) where #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 @@ -134,7 +140,7 @@ mallocForeignPtrBytes (I# size) = do (# 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. @@ -171,7 +177,7 @@ addForeignPtrConcFinalizer f@(MallocPtr fo r) finalizer = do 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 -- 1.7.10.4