1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.Ptr
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- This module provides typed pointers to foreign data. It is part
13 -- of the Foreign Function Interface (FFI) and will normally be
14 -- imported via the "Foreign" module.
16 -----------------------------------------------------------------------------
24 castPtr, -- :: Ptr a -> Ptr b
25 plusPtr, -- :: Ptr a -> Int -> Ptr b
26 alignPtr, -- :: Ptr a -> Int -> Ptr a
27 minusPtr, -- :: Ptr a -> Ptr b -> Int
29 -- * Function pointers
31 FunPtr, -- data FunPtr a
32 nullFunPtr, -- :: FunPtr a
33 castFunPtr, -- :: FunPtr a -> FunPtr b
34 castFunPtrToPtr, -- :: FunPtr a -> Ptr b
35 castPtrToFunPtr, -- :: Ptr a -> FunPtr b
37 freeHaskellFunPtr, -- :: FunPtr a -> IO ()
38 -- Free the function pointer created by foreign export dynamic.
41 -- * Integral types with lossless conversion to and from pointers
51 #ifdef __GLASGOW_HASKELL__
59 import GHC.Word ( Word(..) )
64 import Control.Monad ( liftM )
65 import Foreign.C.Types
70 import Foreign.Storable ( Storable(..) )
93 #ifdef __GLASGOW_HASKELL__
94 -- | Release the storage associated with the given 'FunPtr', which
95 -- must have been obtained from a wrapper stub. This should be called
96 -- whenever the return value from a foreign import wrapper function is
97 -- no longer required; otherwise, the storage it uses will leak.
98 foreign import ccall unsafe "freeHaskellFunctionPtr"
99 freeHaskellFunPtr :: FunPtr a -> IO ()
103 # include "HsBaseConfig.h"
106 # ifdef __GLASGOW_HASKELL__
107 -- | An unsigned integral type that can be losslessly converted to and from
108 -- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
109 -- can be marshalled to and from that type safely.
110 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
111 -- Word and Int are guaranteed pointer-sized in GHC
113 -- | A signed integral type that can be losslessly converted to and from
114 -- @Ptr@. This type is also compatible with the C99 type @intptr_t@, and
115 -- can be marshalled to and from that type safely.
116 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
117 -- Word and Int are guaranteed pointer-sized in GHC
119 -- | casts a @Ptr@ to a @WordPtr@
120 ptrToWordPtr :: Ptr a -> WordPtr
121 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
123 -- | casts a @WordPtr@ to a @Ptr@
124 wordPtrToPtr :: WordPtr -> Ptr a
125 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
127 -- | casts a @Ptr@ to an @IntPtr@
128 ptrToIntPtr :: Ptr a -> IntPtr
129 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
131 -- | casts an @IntPtr@ to a @Ptr@
132 intPtrToPtr :: IntPtr -> Ptr a
133 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
135 # else /* !__GLASGOW_HASKELL__ */
137 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
138 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
140 {-# CFILES cbits/PrelIOUtils.c #-}
142 foreign import ccall unsafe "__hscore_to_uintptr"
143 ptrToWordPtr :: Ptr a -> WordPtr
145 foreign import ccall unsafe "__hscore_from_uintptr"
146 wordPtrToPtr :: WordPtr -> Ptr a
148 foreign import ccall unsafe "__hscore_to_intptr"
149 ptrToIntPtr :: Ptr a -> IntPtr
151 foreign import ccall unsafe "__hscore_from_intptr"
152 intPtrToPtr :: IntPtr -> Ptr a
154 # endif /* !__GLASGOW_HASKELL__ */