1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.ForeignPtr
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
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.
16 -----------------------------------------------------------------------------
18 module Foreign.ForeignPtr
20 -- * Finalised data pointers
26 -- ** Basic operations
29 , addForeignPtrFinalizer
32 , addForeignPtrFinalizerEnv
36 #ifdef __GLASGOW_HASKELL__
40 -- ** Low-level operations
41 , unsafeForeignPtrToPtr
45 -- ** Allocating managed memory
47 , mallocForeignPtrBytes
48 , mallocForeignPtrArray
49 , mallocForeignPtrArray0
61 , addForeignPtrFinalizer
63 , unsafeForeignPtrToPtr
67 , malloc, mallocBytes, finalizerFree
72 import Hugs.ForeignPtr
76 import Foreign.Storable ( Storable(sizeOf) )
79 #ifdef __GLASGOW_HASKELL__
83 import GHC.Err ( undefined )
87 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
88 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
92 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
94 instance Eq (ForeignPtr a) where
95 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
97 instance Ord (ForeignPtr a) where
98 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
100 instance Show (ForeignPtr a) where
101 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
106 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
107 -- ^Turns a plain memory reference into a foreign pointer, and
108 -- associates a finaliser with the reference. The finaliser will be executed
109 -- after the last reference to the foreign object is dropped. Note that there
110 -- is no guarantee on how soon the finaliser is executed after the last
111 -- reference was dropped; this depends on the details of the Haskell storage
112 -- manager. The only guarantee is that the finaliser runs before the program
114 newForeignPtr finalizer p
115 = do fObj <- newForeignPtr_ p
116 addForeignPtrFinalizer finalizer fObj
119 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
120 -- ^This is a way to look at the pointer living inside a
121 -- foreign object. This function takes a function which is
122 -- applied to that pointer. The resulting 'IO' action is then
123 -- executed. The foreign object is kept alive at least during
124 -- the whole action, even if it is not used directly
125 -- inside. Note that it is not safe to return the pointer from
126 -- the action and use it after the action completes. All uses
127 -- of the pointer should be inside the
128 -- 'withForeignPtr' bracket. The reason for
129 -- this unsafety is the same as for
130 -- 'unsafeForeignPtrToPtr' below: the finalizer
131 -- may run earlier than expected, because the compiler can only
132 -- track usage of the 'ForeignPtr' object, not
133 -- a 'Ptr' object made from it.
135 -- This function is normally used for marshalling data to
136 -- or from the object pointed to by the
137 -- 'ForeignPtr', using the operations from the
140 = do r <- io (unsafeForeignPtrToPtr fo)
143 #endif /* ! __NHC__ */
146 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
147 -- environment in addition to the finalized pointer. The environment
148 -- that will be passed to the finalizer is fixed by the second argument to
149 -- 'newForeignPtrEnv'.
151 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
152 newForeignPtrEnv finalizer env p
153 = do fObj <- newForeignPtr_ p
154 addForeignPtrFinalizerEnv finalizer env fObj
156 #endif /* __HUGS__ */
158 #ifndef __GLASGOW_HASKELL__
159 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
160 mallocForeignPtr = do
162 newForeignPtr finalizerFree r
164 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
165 mallocForeignPtrBytes n = do
167 newForeignPtr finalizerFree r
168 #endif /* !__GLASGOW_HASKELL__ */
170 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
171 -- but yields a memory area that has a finalizer attached that releases
172 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
173 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
174 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
175 mallocForeignPtrArray = doMalloc undefined
177 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
178 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
180 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
181 -- but yields a memory area that has a finalizer attached that releases
182 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
183 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
184 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
185 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)