8f8f64b729c9364821aa35136cc40255486c254d
[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 , newForeignPtr
27 , newForeignPtr_
28 , addForeignPtrFinalizer
29 #ifdef __HUGS__
30 , newForeignPtrEnv
31 , addForeignPtrFinalizerEnv
32 #endif
33 , withForeignPtr
34 , unsafeForeignPtrToPtr
35 , touchForeignPtr
36 , castForeignPtr
37
38 , mallocForeignPtr
39 , mallocForeignPtrBytes
40 , mallocForeignPtrArray
41 , mallocForeignPtrArray0
42 )
43 where
44
45 import Foreign.Ptr
46
47 #ifdef __NHC__
48 import NHC.FFI
49 ( ForeignPtr
50 , FinalizerPtr
51 , newForeignPtr
52 , newForeignPtr_
53 , addForeignPtrFinalizer
54 , withForeignPtr
55 , unsafeForeignPtrToPtr
56 , touchForeignPtr
57 , castForeignPtr
58 , Storable(sizeOf)
59 , malloc, mallocBytes, finalizerFree
60 )
61 #endif
62
63 #ifdef __HUGS__
64 import Hugs.ForeignPtr
65 #endif
66
67 #ifndef __NHC__
68 import Foreign.Storable ( Storable(sizeOf) )
69 #endif
70
71 #ifdef __GLASGOW_HASKELL__
72 import GHC.Base
73 import GHC.IOBase
74 import GHC.Num
75 import GHC.Err ( undefined )
76 import GHC.ForeignPtr
77 #endif
78
79 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
80 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
81 import Data.Typeable
82
83 #include "Typeable.h"
84 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
85
86 instance Eq (ForeignPtr a) where
87 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
88
89 instance Ord (ForeignPtr a) where
90 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
91
92 instance Show (ForeignPtr a) where
93 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
94 #endif
95
96
97 #ifndef __NHC__
98 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
99 -- ^Turns a plain memory reference into a foreign pointer, and
100 -- associates a finaliser with the reference. The finaliser will be executed
101 -- after the last reference to the foreign object is dropped. Note that there
102 -- is no guarantee on how soon the finaliser is executed after the last
103 -- reference was dropped; this depends on the details of the Haskell storage
104 -- manager. The only guarantee is that the finaliser runs before the program
105 -- terminates.
106 newForeignPtr finalizer p
107 = do fObj <- newForeignPtr_ p
108 addForeignPtrFinalizer finalizer fObj
109 return fObj
110
111 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
112 -- ^This is a way to look at the pointer living inside a
113 -- foreign object. This function takes a function which is
114 -- applied to that pointer. The resulting 'IO' action is then
115 -- executed. The foreign object is kept alive at least during
116 -- the whole action, even if it is not used directly
117 -- inside. Note that it is not safe to return the pointer from
118 -- the action and use it after the action completes. All uses
119 -- of the pointer should be inside the
120 -- 'withForeignPtr' bracket. The reason for
121 -- this unsafety is the same as for
122 -- 'unsafeForeignPtrToPtr' below: the finalizer
123 -- may run earlier than expected, because the compiler can only
124 -- track usage of the 'ForeignPtr' object, not
125 -- a 'Ptr' object made from it.
126 --
127 -- This function is normally used for marshalling data to
128 -- or from the object pointed to by the
129 -- 'ForeignPtr', using the operations from the
130 -- 'Storable' class.
131 withForeignPtr fo io
132 = do r <- io (unsafeForeignPtrToPtr fo)
133 touchForeignPtr fo
134 return r
135 #endif /* ! __NHC__ */
136
137 #ifdef __HUGS__
138 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
139 -- environment in addition to the finalized pointer. The environment
140 -- that will be passed to the finalizer is fixed by the second argument to
141 -- 'newForeignPtrEnv'.
142 newForeignPtrEnv ::
143 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
144 newForeignPtrEnv finalizer env p
145 = do fObj <- newForeignPtr_ p
146 addForeignPtrFinalizerEnv finalizer env fObj
147 return fObj
148 #endif /* __HUGS__ */
149
150 #ifndef __GLASGOW_HASKELL__
151 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
152 mallocForeignPtr = do
153 r <- malloc
154 newForeignPtr finalizerFree r
155
156 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
157 mallocForeignPtrBytes n = do
158 r <- mallocBytes n
159 newForeignPtr finalizerFree r
160 #endif /* __HUGS__ || __NHC__ */
161
162 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
163 mallocForeignPtrArray = doMalloc undefined
164 where
165 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
166 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
167
168 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
169 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)