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
32 , mallocForeignPtrBytes
33 , mallocForeignPtrArray
34 , mallocForeignPtrArray0
45 , addForeignPtrFinalizer
54 import Hugs.ForeignPtr
58 import Foreign.Storable ( Storable(sizeOf) )
61 #ifdef __GLASGOW_HASKELL__
65 import GHC.Err ( undefined )
69 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
70 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
74 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
76 instance Eq (ForeignPtr a) where
77 p == q = foreignPtrToPtr p == foreignPtrToPtr q
79 instance Ord (ForeignPtr a) where
80 compare p q = compare (foreignPtrToPtr p) (foreignPtrToPtr q)
82 instance Show (ForeignPtr a) where
83 showsPrec p f = showsPrec p (foreignPtrToPtr f)
87 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
88 -- ^This is a way to look at the pointer living inside a
89 -- foreign object. This function takes a function which is
90 -- applied to that pointer. The resulting 'IO' action is then
91 -- executed. The foreign object is kept alive at least during
92 -- the whole action, even if it is not used directly
93 -- inside. Note that it is not safe to return the pointer from
94 -- the action and use it after the action completes. All uses
95 -- of the pointer should be inside the
96 -- 'withForeignPtr' bracket. The reason for
97 -- this unsafety is the same as for
98 -- 'foreignPtrToPtr' below: the finalizer
99 -- may run earlier than expected, because the compiler can only
100 -- track usage of the 'ForeignPtr' object, not
101 -- a 'Ptr' object made from it.
103 -- This function is normally used for marshalling data to
104 -- or from the object pointed to by the
105 -- 'ForeignPtr', using the operations from the
108 = do r <- io (foreignPtrToPtr fo)
111 #endif /* ! __NHC__ */
114 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
115 mallocForeignPtr = do
117 newForeignPtr r finalizerFree
119 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
120 mallocForeignPtrBytes n = do
122 newForeignPtr r finalizerFree
123 #endif /* __HUGS__ */
126 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
127 mallocForeignPtrArray = doMalloc undefined
129 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
130 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
132 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
133 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)