blob: 1d20a48e14f20d2e9c4f7ce7c5ce93d6ec625984 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
{-# INCLUDE "fptr01.h" #-}
foreign import ccall "&f" fptr :: FunPtr (Ptr Int -> IO ())
foreign import ccall "&g" gptr :: FunPtr (Ptr Int -> IO ())
foreign import ccall "&h" hptr :: FunPtr (Ptr Int -> IO ())
foreign import ccall "&f_env" fenvptr :: FunPtr (Ptr Int -> Ptr Int -> IO ())
main :: IO ()
main = do
with (33 :: Int) ((>>= finalizeForeignPtr) . test)
with (34 :: Int) ((>> return ()) . test)
with (35 :: Int) ((>>= finalizeForeignPtr) . test_env)
with (36 :: Int) ((>> return ()) . test_env)
-- finalizers must all be run at program exit.
where
-- the finalizers must be run in the correct order, starting with
-- the most recently-added.
test p = do
f <- newForeignPtr_ p
addForeignPtrFinalizer fptr f
addForeignPtrFinalizer gptr f
addForeignPtrFinalizer hptr f
return f
test_env p = do
f <- newForeignPtr_ p
envp1 <- new 1
envp2 <- new 2
envp3 <- new 3
addForeignPtrFinalizerEnv fenvptr envp1 f
addForeignPtrFinalizerEnv fenvptr envp2 f
addForeignPtrFinalizerEnv fenvptr envp3 f
return f
|