-mallocForeignPtr :: Storable a => IO (ForeignPtr a)
-mallocForeignPtr = doMalloc undefined
- where doMalloc :: Storable a => a -> IO (ForeignPtr a)
- doMalloc a = IO $ \s ->
- case newPinnedByteArray# size s of { (# s, mbarr# #) ->
- (# s, MallocPtr mbarr# #)
- }
- where (I# size) = sizeOf a
-
--- | similar to 'mallocForeignPtr', except that the size of the memory required
--- is given explicitly as a number of bytes.
-mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
-mallocForeignPtrBytes (I# size) = IO $ \s ->
- case newPinnedByteArray# size s of { (# s, mbarr# #) ->
- (# s, MallocPtr mbarr# #)
- }
-
-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, () #) }
-addForeignPtrFinalizer (MallocPtr 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, () #)
-touchForeignPtr (MallocPtr 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 finalizer with the reference. The finalizer will be
+-- executed after the last reference to the foreign object is dropped.
+-- There is no guarantee of promptness, however the finalizer will be
+-- executed before the program exits.
+newForeignPtr finalizer p
+ = do fObj <- newForeignPtr_ p
+ addForeignPtrFinalizer finalizer fObj
+ return fObj