add WordPtr and IntPtr types to Foreign.Ptr, with associated conversions
[ghc-base.git] / Foreign / Ptr.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
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     -- * Integral types with lossless conversion to/from pointers
41     IntPtr,
42     ptrToIntPtr,
43     intPtrToPtr,
44     WordPtr,
45     ptrToWordPtr,
46     wordPtrToPtr
47  ) where
48
49 #ifdef __GLASGOW_HASKELL__
50 import GHC.Ptr
51 import GHC.IOBase
52 import GHC.Base
53 import GHC.Num
54 import GHC.List
55 import GHC.Read
56 import GHC.Real
57 import GHC.Show
58 import GHC.Enum
59 import GHC.Word         ( Word(..) )
60 import Data.Bits
61 import Data.Typeable    ( Typeable(..), mkTyCon, mkTyConApp )
62 import Numeric
63 import Foreign.C.Types
64
65 import Foreign.Storable
66 import Data.Int
67 import Data.Word
68 #endif
69
70 #ifdef __NHC__
71 import NHC.FFI
72   ( Ptr
73   , nullPtr
74   , castPtr
75   , plusPtr
76   , alignPtr
77   , minusPtr
78   , FunPtr
79   , nullFunPtr
80   , castFunPtr
81   , castFunPtrToPtr
82   , castPtrToFunPtr
83   , freeHaskellFunPtr
84   )
85 #endif
86
87 #ifdef __HUGS__
88 import Hugs.Ptr
89 #endif
90
91 #ifdef __GLASGOW_HASKELL__
92 -- | Release the storage associated with the given 'FunPtr', which
93 -- must have been obtained from a wrapper stub.  This should be called
94 -- whenever the return value from a foreign import wrapper function is
95 -- no longer required; otherwise, the storage it uses will leak.
96 foreign import ccall unsafe "freeHaskellFunctionPtr"
97     freeHaskellFunPtr :: FunPtr a -> IO ()
98
99 #include "HsBaseConfig.h"
100 #include "CTypes.h"
101
102 -- | An unsigend integral type that can be losslessly converted to and from
103 -- @Ptr@.
104 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
105         -- Word and Int are guaranteed pointer-sized in GHC
106
107 -- | A sigend integral type that can be losslessly converted to and from
108 -- @Ptr@.
109 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
110         -- Word and Int are guaranteed pointer-sized in GHC
111
112 -- | casts a @Ptr@ to a @WordPtr@
113 ptrToWordPtr :: Ptr a -> WordPtr
114 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
115
116 -- | casts a @WordPtr@ to a @Ptr@
117 wordPtrToPtr :: WordPtr -> Ptr a
118 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
119
120 -- | casts a @Ptr@ to an @IntPtr@
121 ptrToIntPtr :: Ptr a -> IntPtr
122 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
123
124 -- | casts an @IntPtr@ to a @Ptr@
125 intPtrToPtr :: IntPtr -> Ptr a
126 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
127 #endif