ForeignPtr reloaded

Bulat Ziganshin bulat.ziganshin at gmail.com
Sun Jun 4 11:01:54 EDT 2006


Hello Donald,

Sunday, June 4, 2006, 11:22:24 AM, you wrote:

> +-- ForeignPtr created with mallocPlainForeignPtr carries no finalizers.
> +-- It is not possible to add a finalizer to a ForeignPtr created with
> +-- mallocPlainForeignPtr. This is useful for ForeignPtrs that will live
> +-- only inside Haskell (such as those created for packed strings).
> +-- Attempts to add a finalizer to a ForeignPtr created this way, or to
> +-- finalize such a pointer, will have no effect.

i think that we should raise exception when someone tries to add
finalizer to plain foreign ptr:

> hunk ./GHC/ForeignPtr.hs 234
> +addForeignPtrConcFinalizer_ _ _ = return () -- has no effect
> +

should become

addForeignPtrConcFinalizer_ _ _ = fail ...

(i'm a little crazy on handling unusual situations. once i've written
program that freed up used EMS memory in case of error, but leave it
allocated when program successfully finished :)


second, this module use a bad practice of repeating the same
allocation code. we can call from mallocPlainForeignPtr to
mallocPlainForeignPtrBytes and the same for
mallocForeignPtr/mallocForeignPtrBytes pair:

mallocForeignPtr :: Storable a => IO (ForeignPtr a)
mallocForeignPtr = doMalloc undefined
  where doMalloc :: Storable b => b -> IO (ForeignPtr b)
        doMalloc a = mallocForeignPtrBytes (sizeOf a)

we can go even further and use ghc-specific code:

mallocForeignPtr :: forall a . Storable a => IO (ForeignPtr a)
mallocForeignPtr = mallocForeignPtrBytes (sizeOf (undefined::a))



-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Cvs-ghc mailing list