implemented dummy mallocBytes etc which; added cabal build system;
[hs-boehmgc.git] / src / Mate / GC / Boehm.hs
1 {-# LANGUAGE CPP #-}
2 module Mate.GC.Boehm (
3      mallocBytesGC,
4      unsafeFreeGC,
5      getHeapSize,
6   ) where
7
8 #define DEBUG
9
10 import Foreign.Marshal.Alloc
11 import Foreign.Ptr
12 import Data.IORef
13 import System.IO.Unsafe
14
15 -- counts allocated memory in heap
16 {-# NOINLINE globalBytesAllocated #-}
17 globalBytesAllocated :: IORef Int
18 globalBytesAllocated = unsafePerformIO (newIORef 0)
19
20 #ifdef DEBUG
21 -- |Allocates size bytes in managed memory
22 mallocBytesGC :: Int -> IO (Ptr a)
23 mallocBytesGC size = modifyIORef globalBytesAllocated (+ size) >> mallocBytes size
24 #else
25 -- |Allocates size bytes in managed memory
26 mallocBytesGC :: Int -> IO (Ptr a)
27 mallocBytesGC = mallocBytes
28 #endif
29
30 -- |Explicitely deallocate an object. Not required and dangerous.
31 unsafeFreeGC :: Ptr a -> IO ()
32 unsafeFreeGC _ = print "not implemented"
33
34 -- |Returns currently allocated memory in bytes
35 getHeapSize :: IO Int
36 getHeapSize = readIORef globalBytesAllocated