X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Foreign%2FPtr.hs;h=f57e1ac52d67da9ef45bef84fe5968690c11bdc0;hb=10de2c656f74562b662c22928be85e1b3ccda796;hp=d7f9cb0cde2867a8f5e6bf3464ec4836071d707d;hpb=7f1f4e7a695c402ddd3a1dc2cc7114e649a78ebc;p=ghc-base.git diff --git a/Foreign/Ptr.hs b/Foreign/Ptr.hs index d7f9cb0..f57e1ac 100644 --- a/Foreign/Ptr.hs +++ b/Foreign/Ptr.hs @@ -1,55 +1,154 @@ -{-# OPTIONS -fno-implicit-prelude #-} +{-# OPTIONS_GHC -XNoImplicitPrelude #-} ----------------------------------------------------------------------------- --- +-- | -- Module : Foreign.Ptr -- Copyright : (c) The FFI task force 2001 --- License : BSD-style (see the file libraries/core/LICENSE) +-- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : ffi@haskell.org --- Stability : experimental --- Portability : non-portable --- --- $Id: Ptr.hs,v 1.1 2001/06/28 14:15:03 simonmar Exp $ +-- Stability : provisional +-- Portability : portable -- --- Pointer types. +-- This module provides typed pointers to foreign data. It is part +-- of the Foreign Function Interface (FFI) and will normally be +-- imported via the "Foreign" module. -- ----------------------------------------------------------------------------- module Foreign.Ptr ( - -------------------------------------------------------------------- - -- Data pointers. - - Ptr(..), -- data Ptr a + + -- * Data pointers + + Ptr, -- data Ptr a nullPtr, -- :: Ptr a castPtr, -- :: Ptr a -> Ptr b plusPtr, -- :: Ptr a -> Int -> Ptr b alignPtr, -- :: Ptr a -> Int -> Ptr a minusPtr, -- :: Ptr a -> Ptr b -> Int - - -------------------------------------------------------------------- - -- Function pointers. - - FunPtr(..), -- data FunPtr a + + -- * Function pointers + + FunPtr, -- data FunPtr a nullFunPtr, -- :: FunPtr a castFunPtr, -- :: FunPtr a -> FunPtr b castFunPtrToPtr, -- :: FunPtr a -> Ptr b castPtrToFunPtr, -- :: Ptr a -> FunPtr b - + freeHaskellFunPtr, -- :: FunPtr a -> IO () -- Free the function pointer created by foreign export dynamic. +#ifndef __NHC__ + -- * Integral types with lossless conversion to and from pointers + IntPtr, + ptrToIntPtr, + intPtrToPtr, + WordPtr, + ptrToWordPtr, + wordPtrToPtr +#endif ) where -import Data.Dynamic - #ifdef __GLASGOW_HASKELL__ import GHC.Ptr import GHC.IOBase -import GHC.Err +import GHC.Base +import GHC.Num +import GHC.Read +import GHC.Real +import GHC.Show +import GHC.Enum +import GHC.Word ( Word(..) ) + +import Data.Int +import Data.Word +#else +import Control.Monad ( liftM ) +import Foreign.C.Types +#endif + +import Data.Bits +import Data.Typeable ( Typeable(..), mkTyCon, mkTyConApp ) +import Foreign.Storable ( Storable(..) ) + +#ifdef __NHC__ +import NHC.FFI + ( Ptr + , nullPtr + , castPtr + , plusPtr + , alignPtr + , minusPtr + , FunPtr + , nullFunPtr + , castFunPtr + , castFunPtrToPtr + , castPtrToFunPtr + , freeHaskellFunPtr + ) +#endif + +#ifdef __HUGS__ +import Hugs.Ptr #endif -foreign import "freeHaskellFunctionPtr" unsafe +#ifdef __GLASGOW_HASKELL__ +-- | Release the storage associated with the given 'FunPtr', which +-- must have been obtained from a wrapper stub. This should be called +-- whenever the return value from a foreign import wrapper function is +-- no longer required; otherwise, the storage it uses will leak. +foreign import ccall unsafe "freeHaskellFunctionPtr" freeHaskellFunPtr :: FunPtr a -> IO () +#endif + +#ifndef __NHC__ +# include "HsBaseConfig.h" +# include "CTypes.h" + +# ifdef __GLASGOW_HASKELL__ +-- | An unsigned integral type that can be losslessly converted to and from +-- @Ptr@. +INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word) + -- Word and Int are guaranteed pointer-sized in GHC + +-- | A signed integral type that can be losslessly converted to and from +-- @Ptr@. +INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int) + -- Word and Int are guaranteed pointer-sized in GHC + +-- | casts a @Ptr@ to a @WordPtr@ +ptrToWordPtr :: Ptr a -> WordPtr +ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#))) + +-- | casts a @WordPtr@ to a @Ptr@ +wordPtrToPtr :: WordPtr -> Ptr a +wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#)) + +-- | casts a @Ptr@ to an @IntPtr@ +ptrToIntPtr :: Ptr a -> IntPtr +ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#)) + +-- | casts an @IntPtr@ to a @Ptr@ +intPtrToPtr :: IntPtr -> Ptr a +intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#) + +# else /* !__GLASGOW_HASKELL__ */ + +INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr) +INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr) + +{-# CFILES cbits/PrelIOUtils.c #-} + +foreign import ccall unsafe "__hscore_to_uintptr" + ptrToWordPtr :: Ptr a -> WordPtr + +foreign import ccall unsafe "__hscore_from_uintptr" + wordPtrToPtr :: WordPtr -> Ptr a + +foreign import ccall unsafe "__hscore_to_intptr" + ptrToIntPtr :: Ptr a -> IntPtr + +foreign import ccall unsafe "__hscore_from_intptr" + intPtrToPtr :: IntPtr -> Ptr a -#include "Dynamic.h" -INSTANCE_TYPEABLE1(Ptr,ptrTc,"Ptr") +# endif /* !__GLASGOW_HASKELL__ */ +#endif /* !__NHC_ */