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
24 , addForeignPtrFinalizer
26 , foreignPtrToPtr -- will soon become unsafeForeignPtrToPtr
32 , mallocForeignPtrBytes
33 , mallocForeignPtrArray
34 , mallocForeignPtrArray0
46 , addForeignPtrFinalizer
48 , unsafeForeignPtrToPtr
55 import Hugs.ForeignPtr
59 import Foreign.Storable ( Storable(sizeOf) )
62 #ifdef __GLASGOW_HASKELL__
66 import GHC.Err ( undefined )
70 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
71 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
75 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
77 instance Eq (ForeignPtr a) where
78 p == q = foreignPtrToPtr p == foreignPtrToPtr q
80 instance Ord (ForeignPtr a) where
81 compare p q = compare (foreignPtrToPtr p) (foreignPtrToPtr q)
83 instance Show (ForeignPtr a) where
84 showsPrec p f = showsPrec p (foreignPtrToPtr f)
89 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
90 -- ^This is a way to look at the pointer living inside a
91 -- foreign object. This function takes a function which is
92 -- applied to that pointer. The resulting 'IO' action is then
93 -- executed. The foreign object is kept alive at least during
94 -- the whole action, even if it is not used directly
95 -- inside. Note that it is not safe to return the pointer from
96 -- the action and use it after the action completes. All uses
97 -- of the pointer should be inside the
98 -- 'withForeignPtr' bracket. The reason for
99 -- this unsafety is the same as for
100 -- 'foreignPtrToPtr' below: the finalizer
101 -- may run earlier than expected, because the compiler can only
102 -- track usage of the 'ForeignPtr' object, not
103 -- a 'Ptr' object made from it.
105 -- This function is normally used for marshalling data to
106 -- or from the object pointed to by the
107 -- 'ForeignPtr', using the operations from the
110 = do r <- io (foreignPtrToPtr fo)
113 #endif /* ! __NHC__ */
116 -- temporary aliasing until ghc and hugs catch up
117 foreignPtrToPtr = unsafeForeignPtrToPtr
121 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
122 mallocForeignPtr = do
124 newForeignPtr r finalizerFree
126 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
127 mallocForeignPtrBytes n = do
129 newForeignPtr r finalizerFree
130 #endif /* __HUGS__ */
133 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
134 mallocForeignPtrArray = doMalloc undefined
136 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
137 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
139 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
140 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)