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