2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The FFI Task Force, 2000-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : ffi@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The 'Ptr' and 'FunPtr' types and operations.
15 -----------------------------------------------------------------------------
21 ------------------------------------------------------------------------
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
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@.
35 -- |The constant 'nullPtr' contains a distinguished value of 'Ptr'
36 -- that is not associated with a valid memory location.
38 nullPtr = Ptr nullAddr#
40 -- |The 'castPtr' function casts a pointer from one type to another.
41 castPtr :: Ptr a -> Ptr b
42 castPtr (Ptr addr) = Ptr addr
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)
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 {
56 n -> Ptr (plusAddr# a (i -# n)) }
58 -- |Computes the offset required to get from the first to the second
61 -- > p2 == p1 `plusPtr` (p2 `minusPtr` p1)
62 minusPtr :: Ptr a -> Ptr b -> Int
63 minusPtr (Ptr a1) (Ptr a2) = I# (minusAddr# a1 a2)
65 ------------------------------------------------------------------------
66 -- Function pointers for the default calling convention.
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
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
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 @()@.
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
88 -- > foreign import ccall "stdlib.h &free"
89 -- > p_free :: FunPtr (Ptr a -> IO ())
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:
94 -- > type Compare = Int -> Int -> Bool
95 -- > foreign import ccall "wrapper"
96 -- > mkCompare :: Compare -> IO (FunPtr Compare)
98 -- Calls to wrapper stubs like @mkCompare@ allocate storage, which
99 -- should be released with 'Foreign.Ptr.freeHaskellFunPtr' when no
102 -- To convert 'FunPtr' values to corresponding Haskell functions, one
103 -- can define a /dynamic/ stub for the specific foreign type, e.g.
105 -- > type IntFunction = CInt -> IO ()
106 -- > foreign import ccall "dynamic"
107 -- > mkFun :: FunPtr IntFunction -> IntFunction
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#
115 -- |Casts a 'FunPtr' to a 'FunPtr' of a different type.
116 castFunPtr :: FunPtr a -> FunPtr b
117 castFunPtr (FunPtr addr) = FunPtr addr
119 -- |Casts a 'FunPtr' to a 'Ptr'.
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
125 castFunPtrToPtr :: FunPtr a -> Ptr b
126 castFunPtrToPtr (FunPtr addr) = Ptr addr
128 -- |Casts a 'Ptr' to a 'FunPtr'.
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
134 castPtrToFunPtr :: Ptr a -> FunPtr b
135 castPtrToFunPtr (Ptr addr) = FunPtr addr