From 280c9c440fc6117911c329f9ce5556bac75b359b Mon Sep 17 00:00:00 2001 From: Harald Steinlechner Date: Sun, 26 Aug 2012 16:50:00 +0200 Subject: [PATCH] scratch/GC: playground for datastructures to be used in GC - probably... --- scratch/GC.hs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 scratch/GC.hs diff --git a/scratch/GC.hs b/scratch/GC.hs new file mode 100644 index 0000000..c78eb50 --- /dev/null +++ b/scratch/GC.hs @@ -0,0 +1,60 @@ +{-# LANGUAGE GADTs #-} +{-# LANGUAGE ExistentialQuantification #-} +module GC where + +import Control.Monad + +import Foreign.Ptr +import Foreign.Marshal.Alloc +import Foreign.Storable +import Foreign.C.Types +import GHC.Int +import Text.Printf + +class RefObject a where + mem :: a -> IntPtr + refs :: a -> [IntPtr] + + +data RefObj = RefObj IntPtr [IntPtr] deriving Show + +instance RefObject RefObj where + mem (RefObj mem _ ) = mem + refs (RefObj _ refs) = refs + +data Succ = forall a. (RefObject a) => Succ (a -> [a]) + +obj2 = do buffer <- mallocBytes 4 + pokeByteOff buffer 0 (0::Int32) + return buffer + +obj3 = do buffer <- mallocBytes 4 + pokeByteOff buffer 0 (0::Int32) + return buffer + +obj1 f g = do buffer <- mallocBytes 12 + pokeByteOff buffer 0 (2::Int32) + pokeByteOff buffer 4 f + pokeByteOff buffer 8 g + return buffer + +ptrToRefObj ptr = do objCount <- peek ptr :: IO Int32 + let objsBase = ptr `plusPtr` 4 + objs <- mapM ((liftM ptrToIntPtr) . peekElemOff objsBase . fromIntegral) [0..objCount-1] + return $ RefObj (ptrToIntPtr ptr) objs + +test1 = do f <- obj2 + g <- obj3 + (print . ptrToIntPtr) f + (print . ptrToIntPtr) g + ptrToRefObj =<< obj1 f g + +traverse :: (RefObject a) => (IntPtr -> IO a) -> a -> [a] -> IO [a] +traverse dereference x ws = do children <- mapM dereference (refs x) + undefined + +succMem :: Ptr a -> Succ +succMem =undefined-- Succ (\_ -> obj1 + + + -- 2.25.1