MemoryManager: twoSpace memorymanager initialization code; here its time for monad...
authorHarald Steinlechner <haraldsteinlechner@gmail.com>
Mon, 27 Aug 2012 18:16:53 +0000 (20:16 +0200)
committerHarald Steinlechner <haraldsteinlechner@gmail.com>
Mon, 27 Aug 2012 18:16:53 +0000 (20:16 +0200)
Mate/GC.hs
Mate/MemoryManager.hs

index 9781d207183332f517a3cea430aaf871d9cf6c83..8f37216b747d36f3dca91183691075cc413fca08 100644 (file)
@@ -1,5 +1,5 @@
 {-# LANGUAGE ScopedTypeVariables #-}
-module GC 
+module Mate.GC 
   ({- dont export generic versions for high performance ;-) -}) where
 
 import Control.Monad
index 8ddde1de9c850ba9625b8a28267065acb0aa00fb..57aec48cfed67a98f7cd74df9c3403c90f5efef5 100644 (file)
@@ -1,12 +1,16 @@
-module MemoryManager ( ) where
+{-# LANGUAGE ExistentialQuantification #-}
+module Mate.MemoryManager ( ) where
 
 import qualified Foreign.Marshal.Alloc as Alloc
 import Foreign.Ptr
 import Foreign.Storable
 
+import Data.HashTable
+
+import Text.Printf
 import Control.Monad.State
 
---import GC
+import Mate.GC
 
 class AllocationManager a where
   mallocBytes :: a -> Int -> (a,Ptr b)
@@ -29,3 +33,24 @@ mallocBytes' bytes = do state <- get
                              put $ state { toHeap = end } 
                              return $ intPtrToPtr ptr
         fail = error "no space left in two space (mallocBytes')"
+
+-- here its time for monadtransformer :)
+evacuate :: [Ptr a] -> State TwoSpace (IO (HashTable (Ptr a) (Ptr a)))
+evacuate = undefined
+
+
+initTwoSpace :: Int -> IO TwoSpace
+initTwoSpace size =  do printf "initializing TwoSpace memory manager with %d bytes." size
+                        fromSpace <- Alloc.mallocBytes size
+                        toSpace   <- Alloc.mallocBytes size
+                        if fromSpace /= nullPtr && toSpace /= nullPtr 
+                           then return $ buildToSpace fromSpace toSpace
+                           else error "Could not initialize TwoSpace memory manager (malloc returned null ptr)"
+   where buildToSpace from to = let fromBase' = ptrToIntPtr from
+                                    toBase' = ptrToIntPtr to
+                                    fromExtreme' = ptrToIntPtr $ from `plusPtr` size
+                                    toExtreme' = ptrToIntPtr $ to `plusPtr` size
+                                in TwoSpace { fromBase = fromBase', toBase = toBase',
+                                              fromHeap = fromBase', toHeap = toBase',
+                                              fromExtreme = fromExtreme', toExtreme = toExtreme' }
+