-addForeignPtrFinalizer :: ForeignPtr a -> IO () -> IO ()
--- ^This function adds another finaliser to the given
--- foreign object. No guarantees are made on the order in
--- which multiple finalisers for a single object are run.
-addForeignPtrFinalizer (ForeignPtr fo) finalizer =
- IO $ \s -> case mkWeak# fo () finalizer s of { (# s1, w #) -> (# s1, () #) }
-
-mkForeignPtr :: Ptr a -> IO (ForeignPtr a) {- not exported -}
-mkForeignPtr (Ptr obj) = IO ( \ s# ->
- case mkForeignObj# obj s# of
- (# s1#, fo# #) -> (# s1#, ForeignPtr fo# #) )
-
-touchForeignPtr :: ForeignPtr a -> IO ()
--- ^This function ensures that the foreign object in
--- question is alive at the given place in the sequence of IO
--- actions. In particular 'withForeignPtr'
--- does a 'touchForeignPtr' after it
--- executes the user action.
---
--- This function can be used to express liveness
--- dependencies between 'ForeignPtr's: for
--- example, if the finalizer for one
--- 'ForeignPtr' touches a second
--- 'ForeignPtr', then it is ensured that the
--- second 'ForeignPtr' will stay alive at
--- least as long as the first. This can be useful when you
--- want to manipulate /interior pointers/ to
--- a foreign structure: you can use
--- 'touchForeignObj' to express the
--- requirement that the exterior pointer must not be finalized
--- until the interior pointer is no longer referenced.
-touchForeignPtr (ForeignPtr fo)
- = IO $ \s -> case touch# fo s of s -> (# s, () #)
+#ifndef __NHC__
+newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
+-- ^Turns a plain memory reference into a foreign pointer, and
+-- associates a finaliser with the reference. The finaliser will be executed
+-- after the last reference to the foreign object is dropped. Note that there
+-- is no guarantee on how soon the finaliser is executed after the last
+-- reference was dropped; this depends on the details of the Haskell storage
+-- manager. Indeed, there is no guarantee that the finalizer is executed at
+-- all; a program may exit with finalizers outstanding. (This is true
+-- of GHC, other implementations may give stronger guarantees).
+newForeignPtr finalizer p
+ = do fObj <- newForeignPtr_ p
+ addForeignPtrFinalizer finalizer fObj
+ return fObj