3 , ForeignFunctionInterface
5 , GeneralizedNewtypeDeriving
7 #ifdef __GLASGOW_HASKELL__
8 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
11 -----------------------------------------------------------------------------
13 -- Module : Foreign.Ptr
14 -- Copyright : (c) The FFI task force 2001
15 -- License : BSD-style (see the file libraries/base/LICENSE)
17 -- Maintainer : ffi@haskell.org
18 -- Stability : provisional
19 -- Portability : portable
21 -- This module provides typed pointers to foreign data. It is part
22 -- of the Foreign Function Interface (FFI) and will normally be
23 -- imported via the "Foreign" module.
25 -----------------------------------------------------------------------------
33 castPtr, -- :: Ptr a -> Ptr b
34 plusPtr, -- :: Ptr a -> Int -> Ptr b
35 alignPtr, -- :: Ptr a -> Int -> Ptr a
36 minusPtr, -- :: Ptr a -> Ptr b -> Int
38 -- * Function pointers
40 FunPtr, -- data FunPtr a
41 nullFunPtr, -- :: FunPtr a
42 castFunPtr, -- :: FunPtr a -> FunPtr b
43 castFunPtrToPtr, -- :: FunPtr a -> Ptr b
44 castPtrToFunPtr, -- :: Ptr a -> FunPtr b
46 freeHaskellFunPtr, -- :: FunPtr a -> IO ()
47 -- Free the function pointer created by foreign export dynamic.
50 -- * Integral types with lossless conversion to and from pointers
60 #ifdef __GLASGOW_HASKELL__
68 import GHC.Word ( Word(..) )
73 import Control.Monad ( liftM )
74 import Foreign.C.Types
79 import Foreign.Storable ( Storable(..) )
102 #ifdef __GLASGOW_HASKELL__
103 -- | Release the storage associated with the given 'FunPtr', which
104 -- must have been obtained from a wrapper stub. This should be called
105 -- whenever the return value from a foreign import wrapper function is
106 -- no longer required; otherwise, the storage it uses will leak.
107 foreign import ccall unsafe "freeHaskellFunctionPtr"
108 freeHaskellFunPtr :: FunPtr a -> IO ()
112 # include "HsBaseConfig.h"
115 # ifdef __GLASGOW_HASKELL__
116 -- | An unsigned integral type that can be losslessly converted to and from
117 -- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
118 -- can be marshalled to and from that type safely.
119 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
120 -- Word and Int are guaranteed pointer-sized in GHC
122 -- | A signed integral type that can be losslessly converted to and from
123 -- @Ptr@. This type is also compatible with the C99 type @intptr_t@, and
124 -- can be marshalled to and from that type safely.
125 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
126 -- Word and Int are guaranteed pointer-sized in GHC
128 -- | casts a @Ptr@ to a @WordPtr@
129 ptrToWordPtr :: Ptr a -> WordPtr
130 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
132 -- | casts a @WordPtr@ to a @Ptr@
133 wordPtrToPtr :: WordPtr -> Ptr a
134 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
136 -- | casts a @Ptr@ to an @IntPtr@
137 ptrToIntPtr :: Ptr a -> IntPtr
138 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
140 -- | casts an @IntPtr@ to a @Ptr@
141 intPtrToPtr :: IntPtr -> Ptr a
142 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
144 # else /* !__GLASGOW_HASKELL__ */
146 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
147 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
149 {-# CFILES cbits/PrelIOUtils.c #-}
151 foreign import ccall unsafe "__hscore_to_uintptr"
152 ptrToWordPtr :: Ptr a -> WordPtr
154 foreign import ccall unsafe "__hscore_from_uintptr"
155 wordPtrToPtr :: WordPtr -> Ptr a
157 foreign import ccall unsafe "__hscore_to_intptr"
158 ptrToIntPtr :: Ptr a -> IntPtr
160 foreign import ccall unsafe "__hscore_from_intptr"
161 intPtrToPtr :: IntPtr -> Ptr a
163 # endif /* !__GLASGOW_HASKELL__ */