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