-- Tests ForeignPtrEnv finalizers

import Text.Printf
import Foreign.ForeignPtr
import Foreign
import GHC.TopHandler
import Control.Concurrent
import Data.List
import System.Mem

foreign export ccall fin :: Ptr Int -> Ptr Int -> IO ()
foreign import ccall "&fin" finptr :: FinalizerEnvPtr Int Int

fin :: Ptr Int -> Ptr Int -> IO ()
fin envp ap = runIO $ do
  env <- peek envp
  a <- peek ap
  printf "%d %d\n" env a
  return ()

main = do
  a   <- new (55 :: Int)
  env <- new (66 :: Int)
  fp  <- newForeignPtrEnv finptr env a
  performGC
  threadDelay 100000

-- Tests Foreign.Concurrent finalizers

import Text.Printf
import Foreign.Concurrent as Conc
import Foreign
import GHC.TopHandler
import Control.Concurrent
import Data.List
import System.Mem

-- This finalizer calls back into Haskell, so we can't use
-- the ordinary newForeignPtr.
foreign export ccall fin :: Ptr Int -> Ptr Int -> IO ()
foreign import ccall "fin" finptr :: Ptr Int -> Ptr Int -> IO ()

fin :: Ptr Int -> Ptr Int -> IO ()
fin envp ap = runIO $ do
  env <- peek envp
  a <- peek ap
  printf "%d %d\n" env a
  return ()

main = do
  a   <- new (55 :: Int)
  env <- new (66 :: Int)
  fp  <- Conc.newForeignPtr a (fin env a)
  performGC
  threadDelay 100000

Fix this test: it was using a C finalizer that called back into Haskell Which isn't allowed using the ordinary newForeignPtr any more, it has to be done with Foreign.Concurrent.newForeignPtr.
Fix this test: it was using a C finalizer that called back into Haskell
Which isn't allowed using the ordinary newForeignPtr any more, it has
to be done with Foreign.Concurrent.newForeignPtr.
