19d31b858109e171b6e9c11644d1b2206557125a
[packages/base.git] / Foreign / ForeignPtr / Imp.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : Foreign.ForeignPtr.Imp
8 -- Copyright : (c) The University of Glasgow 2001
9 -- License : BSD-style (see the file libraries/base/LICENSE)
10 --
11 -- Maintainer : ffi@haskell.org
12 -- Stability : provisional
13 -- Portability : portable
14 --
15 -- The 'ForeignPtr' type and operations. This module is part of the
16 -- Foreign Function Interface (FFI) and will usually be imported via
17 -- the "Foreign" module.
18 --
19 -----------------------------------------------------------------------------
20
21 module Foreign.ForeignPtr.Imp
22 (
23 -- * Finalised data pointers
24 ForeignPtr
25 , FinalizerPtr
26 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
27 , FinalizerEnvPtr
28 #endif
29 -- ** Basic operations
30 , newForeignPtr
31 , newForeignPtr_
32 , addForeignPtrFinalizer
33 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
34 , newForeignPtrEnv
35 , addForeignPtrFinalizerEnv
36 #endif
37 , withForeignPtr
38
39 #ifdef __GLASGOW_HASKELL__
40 , finalizeForeignPtr
41 #endif
42
43 -- ** Low-level operations
44 , unsafeForeignPtrToPtr
45 , touchForeignPtr
46 , castForeignPtr
47
48 -- ** Allocating managed memory
49 , mallocForeignPtr
50 , mallocForeignPtrBytes
51 , mallocForeignPtrArray
52 , mallocForeignPtrArray0
53 )
54 where
55
56 import Foreign.Ptr
57
58 #ifdef __HUGS__
59 import Hugs.ForeignPtr
60 #endif
61
62 import Foreign.Storable ( Storable(sizeOf) )
63
64 #ifdef __GLASGOW_HASKELL__
65 import GHC.Base
66 import GHC.Num
67 import GHC.ForeignPtr
68 #endif
69
70 #if !defined(__GLASGOW_HASKELL__)
71 import Foreign.Marshal.Alloc ( malloc, mallocBytes, finalizerFree )
72
73 instance Eq (ForeignPtr a) where
74 p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
75
76 instance Ord (ForeignPtr a) where
77 compare p q = compare (unsafeForeignPtrToPtr p) (unsafeForeignPtrToPtr q)
78
79 instance Show (ForeignPtr a) where
80 showsPrec p f = showsPrec p (unsafeForeignPtrToPtr f)
81 #endif
82
83
84 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
85 -- ^Turns a plain memory reference into a foreign pointer, and
86 -- associates a finalizer with the reference. The finalizer will be
87 -- executed after the last reference to the foreign object is dropped.
88 -- There is no guarantee of promptness, however the finalizer will be
89 -- executed before the program exits.
90 newForeignPtr finalizer p
91 = do fObj <- newForeignPtr_ p
92 addForeignPtrFinalizer finalizer fObj
93 return fObj
94
95 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
96 -- ^This is a way to look at the pointer living inside a
97 -- foreign object. This function takes a function which is
98 -- applied to that pointer. The resulting 'IO' action is then
99 -- executed. The foreign object is kept alive at least during
100 -- the whole action, even if it is not used directly
101 -- inside. Note that it is not safe to return the pointer from
102 -- the action and use it after the action completes. All uses
103 -- of the pointer should be inside the
104 -- 'withForeignPtr' bracket. The reason for
105 -- this unsafeness is the same as for
106 -- 'unsafeForeignPtrToPtr' below: the finalizer
107 -- may run earlier than expected, because the compiler can only
108 -- track usage of the 'ForeignPtr' object, not
109 -- a 'Ptr' object made from it.
110 --
111 -- This function is normally used for marshalling data to
112 -- or from the object pointed to by the
113 -- 'ForeignPtr', using the operations from the
114 -- 'Storable' class.
115 withForeignPtr fo io
116 = do r <- io (unsafeForeignPtrToPtr fo)
117 touchForeignPtr fo
118 return r
119
120 #if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
121 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
122 -- environment in addition to the finalized pointer. The environment
123 -- that will be passed to the finalizer is fixed by the second argument to
124 -- 'newForeignPtrEnv'.
125 newForeignPtrEnv ::
126 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
127 newForeignPtrEnv finalizer env p
128 = do fObj <- newForeignPtr_ p
129 addForeignPtrFinalizerEnv finalizer env fObj
130 return fObj
131 #endif /* __HUGS__ */
132
133 #ifndef __GLASGOW_HASKELL__
134 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
135 mallocForeignPtr = do
136 r <- malloc
137 newForeignPtr finalizerFree r
138
139 mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
140 mallocForeignPtrBytes n = do
141 r <- mallocBytes n
142 newForeignPtr finalizerFree r
143 #endif /* !__GLASGOW_HASKELL__ */
144
145 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
146 -- but yields a memory area that has a finalizer attached that releases
147 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
148 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
149 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
150 mallocForeignPtrArray = doMalloc undefined
151 where
152 doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
153 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
154
155 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
156 -- but yields a memory area that has a finalizer attached that releases
157 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
158 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
159 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
160 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)
161