1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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
23 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
26 -- ** Basic operations
29 , addForeignPtrFinalizer
30 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
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 )
90 instance Eq (ForeignPtr a) where
91 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
93 instance Ord (ForeignPtr a) where
94 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
96 instance Show (ForeignPtr a) where
97 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
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
105 -- executed after the last reference to the foreign object is dropped.
106 -- There is no guarantee of promptness, however the finalizer will be
107 -- executed before the program exits.
108 newForeignPtr finalizer p
109 = do fObj <- newForeignPtr_ p
110 addForeignPtrFinalizer finalizer fObj
113 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
114 -- ^This is a way to look at the pointer living inside a
115 -- foreign object. This function takes a function which is
116 -- applied to that pointer. The resulting 'IO' action is then
117 -- executed. The foreign object is kept alive at least during
118 -- the whole action, even if it is not used directly
119 -- inside. Note that it is not safe to return the pointer from
120 -- the action and use it after the action completes. All uses
121 -- of the pointer should be inside the
122 -- 'withForeignPtr' bracket. The reason for
123 -- this unsafeness is the same as for
124 -- 'unsafeForeignPtrToPtr' below: the finalizer
125 -- may run earlier than expected, because the compiler can only
126 -- track usage of the 'ForeignPtr' object, not
127 -- a 'Ptr' object made from it.
129 -- This function is normally used for marshalling data to
130 -- or from the object pointed to by the
131 -- 'ForeignPtr', using the operations from the
134 = do r <- io (unsafeForeignPtrToPtr fo)
137 #endif /* ! __NHC__ */
139 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
140 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
141 -- environment in addition to the finalized pointer. The environment
142 -- that will be passed to the finalizer is fixed by the second argument to
143 -- 'newForeignPtrEnv'.
145 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
146 newForeignPtrEnv finalizer env p
147 = do fObj <- newForeignPtr_ p
148 addForeignPtrFinalizerEnv finalizer env fObj
150 #endif /* __HUGS__ */
152 #ifndef __GLASGOW_HASKELL__
153 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
154 mallocForeignPtr = do
156 newForeignPtr finalizerFree r
158 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
159 mallocForeignPtrBytes n = do
161 newForeignPtr finalizerFree r
162 #endif /* !__GLASGOW_HASKELL__ */
164 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
165 -- but yields a memory area that has a finalizer attached that releases
166 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
167 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
168 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
169 mallocForeignPtrArray = doMalloc undefined
171 doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
172 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
174 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
175 -- but yields a memory area that has a finalizer attached that releases
176 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
177 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
178 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
179 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)