3 , ForeignFunctionInterface
5 , GeneralizedNewtypeDeriving
8 -----------------------------------------------------------------------------
10 -- Module : Foreign.Ptr
11 -- Copyright : (c) The FFI task force 2001
12 -- License : BSD-style (see the file libraries/base/LICENSE)
14 -- Maintainer : ffi@haskell.org
15 -- Stability : provisional
16 -- Portability : portable
18 -- This module provides typed pointers to foreign data. It is part
19 -- of the Foreign Function Interface (FFI) and will normally be
20 -- imported via the "Foreign" module.
22 -----------------------------------------------------------------------------
30 castPtr, -- :: Ptr a -> Ptr b
31 plusPtr, -- :: Ptr a -> Int -> Ptr b
32 alignPtr, -- :: Ptr a -> Int -> Ptr a
33 minusPtr, -- :: Ptr a -> Ptr b -> Int
35 -- * Function pointers
37 FunPtr, -- data FunPtr a
38 nullFunPtr, -- :: FunPtr a
39 castFunPtr, -- :: FunPtr a -> FunPtr b
40 castFunPtrToPtr, -- :: FunPtr a -> Ptr b
41 castPtrToFunPtr, -- :: Ptr a -> FunPtr b
43 freeHaskellFunPtr, -- :: FunPtr a -> IO ()
44 -- Free the function pointer created by foreign export dynamic.
47 -- * Integral types with lossless conversion to and from pointers
57 #ifdef __GLASGOW_HASKELL__
65 import GHC.Word ( Word(..) )
70 import Control.Monad ( liftM )
71 import Foreign.C.Types
76 import Foreign.Storable ( Storable(..) )
99 #ifdef __GLASGOW_HASKELL__
100 -- | Release the storage associated with the given 'FunPtr', which
101 -- must have been obtained from a wrapper stub. This should be called
102 -- whenever the return value from a foreign import wrapper function is
103 -- no longer required; otherwise, the storage it uses will leak.
104 foreign import ccall unsafe "freeHaskellFunctionPtr"
105 freeHaskellFunPtr :: FunPtr a -> IO ()
109 # include "HsBaseConfig.h"
112 # ifdef __GLASGOW_HASKELL__
113 -- | An unsigned integral type that can be losslessly converted to and from
114 -- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
115 -- can be marshalled to and from that type safely.
116 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
117 -- Word and Int are guaranteed pointer-sized in GHC
119 -- | A signed integral type that can be losslessly converted to and from
120 -- @Ptr@. This type is also compatible with the C99 type @intptr_t@, and
121 -- can be marshalled to and from that type safely.
122 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
123 -- Word and Int are guaranteed pointer-sized in GHC
125 -- | casts a @Ptr@ to a @WordPtr@
126 ptrToWordPtr :: Ptr a -> WordPtr
127 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
129 -- | casts a @WordPtr@ to a @Ptr@
130 wordPtrToPtr :: WordPtr -> Ptr a
131 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
133 -- | casts a @Ptr@ to an @IntPtr@
134 ptrToIntPtr :: Ptr a -> IntPtr
135 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
137 -- | casts an @IntPtr@ to a @Ptr@
138 intPtrToPtr :: IntPtr -> Ptr a
139 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
141 # else /* !__GLASGOW_HASKELL__ */
143 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
144 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
146 {-# CFILES cbits/PrelIOUtils.c #-}
148 foreign import ccall unsafe "__hscore_to_uintptr"
149 ptrToWordPtr :: Ptr a -> WordPtr
151 foreign import ccall unsafe "__hscore_from_uintptr"
152 wordPtrToPtr :: WordPtr -> Ptr a
154 foreign import ccall unsafe "__hscore_to_intptr"
155 ptrToIntPtr :: Ptr a -> IntPtr
157 foreign import ccall unsafe "__hscore_from_intptr"
158 intPtrToPtr :: IntPtr -> Ptr a
160 # endif /* !__GLASGOW_HASKELL__ */