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