Typo
[packages/base.git] / Foreign / ForeignPtr / Imp.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE 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 , FinalizerEnvPtr
27
28 -- ** Basic operations
29 , newForeignPtr
30 , newForeignPtr_
31 , addForeignPtrFinalizer
32 , newForeignPtrEnv
33 , addForeignPtrFinalizerEnv
34 , withForeignPtr
35 , finalizeForeignPtr
36
37 -- ** Low-level operations
38 , unsafeForeignPtrToPtr
39 , touchForeignPtr
40 , castForeignPtr
41
42 -- ** Allocating managed memory
43 , mallocForeignPtr
44 , mallocForeignPtrBytes
45 , mallocForeignPtrArray
46 , mallocForeignPtrArray0
47 )
48 where
49
50 import Foreign.Ptr
51 import Foreign.Storable ( Storable(sizeOf) )
52
53 import GHC.Base
54 import GHC.Num
55 import GHC.ForeignPtr
56
57 newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
58 -- ^Turns a plain memory reference into a foreign pointer, and
59 -- associates a finalizer with the reference. The finalizer will be
60 -- executed after the last reference to the foreign object is dropped.
61 -- There is no guarantee of promptness, however the finalizer will be
62 -- executed before the program exits.
63 newForeignPtr finalizer p
64 = do fObj <- newForeignPtr_ p
65 addForeignPtrFinalizer finalizer fObj
66 return fObj
67
68 withForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
69 -- ^This is a way to look at the pointer living inside a
70 -- foreign object. This function takes a function which is
71 -- applied to that pointer. The resulting 'IO' action is then
72 -- executed. The foreign object is kept alive at least during
73 -- the whole action, even if it is not used directly
74 -- inside. Note that it is not safe to return the pointer from
75 -- the action and use it after the action completes. All uses
76 -- of the pointer should be inside the
77 -- 'withForeignPtr' bracket. The reason for
78 -- this unsafeness is the same as for
79 -- 'unsafeForeignPtrToPtr' below: the finalizer
80 -- may run earlier than expected, because the compiler can only
81 -- track usage of the 'ForeignPtr' object, not
82 -- a 'Ptr' object made from it.
83 --
84 -- This function is normally used for marshalling data to
85 -- or from the object pointed to by the
86 -- 'ForeignPtr', using the operations from the
87 -- 'Storable' class.
88 withForeignPtr fo io
89 = do r <- io (unsafeForeignPtrToPtr fo)
90 touchForeignPtr fo
91 return r
92
93 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
94 -- environment in addition to the finalized pointer. The environment
95 -- that will be passed to the finalizer is fixed by the second argument to
96 -- 'newForeignPtrEnv'.
97 newForeignPtrEnv ::
98 FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO (ForeignPtr a)
99 newForeignPtrEnv finalizer env p
100 = do fObj <- newForeignPtr_ p
101 addForeignPtrFinalizerEnv finalizer env fObj
102 return fObj
103
104 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray',
105 -- but yields a memory area that has a finalizer attached that releases
106 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
107 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
108 mallocForeignPtrArray :: Storable a => Int -> IO (ForeignPtr a)
109 mallocForeignPtrArray = doMalloc undefined
110 where
111 doMalloc :: Storable b => b -> Int -> IO (ForeignPtr b)
112 doMalloc dummy size = mallocForeignPtrBytes (size * sizeOf dummy)
113
114 -- | This function is similar to 'Foreign.Marshal.Array.mallocArray0',
115 -- but yields a memory area that has a finalizer attached that releases
116 -- the memory area. As with 'mallocForeignPtr', it is not guaranteed that
117 -- the block of memory was allocated by 'Foreign.Marshal.Alloc.malloc'.
118 mallocForeignPtrArray0 :: Storable a => Int -> IO (ForeignPtr a)
119 mallocForeignPtrArray0 size = mallocForeignPtrArray (size + 1)
120