From: Bernhard Urban Date: Thu, 26 Apr 2012 15:11:08 +0000 (+0200) Subject: strings: other solution X-Git-Url: http://wien.tomnetworks.com/gitweb/?p=mate.git;a=commitdiff_plain;h=571767aab9622f896c38fb82d1820cfe4ae04dea strings: other solution strings got free'd by the GHC runtime, ooops --- diff --git a/Mate/Strings.hs b/Mate/Strings.hs index 2867bad..22567a8 100644 --- a/Mate/Strings.hs +++ b/Mate/Strings.hs @@ -7,11 +7,11 @@ module Mate.Strings ( import Data.Word import qualified Data.Map as M import qualified Data.ByteString.Lazy as B -import qualified Data.ByteString.Internal as BI import Text.Printf import Foreign.Ptr -import Foreign.ForeignPtr +import Foreign.Marshal.Alloc +import Foreign.Marshal.Utils import Foreign.Marshal.Array import Mate.Types @@ -28,14 +28,14 @@ getUniqueStringAddr str = do return addr Just addr -> return addr --- TOOD(bernhard): quite hackish allocateJavaString :: B.ByteString -> IO Word32 allocateJavaString str = do - let strlen = fromIntegral $ B.length str - let str_unpacked = (map fromIntegral $ B.unpack str) :: [Word8] - arr <- newArray str_unpacked - newstr <- BI.create strlen (\x -> BI.memcpy x arr (fromIntegral strlen)) - let (newstrptr, _, _) = BI.toForeignPtr newstr - let w32_ptr = fromIntegral $ ptrToIntPtr $ unsafeForeignPtrToPtr newstrptr - printf "new str ptr: 0x%08x\n" w32_ptr + -- TODO(bernhard): is this also true for UTF8 stuff? + -- (+1) for \0 + let strlen = (+1) $ fromIntegral $ B.length str + arr <- newArray $ ((map fromIntegral $ B.unpack str) :: [Word8]) + newstr <- mallocBytes strlen + copyBytes newstr arr strlen + let w32_ptr = fromIntegral $ ptrToIntPtr newstr + printf "new str ptr: 0x%08x (%s)@%d\n" w32_ptr (toString str) strlen return w32_ptr