From: qrczak Date: Tue, 13 Mar 2001 21:21:27 +0000 (+0000) Subject: [project @ 2001-03-13 21:21:27 by qrczak] X-Git-Tag: Approximately_9120_patches~2421 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=8abcce117d3c5195a78ae42c821e7882b606d260;p=ghc-hetmet.git [project @ 2001-03-13 21:21:27 by qrczak] Add 'destruct :: Ptr a -> IO ()' method to class Storable. Thanks Wojciech Moczydlowski for the suggestion. It should free memory associated with the object (except the object pointer itself). A default definition does nothing, but will be non-trivial for some C structs. The canonical ForeignPtr finalizer for a malloced data object would be 'destruct p >> free p'. --- diff --git a/ghc/lib/std/PrelStorable.lhs b/ghc/lib/std/PrelStorable.lhs index f02b832..0786954 100644 --- a/ghc/lib/std/PrelStorable.lhs +++ b/ghc/lib/std/PrelStorable.lhs @@ -1,5 +1,5 @@ % ----------------------------------------------------------------------------- -% $Id: PrelStorable.lhs,v 1.3 2001/02/28 00:01:03 qrczak Exp $ +% $Id: PrelStorable.lhs,v 1.4 2001/03/13 21:21:27 qrczak Exp $ % % (c) The FFI task force, 2000 % @@ -18,7 +18,8 @@ module PrelStorable peekByteOff, -- :: Ptr b -> Int -> IO a pokeByteOff, -- :: Ptr b -> Int -> a -> IO () peek, -- :: Ptr a -> IO a - poke) -- :: Ptr a -> a -> IO () + poke, -- :: Ptr a -> a -> IO () + destruct) -- :: Ptr a -> IO () ) where \end{code} @@ -64,6 +65,10 @@ class Storable a where peek :: Ptr a -> IO a poke :: Ptr a -> a -> IO () + -- free memory associated with the object + -- (except the object pointer itself) + destruct :: Ptr a -> IO () + -- circular default instances peekElemOff = peekElemOff_ undefined where peekElemOff_ :: a -> Ptr a -> Int -> IO a @@ -75,6 +80,8 @@ class Storable a where peek ptr = peekElemOff ptr 0 poke ptr = pokeElemOff ptr 0 + + destruct _ = return () \end{code} System-dependent, but rather obvious instances