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