[project @ 2003-11-05 15:05:12 by ross]
[packages/old-time.git] / Foreign / ForeignPtr.hs
1 {-# OPTIONS -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 #ifdef __HUGS__
24 , FinalizerEnvPtr
25 #endif
26 -- ** Basic operations
27 , newForeignPtr
28 , newForeignPtr_
29 , addForeignPtrFinalizer
30 #ifdef __HUGS__
31 , newForeignPtrEnv
32 , addForeignPtrFinalizerEnv
33 #endif
34 , withForeignPtr
35
36 -- ** Low-level operations
37 , unsafeForeignPtrToPtr
38 , touchForeignPtr
39 , castForeignPtr
40
41 -- ** Allocating managed memory
42 , mallocForeignPtr
43 , mallocForeignPtrBytes
44 , mallocForeignPtrArray
45 , mallocForeignPtrArray0
46 )
47 where
48
49 import Foreign.Ptr
50
51 #ifdef __NHC__
52 import NHC.FFI
53 ( ForeignPtr
54 , FinalizerPtr
55 , newForeignPtr
56 , newForeignPtr_
57 , addForeignPtrFinalizer
58 , withForeignPtr
59 , unsafeForeignPtrToPtr
60 , touchForeignPtr
61 , castForeignPtr
62 , Storable(sizeOf)
63 , malloc, mallocBytes, finalizerFree
64 )
65 #endif
66
67 #ifdef __HUGS__
68 import Hugs.ForeignPtr
69 #endif
70
71 #ifndef __NHC__
72 import Foreign.Storable ( Storable(sizeOf) )
73 #endif
74
75 #ifdef __GLASGOW_HASKELL__
76 import GHC.Base
77 import GHC.IOBase
78 import GHC.Num
79 import GHC.Err ( undefined )
80 import GHC.ForeignPtr
81 #endif
82
83 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
84 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
85 import Data.Typeable
86
87 #include "Typeable.h"
88 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
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. The only guarantee is that the finaliser runs before the program
109 -- terminates.
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 unsafety 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 #ifdef __HUGS__
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 a => a -> Int -> IO (ForeignPtr a)
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)