[project @ 2003-03-30 12:20:16 by ross]
authorross <unknown>
Sun, 30 Mar 2003 12:20:17 +0000 (12:20 +0000)
committerross <unknown>
Sun, 30 Mar 2003 12:20:17 +0000 (12:20 +0000)
withForeignPtr is portable.  So is some other stuff, though only Hugs
uses the portable versions at present.

Foreign/ForeignPtr.hs
GHC/ForeignPtr.hs

index 1941d8f..4870a46 100644 (file)
@@ -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
index dad5f04..cefc899 100644 (file)
@@ -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