From: ross Date: Sun, 30 Mar 2003 12:20:17 +0000 (+0000) Subject: [project @ 2003-03-30 12:20:16 by ross] X-Git-Tag: nhc98-1-18-release~711 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=06fa098610391a8fa1cad55694de9a2e44494fc4;p=ghc-base.git [project @ 2003-03-30 12:20:16 by ross] withForeignPtr is portable. So is some other stuff, though only Hugs uses the portable versions at present. --- diff --git a/Foreign/ForeignPtr.hs b/Foreign/ForeignPtr.hs index 1941d8f..4870a46 100644 --- a/Foreign/ForeignPtr.hs +++ b/Foreign/ForeignPtr.hs @@ -35,6 +35,8 @@ module Foreign.ForeignPtr ) where +import Foreign.Ptr + #ifdef __NHC__ import NHC.FFI ( ForeignPtr @@ -47,10 +49,6 @@ import NHC.FFI ) #endif -#ifdef __GLASGOW_HASKELL__ -import GHC.ForeignPtr -#endif - #ifdef __HUGS__ import Hugs.ForeignPtr #endif @@ -64,9 +62,66 @@ import GHC.Base import GHC.IOBase import GHC.Num import GHC.Err ( undefined ) +import GHC.ForeignPtr +#endif + +#if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__) +import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree ) +import Data.Dynamic + +#include "Dynamic.h" +INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr") + +instance Eq (ForeignPtr a) where + p == q = foreignPtrToPtr p == foreignPtrToPtr q + +instance Ord (ForeignPtr a) where + compare p q = compare (foreignPtrToPtr p) (foreignPtrToPtr q) + +instance Show (ForeignPtr a) where + showsPrec p f = showsPrec p (foreignPtrToPtr f) #endif #ifndef __NHC__ +withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b +-- ^This is a way to look at the pointer living inside a +-- foreign object. This function takes a function which is +-- applied to that pointer. The resulting 'IO' action is then +-- executed. The foreign object is kept alive at least during +-- the whole action, even if it is not used directly +-- inside. Note that it is not safe to return the pointer from +-- the action and use it after the action completes. All uses +-- of the pointer should be inside the +-- 'withForeignPtr' bracket. The reason for +-- this unsafety is the same as for +-- 'foreignPtrToPtr' below: the finalizer +-- may run earlier than expected, because the compiler can only +-- track usage of the 'ForeignPtr' object, not +-- a 'Ptr' object made from it. +-- +-- This function is normally used for marshalling data to +-- or from the object pointed to by the +-- 'ForeignPtr', using the operations from the +-- 'Storable' class. +withForeignPtr fo io + = do r <- io (foreignPtrToPtr fo) + touchForeignPtr fo + return r +#endif /* ! __NHC__ */ + +#ifdef __HUGS__ +mallocForeignPtr :: Storable a => IO (ForeignPtr a) +mallocForeignPtr = do + r <- malloc + newForeignPtr r finalizerFree + +mallocForeignPtrBytes :: Int -> IO (ForeignPtr a) +mallocForeignPtrBytes n = do + r <- mallocBytes n + newForeignPtr r finalizerFree +#endif /* __HUGS__ */ + +#ifndef __NHC__ mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a) mallocForeignPtrArray = doMalloc undefined where diff --git a/GHC/ForeignPtr.hs b/GHC/ForeignPtr.hs index dad5f04..cefc899 100644 --- a/GHC/ForeignPtr.hs +++ b/GHC/ForeignPtr.hs @@ -21,7 +21,6 @@ module GHC.ForeignPtr mallocForeignPtrBytes, addForeignPtrFinalizer, touchForeignPtr, - withForeignPtr, foreignPtrToPtr, castForeignPtr, newConcForeignPtr, @@ -210,31 +209,6 @@ touchForeignPtr (ForeignPtr fo r) touchForeignPtr (MallocPtr fo r) = IO $ \s -> case touch# fo s of s -> (# s, () #) -withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b --- ^This is a way to look at the pointer living inside a --- foreign object. This function takes a function which is --- applied to that pointer. The resulting 'IO' action is then --- executed. The foreign object is kept alive at least during --- the whole action, even if it is not used directly --- inside. Note that it is not safe to return the pointer from --- the action and use it after the action completes. All uses --- of the pointer should be inside the --- 'withForeignPtr' bracket. The reason for --- this unsafety is the same as for --- 'foreignPtrToPtr' below: the finalizer --- may run earlier than expected, because the compiler can only --- track usage of the 'ForeignPtr' object, not --- a 'Ptr' object made from it. --- --- This function is normally used for marshalling data to --- or from the object pointed to by the --- 'ForeignPtr', using the operations from the --- 'Storable' class. -withForeignPtr fo io - = do r <- io (foreignPtrToPtr fo) - touchForeignPtr fo - return r - foreignPtrToPtr :: ForeignPtr a -> Ptr a -- ^This function extracts the pointer component of a foreign -- pointer. This is a potentially dangerous operations, as if the