imported some gc functions - refactoring,cleanup required ;-)
build-depends: base
exposed-modules: Mate.GC.Boehm
hs-source-dirs: src
build-depends: base
exposed-modules: Mate.GC.Boehm
hs-source-dirs: src
+ extra-lib-dirs: /home/hs/Mate/boehmgc/gc/lib/
+ extra-libraries: gc
+ include-dirs: /home/hs/Mate/boehmgc/gc/include
+ ld-options: -lgcblub
+{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
module Mate.GC.Boehm (
mallocBytesGC,
unsafeFreeGC,
getHeapSize,
{-# LANGUAGE CPP #-}
module Mate.GC.Boehm (
mallocBytesGC,
unsafeFreeGC,
getHeapSize,
import Foreign.Ptr
import Data.IORef
import System.IO.Unsafe
import Foreign.Ptr
import Data.IORef
import System.IO.Unsafe
+import Foreign.C.Types
+import Control.Monad
+
+foreign import ccall "gc/gc.h GC_malloc"
+ mallocGC :: CSize -> IO (Ptr a)
+
+foreign import ccall "gc/gc.h GC_get_heap_size"
+ heapSizeGC :: IO CSize
+
+
+foreign import ccall "gc/gc.h GC_init"
+ initGC :: IO ()
+
+
+foreign import ccall "gc/gc.h GC_add_roots"
+ addRootGCInternal :: Ptr a -> Ptr a -> IO ()
-- counts allocated memory in heap
{-# NOINLINE globalBytesAllocated #-}
-- counts allocated memory in heap
{-# NOINLINE globalBytesAllocated #-}
#ifdef DEBUG
-- |Allocates size bytes in managed memory
mallocBytesGC :: Int -> IO (Ptr a)
#ifdef DEBUG
-- |Allocates size bytes in managed memory
mallocBytesGC :: Int -> IO (Ptr a)
-mallocBytesGC size = modifyIORef globalBytesAllocated (+ size) >> mallocBytes size
+mallocBytesGC size = do
+ --print "trying alloc"
+ ptr <- modifyIORef globalBytesAllocated (+ size) >> mallocGC (fromIntegral size)
+ if ptr == nullPtr
+ then error "alloc 0"
+ else return ptr
+
#else
-- |Allocates size bytes in managed memory
mallocBytesGC :: Int -> IO (Ptr a)
#else
-- |Allocates size bytes in managed memory
mallocBytesGC :: Int -> IO (Ptr a)
-mallocBytesGC = mallocBytes
+mallocBytesGC = mallocGC . fromIntegral
#endif
-- |Explicitely deallocate an object. Not required and dangerous.
#endif
-- |Explicitely deallocate an object. Not required and dangerous.
-- |Returns currently allocated memory in bytes
getHeapSize :: IO Int
-- |Returns currently allocated memory in bytes
getHeapSize :: IO Int
-getHeapSize = readIORef globalBytesAllocated
+getHeapSize = liftM fromIntegral heapSizeGC
+
+addRootGC :: Ptr a -> Ptr a -> IO ()
+addRootGC = addRootGCInternal