+% ------------------------------------------------------------------------------
+% $Id: PrelForeign.lhs,v 1.20 2001/07/16 00:39:04 sof Exp $
%
-% (c) The AQUA Project, Glasgow University, 1994-1996
+% (c) The University of Glasgow, 1994-2000
%
\section[Foreign]{Module @Foreign@}
\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}
-module PrelForeign (
- module PrelForeign,
-#ifndef __PARALLEL_HASKELL__
- ForeignObj(..),
- makeForeignObj,
- writeForeignObj
-#endif
- ) where
+module PrelForeign where
import PrelIOBase
-import PrelST
+import PrelNum -- for fromInteger
import PrelBase
-import PrelCCall
-import PrelAddr
-import PrelGHC
+import PrelPtr
\end{code}
-
%*********************************************************
%* *
-\subsection{Type @ForeignObj@ and its operations}
+\subsection{ForeignPtr}
%* *
%*********************************************************
\begin{code}
-#ifndef __PARALLEL_HASKELL__
---instance CCallable ForeignObj
---instance CCallable ForeignObj#
+data ForeignPtr a = ForeignPtr ForeignObj#
+instance CCallable (ForeignPtr a)
-makeForeignObj :: Addr -> IO ForeignObj
-makeForeignObj (A# obj) = IO ( \ s# ->
- case makeForeignObj# obj s# of
- (# s1#, fo# #) -> (# s1#, ForeignObj fo# #) )
+eqForeignPtr :: ForeignPtr a -> ForeignPtr a -> Bool
+eqForeignPtr (ForeignPtr fo1#) (ForeignPtr fo2#) = eqForeignObj# fo1# fo2#
-eqForeignObj :: ForeignObj -> ForeignObj -> Bool
---makeForeignObj :: Addr -> Addr -> IO ForeignObj
-writeForeignObj :: ForeignObj -> Addr -> IO ()
+instance Eq (ForeignPtr a) where
+ p == q = eqForeignPtr p q
+ p /= q = not (eqForeignPtr p q)
-writeForeignObj (ForeignObj fo#) (A# datum#) = IO ( \ s# ->
- case writeForeignObj# fo# datum# s# of { s1# -> (# s1#, () #) } )
+newForeignPtr :: Ptr a -> IO () -> IO (ForeignPtr a)
+newForeignPtr p finalizer
+ = do fObj <- mkForeignPtr p
+ addForeignPtrFinalizer fObj finalizer
+ return fObj
-eqForeignObj mp1 mp2
- = unsafePerformIO (primEqForeignObj mp1 mp2) /= (0::Int)
+addForeignPtrFinalizer :: ForeignPtr a -> IO () -> IO ()
+addForeignPtrFinalizer (ForeignPtr fo) finalizer =
+ IO $ \s -> case mkWeak# fo () finalizer s of { (# s1, w #) -> (# s1, () #) }
-foreign import "eqForeignObj" primEqForeignObj :: ForeignObj -> ForeignObj -> IO Int
+mkForeignPtr :: Ptr a -> IO (ForeignPtr a) {- not exported -}
+mkForeignPtr (Ptr obj) = IO ( \ s# ->
+ case mkForeignObj# obj s# of
+ (# s1#, fo# #) -> (# s1#, ForeignPtr fo# #) )
-instance Eq ForeignObj where
- p == q = eqForeignObj p q
- p /= q = not (eqForeignObj p q)
-#endif /* !__PARALLEL_HASKELL__ */
-\end{code}
+touchForeignPtr :: ForeignPtr a -> IO ()
+touchForeignPtr (ForeignPtr fo)
+ = IO $ \s -> case touch# fo s of s -> (# s, () #)
-%*********************************************************
-%* *
-\subsection{Unpacking Foreigns}
-%* *
-%*********************************************************
+withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
+withForeignPtr fo io
+ = do r <- io (foreignPtrToPtr fo)
+ touchForeignPtr fo
+ return r
-Primitives for converting Foreigns pointing to external
-sequence of bytes into a list of @Char@s (a renamed version
-of the code above).
+foreignPtrToPtr :: ForeignPtr a -> Ptr a
+foreignPtrToPtr (ForeignPtr fo) = Ptr (foreignObjToAddr# fo)
+
+castForeignPtr (ForeignPtr a) = ForeignPtr a
-\begin{code}
-#ifndef __PARALLEL_HASKELL__
-unpackCStringFO :: ForeignObj -> [Char]
-unpackCStringFO (ForeignObj fo#) = unpackCStringFO# fo#
-
-unpackCStringFO# :: ForeignObj# -> [Char]
-unpackCStringFO# fo {- ptr. to NUL terminated string-}
- = unpack 0#
- where
- unpack nh
- | ch `eqChar#` '\0'# = []
- | otherwise = C# ch : unpack (nh +# 1#)
- where
- ch = indexCharOffForeignObj# fo nh
-
-unpackNBytesFO :: ForeignObj -> Int -> [Char]
-unpackNBytesFO (ForeignObj fo) (I# l) = unpackNBytesFO# fo l
-
-unpackNBytesFO# :: ForeignObj# -> Int# -> [Char]
- -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
-unpackNBytesFO# fo len
- = unpack 0#
- where
- unpack i
- | i >=# len = []
- | otherwise = C# ch : unpack (i +# 1#)
- where
- ch = indexCharOffForeignObj# fo i
-#endif
\end{code}
+
+