1 {-# LANGUAGE OverloadedStrings #-}
2 {-# LANGUAGE ForeignFunctionInterface #-}
8 import qualified Data.Map as M
9 import qualified Data.ByteString.Lazy as B
13 import Foreign.Marshal.Alloc
14 import Foreign.Marshal.Utils
15 import Foreign.Marshal.Array
20 getUniqueStringAddr :: B.ByteString -> IO Word32
21 getUniqueStringAddr str = do
22 smap <- get_stringsmap >>= ptr2stringsmap
23 case M.lookup str smap of
25 addr <- allocateJavaString str
26 let smap' = M.insert str addr smap
27 stringsmap2ptr smap' >>= set_stringsmap
29 Just addr -> return addr
31 allocateJavaString :: B.ByteString -> IO Word32
32 allocateJavaString str = do
33 -- TODO(bernhard): is this also true for UTF8 stuff?
35 let strlen = (+1) $ fromIntegral $ B.length str
36 arr <- newArray $ ((map fromIntegral $ B.unpack str) :: [Word8])
37 newstr <- mallocBytes strlen
38 copyBytes newstr arr strlen
39 let w32_ptr = fromIntegral $ ptrToIntPtr newstr
40 printf "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen