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
45 , addForeignPtrFinalizer
47 , unsafeForeignPtrToPtr
51 , malloc, mallocBytes, finalizerFree
56 import Hugs.ForeignPtr
60 import Foreign.Storable ( Storable(sizeOf) )
63 #ifdef __GLASGOW_HASKELL__
67 import GHC.Err ( undefined )
71 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
72 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
76 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
78 instance Eq (ForeignPtr a) where
79 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
81 instance Ord (ForeignPtr a) where
82 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
84 instance Show (ForeignPtr a) where
85 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
90 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
91 -- ^This is a way to look at the pointer living inside a
92 -- foreign object. This function takes a function which is
93 -- applied to that pointer. The resulting 'IO' action is then
94 -- executed. The foreign object is kept alive at least during
95 -- the whole action, even if it is not used directly
96 -- inside. Note that it is not safe to return the pointer from
97 -- the action and use it after the action completes. All uses
98 -- of the pointer should be inside the
99 -- 'withForeignPtr' bracket. The reason for
100 -- this unsafety is the same as for
101 -- 'unsafeForeignPtrToPtr' below: the finalizer
102 -- may run earlier than expected, because the compiler can only
103 -- track usage of the 'ForeignPtr' object, not
104 -- a 'Ptr' object made from it.
106 -- This function is normally used for marshalling data to
107 -- or from the object pointed to by the
108 -- 'ForeignPtr', using the operations from the
111 = do r <- io (unsafeForeignPtrToPtr fo)
114 #endif /* ! __NHC__ */
117 -- temporary aliasing until hugs catches up
118 unsafeForeignPtrToPtr = foreignPtrToPtr
121 #ifndef __GLASGOW_HASKELL__
122 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
123 mallocForeignPtr = do
125 newForeignPtr r finalizerFree
127 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
128 mallocForeignPtrBytes n = do
130 newForeignPtr r finalizerFree
131 #endif /* __HUGS__ || __NHC__ */
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)