4632714052a67f2d0333bd2dea49464c537ec18c
[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,
33 nullPtr,
34 castPtr,
35 plusPtr,
36 alignPtr,
37 minusPtr,
38
39 -- * Function pointers
40
41 FunPtr,
42 nullFunPtr,
43 castFunPtr,
44 castFunPtrToPtr,
45 castPtrToFunPtr,
46
47 freeHaskellFunPtr,
48 -- Free the function pointer created by foreign export dynamic.
49
50 -- * Integral types with lossless conversion to and from pointers
51 IntPtr,
52 ptrToIntPtr,
53 intPtrToPtr,
54 WordPtr,
55 ptrToWordPtr,
56 wordPtrToPtr
57 ) where
58
59 #ifdef __GLASGOW_HASKELL__
60 import GHC.Ptr
61 import GHC.Base
62 import GHC.Num
63 import GHC.Read
64 import GHC.Real
65 import GHC.Show
66 import GHC.Enum
67 #else
68 import Control.Monad ( liftM )
69 import Foreign.C.Types
70 #endif
71
72 import Data.Bits
73 import Data.Typeable
74 import Foreign.Storable ( Storable(..) )
75
76 #ifdef __HUGS__
77 import Hugs.Ptr
78 #endif
79
80 #ifdef __GLASGOW_HASKELL__
81 -- | Release the storage associated with the given 'FunPtr', which
82 -- must have been obtained from a wrapper stub. This should be called
83 -- whenever the return value from a foreign import wrapper function is
84 -- no longer required; otherwise, the storage it uses will leak.
85 foreign import ccall unsafe "freeHaskellFunctionPtr"
86 freeHaskellFunPtr :: FunPtr a -> IO ()
87 #endif
88
89 #include "HsBaseConfig.h"
90 #include "CTypes.h"
91
92 #ifdef __GLASGOW_HASKELL__
93 -- | An unsigned integral type that can be losslessly converted to and from
94 -- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
95 -- can be marshalled to and from that type safely.
96 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
97 -- Word and Int are guaranteed pointer-sized in GHC
98
99 -- | A signed integral type that can be losslessly converted to and from
100 -- @Ptr@. This type is also compatible with the C99 type @intptr_t@, and
101 -- can be marshalled to and from that type safely.
102 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
103 -- Word and Int are guaranteed pointer-sized in GHC
104
105 -- | casts a @Ptr@ to a @WordPtr@
106 ptrToWordPtr :: Ptr a -> WordPtr
107 ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))
108
109 -- | casts a @WordPtr@ to a @Ptr@
110 wordPtrToPtr :: WordPtr -> Ptr a
111 wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))
112
113 -- | casts a @Ptr@ to an @IntPtr@
114 ptrToIntPtr :: Ptr a -> IntPtr
115 ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))
116
117 -- | casts an @IntPtr@ to a @Ptr@
118 intPtrToPtr :: IntPtr -> Ptr a
119 intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
120
121 #else /* !__GLASGOW_HASKELL__ */
122
123 INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
124 INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)
125
126 {-# CFILES cbits/PrelIOUtils.c #-}
127
128 foreign import ccall unsafe "__hscore_to_uintptr"
129 ptrToWordPtr :: Ptr a -> WordPtr
130
131 foreign import ccall unsafe "__hscore_from_uintptr"
132 wordPtrToPtr :: WordPtr -> Ptr a
133
134 foreign import ccall unsafe "__hscore_to_intptr"
135 ptrToIntPtr :: Ptr a -> IntPtr
136
137 foreign import ccall unsafe "__hscore_from_intptr"
138 intPtrToPtr :: IntPtr -> Ptr a
139
140 #endif /* !__GLASGOW_HASKELL__ */
141