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
25 , addForeignPtrFinalizer
27 , unsafeForeignPtrToPtr
32 , mallocForeignPtrBytes
33 , mallocForeignPtrArray
34 , mallocForeignPtrArray0
46 , addForeignPtrFinalizer
48 , unsafeForeignPtrToPtr
52 , malloc, mallocBytes, finalizerFree
57 import Hugs.ForeignPtr
61 import Foreign.Storable ( Storable(sizeOf) )
64 #ifdef __GLASGOW_HASKELL__
68 import GHC.Err ( undefined )
72 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
73 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
77 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
79 instance Eq (ForeignPtr a) where
80 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
82 instance Ord (ForeignPtr a) where
83 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
85 instance Show (ForeignPtr a) where
86 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
91 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
92 -- ^Turns a plain memory reference into a foreign pointer, and
93 -- associates a finaliser with the reference. The finaliser will be executed
94 -- after the last reference to the foreign object is dropped. Note that there
95 -- is no guarantee on how soon the finaliser is executed after the last
96 -- reference was dropped; this depends on the details of the Haskell storage
97 -- manager. The only guarantee is that the finaliser runs before the program
99 newForeignPtr finalizer p
100 = do fObj <- newForeignPtr_ p
101 addForeignPtrFinalizer finalizer fObj
104 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
105 -- ^This is a way to look at the pointer living inside a
106 -- foreign object. This function takes a function which is
107 -- applied to that pointer. The resulting 'IO' action is then
108 -- executed. The foreign object is kept alive at least during
109 -- the whole action, even if it is not used directly
110 -- inside. Note that it is not safe to return the pointer from
111 -- the action and use it after the action completes. All uses
112 -- of the pointer should be inside the
113 -- 'withForeignPtr' bracket. The reason for
114 -- this unsafety is the same as for
115 -- 'unsafeForeignPtrToPtr' below: the finalizer
116 -- may run earlier than expected, because the compiler can only
117 -- track usage of the 'ForeignPtr' object, not
118 -- a 'Ptr' object made from it.
120 -- This function is normally used for marshalling data to
121 -- or from the object pointed to by the
122 -- 'ForeignPtr', using the operations from the
125 = do r <- io (unsafeForeignPtrToPtr fo)
128 #endif /* ! __NHC__ */
130 #ifndef __GLASGOW_HASKELL__
131 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
132 mallocForeignPtr = do
134 newForeignPtr finalizerFree r
136 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
137 mallocForeignPtrBytes n = do
139 newForeignPtr finalizerFree r
140 #endif /* __HUGS__ || __NHC__ */
142 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
143 mallocForeignPtrArray = doMalloc undefined
145 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
146 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
148 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
149 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)