SafeHaskell: Added SafeHaskell to base
[packages/base.git] / Foreign / Ptr.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP
3 , NoImplicitPrelude
4 , ForeignFunctionInterface
5 , MagicHash
6 , GeneralizedNewtypeDeriving
7 #-}
8 #ifdef __GLASGOW_HASKELL__
9 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
10 #endif
11
12 -----------------------------------------------------------------------------
13 -- |
14 -- Module : Foreign.Ptr
15 -- Copyright : (c) The FFI task force 2001
16 -- License : BSD-style (see the file libraries/base/LICENSE)
17 --
18 -- Maintainer : ffi@haskell.org
19 -- Stability : provisional
20 -- Portability : portable
21 --
22 -- This module provides typed pointers to foreign data. It is part
23 -- of the Foreign Function Interface (FFI) and will normally be
24 -- imported via the "Foreign" module.
25 --
26 -----------------------------------------------------------------------------
27
28 module Foreign.Ptr (
29
30 -- * Data pointers
31
32 Ptr, -- data Ptr a
33 nullPtr, -- :: Ptr a
34 castPtr, -- :: Ptr a -> Ptr b
35 plusPtr, -- :: Ptr a -> Int -> Ptr b
36 alignPtr, -- :: Ptr a -> Int -> Ptr a
37 minusPtr, -- :: Ptr a -> Ptr b -> Int
38
39 -- * Function pointers
40
41 FunPtr, -- data FunPtr a
42 nullFunPtr, -- :: FunPtr a
43 castFunPtr, -- :: FunPtr a -> FunPtr b
44 castFunPtrToPtr, -- :: FunPtr a -> Ptr b
45 castPtrToFunPtr, -- :: Ptr a -> FunPtr b
46
47 freeHaskellFunPtr, -- :: FunPtr a -> IO ()
48 -- Free the function pointer created by foreign export dynamic.
49
50 #ifndef __NHC__
51 -- * Integral types with lossless conversion to and from pointers
52 IntPtr,
53 ptrToIntPtr,
54 intPtrToPtr,
55 WordPtr,
56 ptrToWordPtr,
57 wordPtrToPtr
58 #endif
59 ) where
60
61 #ifdef __GLASGOW_HASKELL__
62 import GHC.Ptr
63 import GHC.Base
64 import GHC.Num
65 import GHC.Read
66 import GHC.Real
67 import GHC.Show
68 import GHC.Enum
69 import GHC.Word ( Word(..) )
70
71 import Data.Word
72 #else
73 import Control.Monad ( liftM )
74 import Foreign.C.Types
75 #endif
76
77 import Data.Bits
78 import Data.Typeable
79 import Foreign.Storable ( Storable(..) )
80
81 #ifdef __NHC__
82 import NHC.FFI
83 ( Ptr
84 , nullPtr
85 , castPtr
86 , plusPtr
87 , alignPtr
88 , minusPtr
89 , FunPtr
90 , nullFunPtr
91 , castFunPtr
92 , castFunPtrToPtr
93 , castPtrToFunPtr
94 , freeHaskellFunPtr
95 )
96 #endif
97
98 #ifdef __HUGS__
99 import Hugs.Ptr
100 #endif
101
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 ()
109 #endif
110
111 #ifndef __NHC__
112 # include "HsBaseConfig.h"
113 # include "CTypes.h"
114
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
121
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
127
128 -- | casts a @Ptr@ to a @WordPtr@
129 ptrToWordPtr :: Ptr a -> WordPtr
130 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
131
132 -- | casts a @WordPtr@ to a @Ptr@
133 wordPtrToPtr :: WordPtr -> Ptr a
134 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
135
136 -- | casts a @Ptr@ to an @IntPtr@
137 ptrToIntPtr :: Ptr a -> IntPtr
138 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
139
140 -- | casts an @IntPtr@ to a @Ptr@
141 intPtrToPtr :: IntPtr -> Ptr a
142 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
143
144 # else /* !__GLASGOW_HASKELL__ */
145
146 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
147 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
148
149 {-# CFILES cbits/PrelIOUtils.c #-}
150
151 foreign import ccall unsafe "__hscore_to_uintptr"
152 ptrToWordPtr :: Ptr a -> WordPtr
153
154 foreign import ccall unsafe "__hscore_from_uintptr"
155 wordPtrToPtr :: WordPtr -> Ptr a
156
157 foreign import ccall unsafe "__hscore_to_intptr"
158 ptrToIntPtr :: Ptr a -> IntPtr
159
160 foreign import ccall unsafe "__hscore_from_intptr"
161 intPtrToPtr :: IntPtr -> Ptr a
162
163 # endif /* !__GLASGOW_HASKELL__ */
164 #endif /* !__NHC_ */