codegen: handle exceptions of a method
[mate.git] / Mate / GarbageAlloc.hs
1 {-# LANGUAGE ForeignFunctionInterface #-}
2 module Mate.GarbageAlloc(
3     mallocClassData,
4     mallocStringGC,
5     mallocObjectGC,
6     getHeapMemory,
7     printMemoryUsage,
8     printGCStats,
9     mallocObjectUnmanaged,
10     mallocStringUnmanaged)  where
11
12 import Foreign
13 import Foreign.C
14
15 import Mate.GC.Boehm
16
17 import Mate.Debug
18
19 -- unified place for allocating Memory
20 -- TODO: implement GC stuff ;-)
21
22 mallocClassData :: Int -> IO (Ptr a)
23 mallocClassData size = do
24   printfStr $ printf "mallocClassData: %d\n" size
25   mem <- mallocBytes size
26   addRootGC mem (plusPtr mem size)
27   return mem
28
29 mallocStringGC :: Int -> IO (Ptr a)
30 mallocStringGC size = do
31   printfStr $ printf "mallocString: %d\n" size
32   mallocBytesGC size
33
34 foreign export ccall mallocObjectGC :: Int -> IO CPtrdiff
35 mallocObjectGC :: Int -> IO CPtrdiff
36 mallocObjectGC size = do
37   ptr <- mallocBytesGC size
38   printfStr $ printf "mallocObject: %d\n" size
39   return $ fromIntegral $ ptrToIntPtr ptr
40
41 mallocObjectUnmanaged :: Int -> IO CPtrdiff
42 mallocObjectUnmanaged size = do
43   ptr <- mallocBytes size
44   printfStr $ printf "mallocObjectUnmanged: %d\n" size
45   return $ fromIntegral $ ptrToIntPtr ptr
46
47 mallocStringUnmanaged :: Int -> IO (Ptr a)
48 mallocStringUnmanaged size = do
49   printfStr $ printf "mallocStringUnamaged: %d\n" size
50   mallocBytes size
51
52
53 getHeapMemory :: IO Int
54 getHeapMemory = getHeapSizeGC
55
56 foreign export ccall printMemoryUsage :: IO ()
57 printMemoryUsage :: IO ()
58 printMemoryUsage = getHeapMemory >>= print
59
60 foreign export ccall printGCStats :: IO ()
61 printGCStats :: IO ()
62 printGCStats = putStrLn "Should print GC Stats"