1fbfeb6ca4289a47fe577953e1cd136f67909b50
[ghc-base.git] / GHC / Ptr.lhs
1 \begin{code}
2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.Ptr
6 -- Copyright   :  (c) The FFI Task Force, 2000-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  ffi@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC Extensions)
12 --
13 -- The 'Ptr' and 'FunPtr' types and operations.
14 --
15 -----------------------------------------------------------------------------
16
17 module GHC.Ptr where
18
19 import GHC.Base
20
21 ------------------------------------------------------------------------
22 -- Data pointers.
23
24 data Ptr a = Ptr Addr# deriving (Eq, Ord)
25 -- ^ A value of type @'Ptr' a@ represents a pointer to an object, or an
26 -- array of objects, which may be marshalled to or from Haskell values
27 -- of type @a@.
28 --
29 -- The type @a@ will often be an instance of class
30 -- 'Foreign.Storable.Storable' which provides the marshalling operations.
31 -- However this is not essential, and you can provide your own operations
32 -- to access the pointer.  For example you might write small foreign
33 -- functions to get or set the fields of a C @struct@.
34
35 -- |The constant 'nullPtr' contains a distinguished value of 'Ptr'
36 -- that is not associated with a valid memory location.
37 nullPtr :: Ptr a
38 nullPtr = Ptr nullAddr#
39
40 -- |The 'castPtr' function casts a pointer from one type to another.
41 castPtr :: Ptr a -> Ptr b
42 castPtr (Ptr addr) = Ptr addr
43
44 -- |Advances the given address by the given offset in bytes.
45 plusPtr :: Ptr a -> Int -> Ptr b
46 plusPtr (Ptr addr) (I# d) = Ptr (plusAddr# addr d)
47
48 -- |Given an arbitrary address and an alignment constraint,
49 -- 'alignPtr' yields the next higher address that fulfills the
50 -- alignment constraint.  An alignment constraint @x@ is fulfilled by
51 -- any address divisible by @x@.  This operation is idempotent.
52 alignPtr :: Ptr a -> Int -> Ptr a
53 alignPtr addr@(Ptr a) (I# i)
54   = case remAddr# a i of {
55       0# -> addr;
56       n -> Ptr (plusAddr# a (i -# n)) }
57
58 -- |Computes the offset required to get from the first to the second
59 -- argument.  We have 
60 --
61 -- > p2 == p1 `plusPtr` (p2 `minusPtr` p1)
62 minusPtr :: Ptr a -> Ptr b -> Int
63 minusPtr (Ptr a1) (Ptr a2) = I# (minusAddr# a1 a2)
64
65 ------------------------------------------------------------------------
66 -- Function pointers for the default calling convention.
67
68 data FunPtr a = FunPtr Addr# deriving (Eq, Ord)
69 -- ^ A value of type @'FunPtr' a@ is a pointer to a function callable
70 -- from foreign code.  The type @a@ will normally be a /foreign type/,
71 -- a function type with zero or more arguments where
72 --
73 -- * the argument types are /marshallable foreign types/,
74 --   i.e. 'Char', 'Int', 'Prelude.Double', 'Prelude.Float',
75 --   'Bool', 'Data.Int.Int8', 'Data.Int.Int16', 'Data.Int.Int32',
76 --   'Data.Int.Int64', 'Data.Word.Word8', 'Data.Word.Word16',
77 --   'Data.Word.Word32', 'Data.Word.Word64', @'Ptr' a@, @'FunPtr' a@,
78 --   @'Foreign.StablePtr.StablePtr' a@ or a renaming of any of these
79 --   using @newtype@.
80 -- 
81 -- * the return type is either a marshallable foreign type or has the form
82 --   @'Prelude.IO' t@ where @t@ is a marshallable foreign type or @()@.
83 --
84 -- A value of type @'FunPtr' a@ may be a pointer to a foreign function,
85 -- either returned by another foreign function or imported with a
86 -- a static address import like
87 --
88 -- > foreign import ccall "stdlib.h &free"
89 -- >   p_free :: FunPtr (Ptr a -> IO ())
90 --
91 -- or a pointer to a Haskell function created using a /wrapper/ stub
92 -- declared to produce a 'FunPtr' of the correct type.  For example:
93 --
94 -- > type Compare = Int -> Int -> Bool
95 -- > foreign import ccall "wrapper"
96 -- >   mkCompare :: Compare -> IO (FunPtr Compare)
97 --
98 -- Calls to wrapper stubs like @mkCompare@ allocate storage, which
99 -- should be released with 'Foreign.Ptr.freeHaskellFunPtr' when no
100 -- longer required.
101 --
102 -- To convert 'FunPtr' values to corresponding Haskell functions, one
103 -- can define a /dynamic/ stub for the specific foreign type, e.g.
104 --
105 -- > type IntFunction = CInt -> IO ()
106 -- > foreign import ccall "dynamic" 
107 -- >   mkFun :: FunPtr IntFunction -> IntFunction
108
109 -- |The constant 'nullFunPtr' contains a
110 -- distinguished value of 'FunPtr' that is not
111 -- associated with a valid memory location.
112 nullFunPtr :: FunPtr a
113 nullFunPtr = FunPtr nullAddr#
114
115 -- |Casts a 'FunPtr' to a 'FunPtr' of a different type.
116 castFunPtr :: FunPtr a -> FunPtr b
117 castFunPtr (FunPtr addr) = FunPtr addr
118
119 -- |Casts a 'FunPtr' to a 'Ptr'.
120 --
121 -- /Note:/ this is valid only on architectures where data and function
122 -- pointers range over the same set of addresses, and should only be used
123 -- for bindings to external libraries whose interface already relies on
124 -- this assumption.
125 castFunPtrToPtr :: FunPtr a -> Ptr b
126 castFunPtrToPtr (FunPtr addr) = Ptr addr
127
128 -- |Casts a 'Ptr' to a 'FunPtr'.
129 --
130 -- /Note:/ this is valid only on architectures where data and function
131 -- pointers range over the same set of addresses, and should only be used
132 -- for bindings to external libraries whose interface already relies on
133 -- this assumption.
134 castPtrToFunPtr :: Ptr a -> FunPtr b
135 castPtrToFunPtr (Ptr addr) = FunPtr addr
136 \end{code}
137