Ptr.hs 4.37 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
2
3
4
5
6
7
{-# LANGUAGE CPP
           , NoImplicitPrelude
           , ForeignFunctionInterface
           , MagicHash
           , GeneralizedNewtypeDeriving
  #-}
8
9
10
#ifdef __GLASGOW_HASKELL__
{-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
#endif
11

12
-----------------------------------------------------------------------------
13
-- |
14
15
-- Module      :  Foreign.Ptr
-- Copyright   :  (c) The FFI task force 2001
16
-- License     :  BSD-style (see the file libraries/base/LICENSE)
17
18
-- 
-- Maintainer  :  ffi@haskell.org
19
20
-- Stability   :  provisional
-- Portability :  portable
21
--
22
-- This module provides typed pointers to foreign data.  It is part
23
24
-- of the Foreign Function Interface (FFI) and will normally be
-- imported via the "Foreign" module.
25
26
27
28
--
-----------------------------------------------------------------------------

module Foreign.Ptr (
29
30

    -- * Data pointers
Don Stewart's avatar
Don Stewart committed
31

32
    Ptr,      -- data Ptr a
33
34
35
36
37
    nullPtr,      -- :: Ptr a
    castPtr,      -- :: Ptr a -> Ptr b
    plusPtr,      -- :: Ptr a -> Int -> Ptr b
    alignPtr,     -- :: Ptr a -> Int -> Ptr a
    minusPtr,     -- :: Ptr a -> Ptr b -> Int
Don Stewart's avatar
Don Stewart committed
38

39
    -- * Function pointers
Don Stewart's avatar
Don Stewart committed
40

41
    FunPtr,      -- data FunPtr a
42
43
44
45
    nullFunPtr,      -- :: FunPtr a
    castFunPtr,      -- :: FunPtr a -> FunPtr b
    castFunPtrToPtr, -- :: FunPtr a -> Ptr b
    castPtrToFunPtr, -- :: Ptr a -> FunPtr b
Don Stewart's avatar
Don Stewart committed
46

47
48
49
    freeHaskellFunPtr, -- :: FunPtr a -> IO ()
    -- Free the function pointer created by foreign export dynamic.

50
#ifndef __NHC__
Ross Paterson's avatar
Ross Paterson committed
51
    -- * Integral types with lossless conversion to and from pointers
52
53
54
55
56
57
    IntPtr,
    ptrToIntPtr,
    intPtrToPtr,
    WordPtr,
    ptrToWordPtr,
    wordPtrToPtr
58
#endif
59
60
61
62
 ) where

#ifdef __GLASGOW_HASKELL__
import GHC.Ptr
63
64
import GHC.Base
import GHC.Num
65
66
import GHC.Read
import GHC.Real
67
import GHC.Show
68
import GHC.Enum
Don Stewart's avatar
Don Stewart committed
69
import GHC.Word         ( Word(..) )
70
71

import Data.Word
72
#else
Don Stewart's avatar
Don Stewart committed
73
import Control.Monad    ( liftM )
74
import Foreign.C.Types
75
76
#endif

77
import Data.Bits
78
import Data.Typeable
79
80
import Foreign.Storable ( Storable(..) )

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifdef __NHC__
import NHC.FFI
  ( Ptr
  , nullPtr
  , castPtr
  , plusPtr
  , alignPtr
  , minusPtr
  , FunPtr
  , nullFunPtr
  , castFunPtr
  , castFunPtrToPtr
  , castPtrToFunPtr
  , freeHaskellFunPtr
  )
#endif

ross's avatar
ross committed
98
99
100
#ifdef __HUGS__
import Hugs.Ptr
#endif
101
102

#ifdef __GLASGOW_HASKELL__
ross's avatar
ross committed
103
104
105
106
-- | Release the storage associated with the given 'FunPtr', which
-- must have been obtained from a wrapper stub.  This should be called
-- whenever the return value from a foreign import wrapper function is
-- 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
112
113
#ifndef __NHC__
# include "HsBaseConfig.h"
# include "CTypes.h"
114

115
116
# ifdef __GLASGOW_HASKELL__
-- | An unsigned integral type that can be losslessly converted to and from
117
118
-- @Ptr@. This type is also compatible with the C99 type @uintptr_t@, and
-- can be marshalled to and from that type safely.
119
INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",Word)
Don Stewart's avatar
Don Stewart committed
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
124
-- @Ptr@.  This type is also compatible with the C99 type @intptr_t@, and
-- can be marshalled to and from that type safely.
125
INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",Int)
Don Stewart's avatar
Don Stewart committed
126
        -- Word and Int are guaranteed pointer-sized in GHC
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

-- | casts a @Ptr@ to a @WordPtr@
ptrToWordPtr :: Ptr a -> WordPtr
ptrToWordPtr (Ptr a#) = WordPtr (W# (int2Word# (addr2Int# a#)))

-- | casts a @WordPtr@ to a @Ptr@
wordPtrToPtr :: WordPtr -> Ptr a
wordPtrToPtr (WordPtr (W# w#)) = Ptr (int2Addr# (word2Int# w#))

-- | casts a @Ptr@ to an @IntPtr@
ptrToIntPtr :: Ptr a -> IntPtr
ptrToIntPtr (Ptr a#) = IntPtr (I# (addr2Int# a#))

-- | casts an @IntPtr@ to a @Ptr@
intPtrToPtr :: IntPtr -> Ptr a
intPtrToPtr (IntPtr (I# i#)) = Ptr (int2Addr# i#)
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

# else /* !__GLASGOW_HASKELL__ */

INTEGRAL_TYPE(WordPtr,tyConWordPtr,"WordPtr",CUIntPtr)
INTEGRAL_TYPE(IntPtr,tyConIntPtr,"IntPtr",CIntPtr)

{-# CFILES cbits/PrelIOUtils.c #-}

foreign import ccall unsafe "__hscore_to_uintptr"
    ptrToWordPtr :: Ptr a -> WordPtr

foreign import ccall unsafe "__hscore_from_uintptr"
    wordPtrToPtr :: WordPtr -> Ptr a

foreign import ccall unsafe "__hscore_to_intptr"
    ptrToIntPtr :: Ptr a -> IntPtr

foreign import ccall unsafe "__hscore_from_intptr"
    intPtrToPtr :: IntPtr -> Ptr a

# endif /* !__GLASGOW_HASKELL__ */
#endif /* !__NHC_ */
dterei's avatar
dterei committed
165