codegen: handle exceptions of a method
[mate.git] / Mate / GarbageAlloc.hs
index b0165d0b9453a3eef149ab1b2213659060e2a2ab..617e571ba6d1407d7ad99200e4724478b94a6a6d 100644 (file)
@@ -1,20 +1,62 @@
 {-# LANGUAGE ForeignFunctionInterface #-}
-module Mate.GarbageAlloc where
+module Mate.GarbageAlloc(
+    mallocClassData,
+    mallocStringGC,
+    mallocObjectGC,
+    getHeapMemory,
+    printMemoryUsage,
+    printGCStats,
+    mallocObjectUnmanaged,
+    mallocStringUnmanaged)  where
 
 import Foreign
 import Foreign.C
 
+import Mate.GC.Boehm
+
+import Mate.Debug
+
 -- unified place for allocating Memory
 -- TODO: implement GC stuff ;-)
 
 mallocClassData :: Int -> IO (Ptr a)
-mallocClassData = mallocBytes
+mallocClassData size = do
+  printfStr $ printf "mallocClassData: %d\n" size
+  mem <- mallocBytes size
+  addRootGC mem (plusPtr mem size)
+  return mem
 
-mallocString :: Int -> IO (Ptr a)
-mallocString = mallocBytes
+mallocStringGC :: Int -> IO (Ptr a)
+mallocStringGC size = do
+  printfStr $ printf "mallocString: %d\n" size
+  mallocBytesGC size
 
-foreign export ccall mallocObject :: Int -> IO CUInt
-mallocObject :: Int -> IO CUInt
-mallocObject size = do
+foreign export ccall mallocObjectGC :: Int -> IO CPtrdiff
+mallocObjectGC :: Int -> IO CPtrdiff
+mallocObjectGC size = do
+  ptr <- mallocBytesGC size
+  printfStr $ printf "mallocObject: %d\n" size
+  return $ fromIntegral $ ptrToIntPtr ptr
+
+mallocObjectUnmanaged :: Int -> IO CPtrdiff
+mallocObjectUnmanaged size = do
   ptr <- mallocBytes size
+  printfStr $ printf "mallocObjectUnmanged: %d\n" size
   return $ fromIntegral $ ptrToIntPtr ptr
+
+mallocStringUnmanaged :: Int -> IO (Ptr a)
+mallocStringUnmanaged size = do
+  printfStr $ printf "mallocStringUnamaged: %d\n" size
+  mallocBytes size
+
+
+getHeapMemory :: IO Int
+getHeapMemory = getHeapSizeGC
+
+foreign export ccall printMemoryUsage :: IO ()
+printMemoryUsage :: IO ()
+printMemoryUsage = getHeapMemory >>= print
+
+foreign export ccall printGCStats :: IO ()
+printGCStats :: IO ()
+printGCStats = putStrLn "Should print GC Stats"