gc: factor out allocation
[mate.git] / Mate / Strings.hs
index ba2166364aca3c3537ac119655a34924b708eb15..6e541814e7645247ee0f2a638d61fe7cd4a31aab 100644 (file)
@@ -1,6 +1,7 @@
 {-# LANGUAGE CPP #-}
 {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE ForeignFunctionInterface #-}
+#include "debug.h"
 module Mate.Strings (
   getUniqueStringAddr
   ) where
@@ -14,11 +15,12 @@ import Text.Printf
 #endif
 
 import Foreign.Ptr
-import Foreign.Marshal.Alloc
 import Foreign.Marshal.Utils
 import Foreign.Marshal.Array
 
 import Mate.Types
+import Mate.Debug
+import Mate.GarbageAlloc
 
 
 getUniqueStringAddr :: B.ByteString -> IO Word32
@@ -38,11 +40,9 @@ allocateJavaString str = do
   let strlen = fromIntegral $ B.length str
   arr <- newArray $ ((map fromIntegral $ B.unpack str) :: [Word8])
   -- (+1) for \0
-  newstr <- mallocBytes (strlen + 1)
+  newstr <- mallocString (strlen + 1)
   BI.memset newstr 0 (fromIntegral $ strlen + 1)
   copyBytes newstr arr strlen
   let w32_ptr = fromIntegral $ ptrToIntPtr newstr
-#ifdef DEBUG
-  printf "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen
-#endif
+  printf_str "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen
   return w32_ptr