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 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
92 -- ^This is a way to look at the pointer living inside a
93 -- foreign object. This function takes a function which is
94 -- applied to that pointer. The resulting 'IO' action is then
95 -- executed. The foreign object is kept alive at least during
96 -- the whole action, even if it is not used directly
97 -- inside. Note that it is not safe to return the pointer from
98 -- the action and use it after the action completes. All uses
99 -- of the pointer should be inside the
100 -- 'withForeignPtr' bracket. The reason for
101 -- this unsafety is the same as for
102 -- 'unsafeForeignPtrToPtr' below: the finalizer
103 -- may run earlier than expected, because the compiler can only
104 -- track usage of the 'ForeignPtr' object, not
105 -- a 'Ptr' object made from it.
107 -- This function is normally used for marshalling data to
108 -- or from the object pointed to by the
109 -- 'ForeignPtr', using the operations from the
112 = do r <- io (unsafeForeignPtrToPtr fo)
115 #endif /* ! __NHC__ */
118 -- temporary aliasing until hugs catches up
119 unsafeForeignPtrToPtr = foreignPtrToPtr
122 #ifndef __GLASGOW_HASKELL__
123 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
124 mallocForeignPtr = do
126 newForeignPtr r finalizerFree
128 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
129 mallocForeignPtrBytes n = do
131 newForeignPtr r finalizerFree
132 #endif /* __HUGS__ || __NHC__ */
134 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
135 mallocForeignPtrArray = doMalloc undefined
137 doMalloc :: Storable a => a -> Int -> IO (ForeignPtr a)
138 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
140 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
141 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)