[project @ 2003-05-22 08:24:32 by chak]
[ghc-base.git] / Foreign / ForeignPtr.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Foreign.ForeignPtr
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  ffi@haskell.org
9 -- Stability   :  provisional
10 -- Portability :  portable
11 --
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.
15 --
16 -----------------------------------------------------------------------------
17
18 module Foreign.ForeignPtr
19         ( 
20         -- * Finalised data pointers
21           ForeignPtr
22         , FinalizerPtr
23         , newForeignPtr
24         , addForeignPtrFinalizer
25         , withForeignPtr
26         , foreignPtrToPtr
27         , touchForeignPtr
28         , castForeignPtr
29
30 #ifndef __NHC__
31         , mallocForeignPtr
32         , mallocForeignPtrBytes
33         , mallocForeignPtrArray
34         , mallocForeignPtrArray0
35 #endif
36         ) 
37         where
38
39 import Foreign.Ptr
40
41 #ifdef __NHC__
42 import NHC.FFI
43   ( ForeignPtr
44   , newForeignPtr
45   , addForeignPtrFinalizer
46   , withForeignPtr
47   , foreignPtrToPtr
48   , touchForeignPtr
49   , castForeignPtr
50   )
51 #endif
52
53 #ifdef __HUGS__
54 import Hugs.ForeignPtr
55 #endif
56
57 #ifndef __NHC__
58 import Foreign.Storable ( Storable(sizeOf) )
59 #endif
60
61 #ifdef __GLASGOW_HASKELL__
62 import GHC.Base
63 import GHC.IOBase
64 import GHC.Num
65 import GHC.Err          ( undefined )
66 import GHC.ForeignPtr
67 #endif
68
69 #if !defined(__NHC__) && !defined(__GLASGOW_HASKELL__)
70 import Foreign.Marshal.Alloc    ( malloc, mallocBytes, finalizerFree )
71 import Data.Dynamic
72
73 #include "Dynamic.h"
74 INSTANCE_TYPEABLE1(ForeignPtr,foreignPtrTc,"ForeignPtr")
75
76 instance Eq (ForeignPtr a) where 
77     p == q  =  foreignPtrToPtr p == foreignPtrToPtr q
78
79 instance Ord (ForeignPtr a) where 
80     compare p q  =  compare (foreignPtrToPtr p) (foreignPtrToPtr q)
81
82 instance Show (ForeignPtr a) where
83     showsPrec p f = showsPrec p (foreignPtrToPtr f)
84 #endif
85
86 #ifndef __NHC__
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.
102 --
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
106 -- 'Storable' class.
107 withForeignPtr fo io
108   = do r <- io (foreignPtrToPtr fo)
109        touchForeignPtr fo
110        return r
111 #endif /* ! __NHC__ */
112
113 #ifdef __HUGS__
114 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
115 mallocForeignPtr = do
116   r <- malloc
117   newForeignPtr r finalizerFree
118
119 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
120 mallocForeignPtrBytes n = do
121   r <- mallocBytes n
122   newForeignPtr r finalizerFree
123 #endif /* __HUGS__ */
124
125 #ifndef __NHC__
126 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
127 mallocForeignPtrArray  = doMalloc undefined
128   where
129     doMalloc            :: Storable a => a -> Int -> IO (ForeignPtr a)
130     doMalloc dummy size  = mallocForeignPtrBytes (size * sizeOf dummy)
131
132 mallocForeignPtrArray0      :: Storable a => Int -> IO (ForeignPtr a)
133 mallocForeignPtrArray0 size  = mallocForeignPtrArray (size + 1)
134 #endif