Remove Control.Parallel*, now in package parallel
[packages/random.git] / Foreign / ForeignPtr.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Foreign.ForeignPtr
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
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.
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.ForeignPtr
19 (
20 -- * Finalised data pointers
21 ForeignPtr
22 , FinalizerPtr
23 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
24 , FinalizerEnvPtr
25 #endif
26 -- ** Basic operations
27 , newForeignPtr
28 , newForeignPtr_
29 , addForeignPtrFinalizer
30 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
31 , newForeignPtrEnv
32 , addForeignPtrFinalizerEnv
33 #endif
34 , withForeignPtr
35
36 #ifdef __GLASGOW_HASKELL__
37 , finalizeForeignPtr
38 #endif
39
40 -- ** Low-level operations
41 , unsafeForeignPtrToPtr
42 , touchForeignPtr
43 , castForeignPtr
44
45 -- ** Allocating managed memory
46 , mallocForeignPtr
47 , mallocForeignPtrBytes
48 , mallocForeignPtrArray
49 , mallocForeignPtrArray0
50 )
51 where
52
53 import Foreign.Ptr
54
55 #ifdef __NHC__
56 import NHC.FFI
57 ( ForeignPtr
58 , FinalizerPtr
59 , newForeignPtr
60 , newForeignPtr_
61 , addForeignPtrFinalizer
62 , withForeignPtr
63 , unsafeForeignPtrToPtr
64 , touchForeignPtr
65 , castForeignPtr
66 , Storable(sizeOf)
67 , malloc, mallocBytes, finalizerFree
68 )
69 #endif
70
71 #ifdef __HUGS__
72 import Hugs.ForeignPtr
73 #endif
74
75 #ifndef __NHC__
76 import Foreign.Storable ( Storable(sizeOf) )
77 #endif
78
79 #ifdef __GLASGOW_HASKELL__
80 import GHC.Base
81 import GHC.IOBase
82 import GHC.Num
83 import GHC.Err ( undefined )
84 import GHC.ForeignPtr
85 #endif
86
87 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
88 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
89
90 instance Eq (ForeignPtr a) where
91 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
92
93 instance Ord (ForeignPtr a) where
94 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
95
96 instance Show (ForeignPtr a) where
97 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
98 #endif
99
100
101 #ifndef __NHC__
102 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
103 -- ^Turns a plain memory reference into a foreign pointer, and
104 -- associates a finaliser with the reference. The finaliser will be executed
105 -- after the last reference to the foreign object is dropped. Note that there
106 -- is no guarantee on how soon the finaliser is executed after the last
107 -- reference was dropped; this depends on the details of the Haskell storage
108 -- manager. Indeed, there is no guarantee that the finalizer is executed at
109 -- all; a program may exit with finalizers outstanding. (This is true
110 -- of GHC, other implementations may give stronger guarantees).
111 newForeignPtr finalizer p
112 = do fObj <- newForeignPtr_ p
113 addForeignPtrFinalizer finalizer fObj
114 return fObj
115
116 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
117 -- ^This is a way to look at the pointer living inside a
118 -- foreign object. This function takes a function which is
119 -- applied to that pointer. The resulting 'IO' action is then
120 -- executed. The foreign object is kept alive at least during
121 -- the whole action, even if it is not used directly
122 -- inside. Note that it is not safe to return the pointer from
123 -- the action and use it after the action completes. All uses
124 -- of the pointer should be inside the
125 -- 'withForeignPtr' bracket. The reason for
126 -- this unsafeness is the same as for
127 -- 'unsafeForeignPtrToPtr' below: the finalizer
128 -- may run earlier than expected, because the compiler can only
129 -- track usage of the 'ForeignPtr' object, not
130 -- a 'Ptr' object made from it.
131 --
132 -- This function is normally used for marshalling data to
133 -- or from the object pointed to by the
134 -- 'ForeignPtr', using the operations from the
135 -- 'Storable' class.
136 withForeignPtr fo io
137 = do r <- io (unsafeForeignPtrToPtr fo)
138 touchForeignPtr fo
139 return r
140 #endif /* ! __NHC__ */
141
142 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
143 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
144 -- environment in addition to the finalized pointer. The environment
145 -- that will be passed to the finalizer is fixed by the second argument to
146 -- 'newForeignPtrEnv'.
147 newForeignPtrEnv ::
148 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
149 newForeignPtrEnv finalizer env p
150 = do fObj <- newForeignPtr_ p
151 addForeignPtrFinalizerEnv finalizer env fObj
152 return fObj
153 #endif /* __HUGS__ */
154
155 #ifdef __GLASGOW_HASKELL__
156 type FinalizerEnvPtr env a = FunPtr (Ptr env -> Ptr a -> IO ())
157
158 -- | like 'addForeignPtrFinalizerEnv' but allows the finalizer to be
159 -- passed an additional environment parameter to be passed to the
160 -- finalizer. The environment passed to the finalizer is fixed by the
161 -- second argument to 'addForeignPtrFinalizerEnv'
162 addForeignPtrFinalizerEnv ::
163 FinalizerEnvPtr env a -> Ptr env -> ForeignPtr a -> IO ()
164 addForeignPtrFinalizerEnv finalizer env fptr =
165 addForeignPtrConcFinalizer fptr
166 (mkFinalizerEnv finalizer env (unsafeForeignPtrToPtr fptr))
167
168 foreign import ccall "dynamic"
169 mkFinalizerEnv :: FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO ()
170 #endif
171
172
173 #ifndef __GLASGOW_HASKELL__
174 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
175 mallocForeignPtr = do
176 r <- malloc
177 newForeignPtr finalizerFree r
178
179 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
180 mallocForeignPtrBytes n = do
181 r <- mallocBytes n
182 newForeignPtr finalizerFree r
183 #endif /* !__GLASGOW_HASKELL__ */
184
185 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
186 -- but yields a memory area that has a finalizer attached that releases
187 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
188 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
189 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
190 mallocForeignPtrArray = doMalloc undefined
191 where
192 doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
193 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
194
195 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
196 -- but yields a memory area that has a finalizer attached that releases
197 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
198 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
199 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
200 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)