| 1 | {-# LANGUAGE EmptyDataDecls, ForeignFunctionInterface #-} |
|---|
| 2 | {-# OPTIONS_GHC -main-is WrapperTest2 #-} |
|---|
| 3 | module WrapperTest2(main) where |
|---|
| 4 | |
|---|
| 5 | import Foreign.Ptr |
|---|
| 6 | import Foreign.ForeignPtr |
|---|
| 7 | import Foreign.C |
|---|
| 8 | |
|---|
| 9 | data FnBlob |
|---|
| 10 | |
|---|
| 11 | foreign import ccall "&free_fn_blob" free_fn_blob :: FunPtr (Ptr FnBlob -> IO ()) |
|---|
| 12 | |
|---|
| 13 | foreign import ccall safe "call_fn_blob" call_fn_blob :: Ptr FnBlob -> CDouble -> CDouble |
|---|
| 14 | |
|---|
| 15 | type DoubleFn = CDouble -> CDouble |
|---|
| 16 | |
|---|
| 17 | foreign import ccall unsafe "create_fn_blob" create_fn_blob :: FunPtr DoubleFn -> FunPtr (FunPtr DoubleFn -> IO ()) -> IO (Ptr FnBlob) |
|---|
| 18 | |
|---|
| 19 | foreign import ccall unsafe "&freeHaskellFunctionPtr" free_fun_ptr :: FunPtr (FunPtr DoubleFn -> IO()) |
|---|
| 20 | |
|---|
| 21 | foreign import ccall "wrapper" wrapDoubleFn :: DoubleFn -> IO (FunPtr DoubleFn) |
|---|
| 22 | |
|---|
| 23 | createFnBlob :: DoubleFn -> IO (ForeignPtr FnBlob) |
|---|
| 24 | createFnBlob dfn = do |
|---|
| 25 | dfn_ptr <- wrapDoubleFn dfn |
|---|
| 26 | ptr_fnblob <- create_fn_blob dfn_ptr free_fun_ptr |
|---|
| 27 | newForeignPtr free_fn_blob ptr_fnblob |
|---|
| 28 | |
|---|
| 29 | callFnBlob :: ForeignPtr FnBlob -> CDouble -> IO (CDouble) |
|---|
| 30 | callFnBlob fnblob d = withForeignPtr fnblob $ |
|---|
| 31 | \ptrblob -> return(call_fn_blob ptrblob d) |
|---|
| 32 | |
|---|
| 33 | main = do |
|---|
| 34 | putStrLn "start" |
|---|
| 35 | step 0 |
|---|
| 36 | putStrLn "done" |
|---|
| 37 | |
|---|
| 38 | step n | n > 1000 = return () |
|---|
| 39 | step n = do |
|---|
| 40 | fnBlob <- createFnBlob (+ n) |
|---|
| 41 | result <- callFnBlob fnBlob 0 |
|---|
| 42 | putStrLn $ "step " ++ show n ++ ": " ++ show result |
|---|
| 43 | step (n + 1) |
|---|