refactor: reduce global var in trap.c to one pointer
[mate.git] / Mate / Strings.hs
index 435f6ec08fda6861c81e2183596f940f52373e73..794af0ff3eb8c68c668532a506e944328379e395 100644 (file)
@@ -1,5 +1,6 @@
+{-# LANGUAGE CPP #-}
 {-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE ForeignFunctionInterface #-}
+#include "debug.h"
 module Mate.Strings (
   getUniqueStringAddr
   ) where
@@ -8,24 +9,26 @@ import Data.Word
 import qualified Data.Map as M
 import qualified Data.ByteString.Lazy as B
 import qualified Data.ByteString.Internal as BI
+#ifdef DEBUG
 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
 getUniqueStringAddr str = do
-  smap <- get_stringsmap >>= ptr2stringsmap
+  smap <- getStringMap
   case M.lookup str smap of
     Nothing -> do
       addr <- allocateJavaString str
-      let smap' = M.insert str addr smap
-      stringsmap2ptr smap' >>= set_stringsmap
+      setStringMap $ M.insert str addr smap
       return addr
     Just addr -> return addr
 
@@ -33,11 +36,11 @@ allocateJavaString :: B.ByteString -> IO Word32
 allocateJavaString str = do
   -- TODO(bernhard): is this also true for UTF8 stuff?
   let strlen = fromIntegral $ B.length str
-  arr <- newArray ((map fromIntegral $ B.unpack str) :: [Word8])
+  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
-  printf "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen
+  printfStr "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen
   return w32_ptr