import Foreign.Storable
import GHC.Int
import Text.Printf
+import System.IO.Unsafe(unsafePerformIO)
import Control.Monad
+import Control.Monad.State
+
+import Test.QuickCheck
+import Test.QuickCheck.Monadic
instance RefObj (Ptr a) where
payload = return . ptrToIntPtr
markRef val ptr = pokeByteOff ptr markedOff val
setNewRefPtr :: Ptr a -> Ptr a -> IO ()
-setNewRefPtr ptr newPtr = pokeByteOff ptr newRefOff newPtr
+setNewRefPtr ptr = pokeByteOff ptr newRefOff
patchRefsPtr :: Ptr a -> [Ptr a] -> IO ()
-patchRefsPtr ptr xs = pokeArray (ptr `plusPtr` fieldsOff) xs
+patchRefsPtr ptr = pokeArray (ptr `plusPtr` fieldsOff)
printRef' :: Ptr a -> IO ()
printRef' ptr = do printf "obj 0x%08x\n" =<< (peekByteOff ptr idOff :: IO Int32)
printf "children 0x%08x\n" =<< (peekByteOff ptr numberOfObjsOff :: IO Int32)
printf "marked 0x%08x\n" =<< (peekByteOff ptr markedOff :: IO Int32)
- printf "payload 0x%08x\n" =<< ((liftM fromIntegral (payload ptr)) :: IO Int32)
+ printf "payload 0x%08x\n" =<< (liftM fromIntegral (payload ptr) :: IO Int32)
printf "newRef 0x%08x\n" =<< (peekByteOff ptr newRefOff :: IO Int32)
printChildren ptr
putStrLn ""
printChildren :: Ptr a -> IO ()
printChildren ptr = do children <- refs ptr
- putStrLn $ "children" ++ (show children)
+ putStrLn $ "children" ++ show children
printTree :: Ptr a -> IO ()
putStrLn "resulting objectTree"
printTree newRef
-
+
+createMemoryManager :: Property
+createMemoryManager = monadicIO $ run f >>= (assert . (==0))
+ where f :: IO Int
+ f = do twoSpace <- initTwoSpace 0x10000
+ evalStateT heapSize twoSpace
+
+
+createObject :: Int -> IO (Ptr a)
+createObject children = do mem <- mallocBytes (0x10 + 0x4 * children)
+ pokeArray mem [0,fromIntegral children,0::Int32,0]
+ fields <- replicateM children (createObject 0)
+ pokeArray (mem `plusPtr` fieldsOff) (fields :: [Ptr Int32])
+ return $ cast mem
+
+data ObjectTree = Node [ObjectTree] deriving Show
+
+instance Arbitrary ObjectTree where
+ arbitrary = resize 8 ( sized $ \n ->
+ do empty <- choose (0,100) :: Gen Int-- [True,False]
+ if empty < 80 then return $ Node []
+ else do k <- choose (0,n)
+ liftM Node $ sequence [ arbitrary | _ <- [1..k] ] )
+
+createObjects :: ObjectTree -> IO (Ptr a)
+createObjects (Node xs) = do let children = length xs
+ mem <- mallocBytes (0x10 + 0x4 * children)
+ pokeArray mem [0,fromIntegral children,0::Int32,0]
+ fields <- mapM createObjects xs
+ pokeArray (mem `plusPtr` fieldsOff) (fields :: [Ptr Int32])
+ return $ cast mem
+
+
+testObjectTree :: ObjectTree -> Property
+testObjectTree objTree = monadicIO $ run f >>= (assert . (==0))
+ where f :: IO Int
+ f = do root <- createObjects objTree
+ twoSpace <- initTwoSpace 0x1000
+ let collection = performCollection [root]
+ runStateT collection twoSpace
+ evalStateT heapSize twoSpace