codegen: handle exceptions of a method
[mate.git] / Mate / GarbageAlloc.hs
index d993f93e68f2af865c3ee841e51fb0cfe21fe363..617e571ba6d1407d7ad99200e4724478b94a6a6d 100644 (file)
@@ -1,18 +1,19 @@
 {-# LANGUAGE ForeignFunctionInterface #-}
-{-# LANGUAGE CPP #-}
-#include "debug.h"
 module Mate.GarbageAlloc(
     mallocClassData,
-    mallocString,
-    mallocObject,
-    getHeapMemory)  where
+    mallocStringGC,
+    mallocObjectGC,
+    getHeapMemory,
+    printMemoryUsage,
+    printGCStats,
+    mallocObjectUnmanaged,
+    mallocStringUnmanaged)  where
 
 import Foreign
 import Foreign.C
 
 import Mate.GC.Boehm
 
-import Text.Printf
 import Mate.Debug
 
 -- unified place for allocating Memory
@@ -20,28 +21,42 @@ import Mate.Debug
 
 mallocClassData :: Int -> IO (Ptr a)
 mallocClassData size = do
-  printfStr "mallocClassData: %d\n" size
+  printfStr $ printf "mallocClassData: %d\n" size
+  mem <- mallocBytes size
+  addRootGC mem (plusPtr mem size)
+  return mem
+
+mallocStringGC :: Int -> IO (Ptr a)
+mallocStringGC size = do
+  printfStr $ printf "mallocString: %d\n" size
   mallocBytesGC size
 
-mallocString :: Int -> IO (Ptr a)
-mallocString size = do
-  printfStr "mallocString: %d\n" size
-  mallocBytesGC size
-
-foreign export ccall mallocObject :: Int -> IO CPtrdiff
-mallocObject :: Int -> IO CPtrdiff
-mallocObject size = do
+foreign export ccall mallocObjectGC :: Int -> IO CPtrdiff
+mallocObjectGC :: Int -> IO CPtrdiff
+mallocObjectGC size = do
   ptr <- mallocBytesGC size
-  printfStr "mallocObject: %d\n" 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
 
--- TODO: delete me
-foreign export ccall demoInterfaceCall :: CUInt -> IO ()
-demoInterfaceCall :: CUInt -> IO ()
-demoInterfaceCall val = do
-  printf "demoInterfaceCall: 0x%08x\n" (fromIntegral val :: Word32)
-  return ()
+mallocStringUnmanaged :: Int -> IO (Ptr a)
+mallocStringUnmanaged size = do
+  printfStr $ printf "mallocStringUnamaged: %d\n" size
+  mallocBytes size
+
 
 getHeapMemory :: IO Int
-getHeapMemory = getHeapSize
+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"