1 module Mate.Tests.MockRefs where
6 import Foreign.Marshal.Alloc
7 import Foreign.Marshal.Array
8 import Foreign.Storable
14 instance RefObj (Ptr a) where
15 payload = return . ptrToIntPtr
16 size a = fmap ((+ 0x10) . length) (refs a)
17 refs = unpackRefs . castPtr
19 mark = markRef (0x1::Int32)
20 unmark = markRef (0x0::Int32)
25 instance PrintableRef (Ptr a) where
35 unpackRefs :: Ptr Int32 -> IO [Ptr b]
36 unpackRefs ptr = do --dereference number of objs; mark field skipped via fieldsOffset
37 numberOfObjs <- peekByteOff ptr numberOfObjsOff :: IO Int32
38 mapM (peekElemOff (ptr `plusPtr` fieldsOff)) [0..fromIntegral $ numberOfObjs-1]
40 markedRef :: Ptr a -> IO Bool
41 markedRef ptr = liftM ((/=0) . fromIntegral) (peekByteOff ptr markedOff :: IO Int32)
43 markRef :: Int32 -> Ptr a -> IO ()
44 markRef val ptr = pokeByteOff ptr markedOff val
46 newRefPtr :: Ptr a -> Ptr a -> IO ()
47 newRefPtr ptr = pokeByteOff ptr newRefOff
49 printRef' :: Ptr a -> IO ()
50 printRef' ptr = do printf "obj 0x%08x\n" =<< (peekByteOff ptr idOff :: IO Int32)
51 printf "children 0x%08x\n" =<< (peekByteOff ptr numberOfObjsOff :: IO Int32)
52 printf "marked 0x%08x\n" =<< (peekByteOff ptr markedOff :: IO Int32)
53 printf "newRef 0x%08x\n\n" =<< (peekByteOff ptr newRefOff :: IO Int32)
55 printTree :: Ptr a -> IO ()
56 printTree = traverseIO printRef'
58 emptyObj id = do mem <- mallocBytes 0x10
59 pokeArray mem [id,0,0::Int32,0]
62 twoRefs = do mem <- mallocBytes 0x18
63 -- idOfObj; numberofObj; marked waste memory Int32
64 pokeArray mem [0::Int32,2,0,0]
67 pokeByteOff mem 0x10 obj1
68 pokeByteOff mem 0x14 obj2
71 cyclR = do mem <- mallocBytes 0x1C
72 pokeArray mem [0::Int32,3,0,0]
75 pokeByteOff mem 0x10 obj1
76 pokeByteOff mem 0x14 obj2
77 pokeByteOff mem 0x18 mem
80 test objr = do twoRefs <- objr
83 lifeRefs <- markTree'' marked mark [] twoRefs
84 putStrLn "life refs: \n"
86 --forM lifeRefs printRef'
87 putStrLn "after marking\n"
89 markTree'' (liftM not . marked) unmark [] twoRefs
90 putStrLn "after unmarking\n"