1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.ForeignPtr
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- The 'ForeignPtr' type and operations. This module is part of the
13 -- Foreign Function Interface (FFI) and will usually be imported via
14 -- the "Foreign" module.
16 -----------------------------------------------------------------------------
18 module Foreign.ForeignPtr
20 -- * Finalised data pointers
21 ForeignPtr, -- abstract, instance of: Eq
22 , newForeignPtr -- :: Ptr a -> IO () -> IO (ForeignPtr a)
23 , addForeignPtrFinalizer -- :: ForeignPtr a -> IO () -> IO ()
24 , withForeignPtr -- :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
25 , foreignPtrToPtr -- :: ForeignPtr a -> Ptr a
26 , touchForeignPtr -- :: ForeignPtr a -> IO ()
27 , castForeignPtr -- :: ForeignPtr a -> ForeignPtr b
34 #ifdef __GLASGOW_HASKELL__
42 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
44 #ifdef __GLASGOW_HASKELL__
45 -- |The type 'ForeignPtr' represents references to objects that are
46 -- maintained in a foreign language, i.e., that are not part of the
47 -- data structures usually managed by the Haskell storage manager.
48 -- The essential difference between 'ForeignPtr's and vanilla memory
49 -- references of type @Ptr a@ is that the former may be associated
50 -- with /finalisers/. A finaliser is a routine that is invoked when
51 -- the Haskell storage manager detects that - within the Haskell heap
52 -- and stack - there are no more references left that are pointing to
53 -- the 'ForeignPtr'. Typically, the finaliser will, then, invoke
54 -- routines in the foreign language that free the resources bound by
55 -- the foreign object.
57 -- The 'ForeignPtr' is parameterised in the same way as 'Ptr'. The
58 -- type argument of 'ForeignPtr' should normally be an instance of
61 data ForeignPtr a = ForeignPtr ForeignObj#
63 instance CCallable (ForeignPtr a)
65 eqForeignPtr :: ForeignPtr a -> ForeignPtr a -> Bool
66 eqForeignPtr (ForeignPtr fo1#) (ForeignPtr fo2#) = eqForeignObj# fo1# fo2#
68 instance Eq (ForeignPtr a) where
69 p == q = eqForeignPtr p q
70 p /= q = not (eqForeignPtr p q)
72 newForeignPtr :: Ptr a -> IO () -> IO (ForeignPtr a)
73 -- ^Turns a plain memory reference into a foreign object
74 -- by associating a finaliser - given by the monadic operation
75 -- - with the reference. The finaliser will be executed after
76 -- the last reference to the foreign object is dropped. Note
77 -- that there is no guarantee on how soon the finaliser is
78 -- executed after the last reference was dropped; this depends
79 -- on the details of the Haskell storage manager. The only
80 -- guarantee is that the finaliser runs before the program
82 newForeignPtr p finalizer
83 = do fObj <- mkForeignPtr p
84 addForeignPtrFinalizer fObj finalizer
87 addForeignPtrFinalizer :: ForeignPtr a -> IO () -> IO ()
88 -- ^This function adds another finaliser to the given
89 -- foreign object. No guarantees are made on the order in
90 -- which multiple finalisers for a single object are run.
91 addForeignPtrFinalizer (ForeignPtr fo) finalizer =
92 IO $ \s -> case mkWeak# fo () finalizer s of { (# s1, w #) -> (# s1, () #) }
94 mkForeignPtr :: Ptr a -> IO (ForeignPtr a) {- not exported -}
95 mkForeignPtr (Ptr obj) = IO ( \ s# ->
96 case mkForeignObj# obj s# of
97 (# s1#, fo# #) -> (# s1#, ForeignPtr fo# #) )
99 touchForeignPtr :: ForeignPtr a -> IO ()
100 -- ^This function ensures that the foreign object in
101 -- question is alive at the given place in the sequence of IO
102 -- actions. In particular 'withForeignPtr'
103 -- does a 'touchForeignPtr' after it
104 -- executes the user action.
106 -- This function can be used to express liveness
107 -- dependencies between 'ForeignPtr's: for
108 -- example, if the finalizer for one
109 -- 'ForeignPtr' touches a second
110 -- 'ForeignPtr', then it is ensured that the
111 -- second 'ForeignPtr' will stay alive at
112 -- least as long as the first. This can be useful when you
113 -- want to manipulate /interior pointers/ to
114 -- a foreign structure: you can use
115 -- 'touchForeignObj' to express the
116 -- requirement that the exterior pointer must not be finalized
117 -- until the interior pointer is no longer referenced.
118 touchForeignPtr (ForeignPtr fo)
119 = IO $ \s -> case touch# fo s of s -> (# s, () #)
121 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
122 -- ^This is a way to look at the pointer living inside a
123 -- foreign object. This function takes a function which is
124 -- applied to that pointer. The resulting 'IO' action is then
125 -- executed. The foreign object is kept alive at least during
126 -- the whole action, even if it is not used directly
127 -- inside. Note that it is not safe to return the pointer from
128 -- the action and use it after the action completes. All uses
129 -- of the pointer should be inside the
130 -- 'withForeignPtr' bracket. The reason for
131 -- this unsafety is the same as for
132 -- 'foreignPtrToPtr' below: the finalizer
133 -- may run earlier than expected, because the compiler can only
134 -- track usage of the 'ForeignPtr' object, not
135 -- a 'Ptr' object made from it.
137 -- This function is normally used for marshalling data to
138 -- or from the object pointed to by the
139 -- 'ForeignPtr', using the operations from the
142 = do r <- io (foreignPtrToPtr fo)
146 foreignPtrToPtr :: ForeignPtr a -> Ptr a
147 -- ^This function extracts the pointer component of a foreign
148 -- pointer. This is a potentially dangerous operations, as if the
149 -- argument to 'foreignPtrToPtr' is the last usage
150 -- occurence of the given foreign pointer, then its finaliser(s) will
151 -- be run, which potentially invalidates the plain pointer just
152 -- obtained. Hence, 'touchForeignPtr' must be used
153 -- wherever it has to be guaranteed that the pointer lives on - i.e.,
154 -- has another usage occurrence.
156 -- To avoid subtle coding errors, hand written marshalling code
157 -- should preferably use 'withForeignPtr' rather
158 -- than combinations of 'foreignPtrToPtr' and
159 -- 'touchForeignPtr'. However, the later routines
160 -- are occasionally preferred in tool generated marshalling code.
161 foreignPtrToPtr (ForeignPtr fo) = Ptr (foreignObjToAddr# fo)
163 castForeignPtr :: ForeignPtr a -> ForeignPtr b
164 -- ^This function casts a 'ForeignPtr'
165 -- parameterised by one type into another type.
166 castForeignPtr (ForeignPtr a) = ForeignPtr a