1 {-# LANGUAGE ForeignFunctionInterface #-}
13 import Foreign.Marshal.Alloc
16 import System.IO.Unsafe
17 import Foreign.C.Types
20 foreign import ccall "gc/gc.h GC_malloc"
21 mallocGC :: CSize -> IO (Ptr a)
23 foreign import ccall "gc/gc.h GC_get_heap_size"
24 heapSizeGC :: IO CSize
27 foreign import ccall "gc/gc.h GC_init"
31 foreign import ccall "gc/gc.h GC_add_roots"
32 addRootGCInternal :: Ptr a -> Ptr a -> IO ()
34 -- counts allocated memory in heap
35 {-# NOINLINE globalBytesAllocated #-}
36 globalBytesAllocated :: IORef Int
37 globalBytesAllocated = unsafePerformIO (newIORef 0)
40 -- |Allocates size bytes in managed memory
41 mallocBytesGC :: Int -> IO (Ptr a)
42 mallocBytesGC size = do
43 --print "trying alloc"
44 ptr <- modifyIORef globalBytesAllocated (+ size) >> mallocGC (fromIntegral size)
50 -- |Allocates size bytes in managed memory
51 mallocBytesGC :: Int -> IO (Ptr a)
52 mallocBytesGC = mallocGC . fromIntegral
55 -- |Explicitely deallocate an object. Not required and dangerous.
56 unsafeFreeGC :: Ptr a -> IO ()
57 unsafeFreeGC _ = print "not implemented"
59 -- |Returns currently allocated memory in bytes
61 getHeapSize = liftM fromIntegral heapSizeGC
63 addRootGC :: Ptr a -> Ptr a -> IO ()
64 addRootGC = addRootGCInternal