8dd220f7699e1b3e53c4fd2c5c75d8e26a271bc7
[ghc-base.git] / Foreign / Ptr.hs
1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Foreign.Ptr
5 -- Copyright   :  (c) The FFI task force 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  ffi@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
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.
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.Ptr (
19
20     -- * Data pointers
21
22     Ptr,      -- data Ptr a
23     nullPtr,      -- :: Ptr a
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
28
29     -- * Function pointers
30
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
36
37     freeHaskellFunPtr, -- :: FunPtr a -> IO ()
38     -- Free the function pointer created by foreign export dynamic.
39
40 #ifndef __NHC__
41     -- * Integral types with lossless conversion to and from pointers
42     IntPtr,
43     ptrToIntPtr,
44     intPtrToPtr,
45     WordPtr,
46     ptrToWordPtr,
47     wordPtrToPtr
48 #endif
49  ) where
50
51 #ifdef __GLASGOW_HASKELL__
52 import GHC.Ptr
53 import GHC.Base
54 import GHC.Num
55 import GHC.Read
56 import GHC.Real
57 import GHC.Show
58 import GHC.Enum
59 import GHC.Word         ( Word(..) )
60
61 -- import Data.Int
62 import Data.Word
63 #else
64 import Control.Monad    ( liftM )
65 import Foreign.C.Types
66 #endif
67
68 import Data.Bits
69 import Data.Typeable
70 import Foreign.Storable ( Storable(..) )
71
72 #ifdef __NHC__
73 import NHC.FFI
74   ( Ptr
75   , nullPtr
76   , castPtr
77   , plusPtr
78   , alignPtr
79   , minusPtr
80   , FunPtr
81   , nullFunPtr
82   , castFunPtr
83   , castFunPtrToPtr
84   , castPtrToFunPtr
85   , freeHaskellFunPtr
86   )
87 #endif
88
89 #ifdef __HUGS__
90 import Hugs.Ptr
91 #endif
92
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 ()
100 #endif
101
102 #ifndef __NHC__
103 # include "HsBaseConfig.h"
104 # include "CTypes.h"
105
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
112
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
118
119 -- | casts a @Ptr@ to a @WordPtr@
120 ptrToWordPtr :: Ptr a -> WordPtr
121 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
122
123 -- | casts a @WordPtr@ to a @Ptr@
124 wordPtrToPtr :: WordPtr -> Ptr a
125 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
126
127 -- | casts a @Ptr@ to an @IntPtr@
128 ptrToIntPtr :: Ptr a -> IntPtr
129 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
130
131 -- | casts an @IntPtr@ to a @Ptr@
132 intPtrToPtr :: IntPtr -> Ptr a
133 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
134
135 # else /* !__GLASGOW_HASKELL__ */
136
137 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
138 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
139
140 {-# CFILES cbits/PrelIOUtils.c #-}
141
142 foreign import ccall unsafe "__hscore_to_uintptr"
143     ptrToWordPtr :: Ptr a -> WordPtr
144
145 foreign import ccall unsafe "__hscore_from_uintptr"
146     wordPtrToPtr :: WordPtr -> Ptr a
147
148 foreign import ccall unsafe "__hscore_to_intptr"
149     ptrToIntPtr :: Ptr a -> IntPtr
150
151 foreign import ccall unsafe "__hscore_from_intptr"
152     intPtrToPtr :: IntPtr -> Ptr a
153
154 # endif /* !__GLASGOW_HASKELL__ */
155 #endif /* !__NHC_ */