summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-10-28 23:58:22 -0400
committerJoe Crayne <joe@jerkface.net>2018-10-28 23:58:22 -0400
commit062db545636881f694e6c0c1eaef1eb973da1b0d (patch)
tree4876e6dc9bdf89cf165113377198dded312810ac
parent9b02922914a90c7a0b5751ca5d4293e00caf366b (diff)
Send empty bytestrings as null pointer.
-rw-r--r--haskell/Data/VCDIFF.hs15
1 files changed, 11 insertions, 4 deletions
diff --git a/haskell/Data/VCDIFF.hs b/haskell/Data/VCDIFF.hs
index c48eb1a..6b95ede 100644
--- a/haskell/Data/VCDIFF.hs
+++ b/haskell/Data/VCDIFF.hs
@@ -47,7 +47,7 @@ import Data.Text.Encoding
47import Data.Word 47import Data.Word
48import Foreign.C.Types 48import Foreign.C.Types
49import Foreign.C.String 49import Foreign.C.String
50import Foreign.ForeignPtr (withForeignPtr) 50import Foreign.ForeignPtr (withForeignPtr,touchForeignPtr)
51import Foreign.Ptr 51import Foreign.Ptr
52import Foreign.Concurrent 52import Foreign.Concurrent
53import Foreign.Storable 53import Foreign.Storable
@@ -67,6 +67,9 @@ data Stream m = Stream
67 , streamSource :: MutVar (PrimState m) (Maybe (Source m)) 67 , streamSource :: MutVar (PrimState m) (Maybe (Source m))
68 } 68 }
69 69
70keepAlive srcvar s = do
71 seq srcvar $ seq s $ return () -- Keep array s alive until the ffi functions finish.
72{-# NOINLINE keepAlive #-}
70 73
71-- The xd3_config structure is used to initialize a stream - all data 74-- The xd3_config structure is used to initialize a stream - all data
72-- is copied into stream so config may be a temporary variable. See 75-- is copied into stream so config may be a temporary variable. See
@@ -84,7 +87,7 @@ config_stream cfg = do
84 xd3_abort_stream sptr 87 xd3_abort_stream sptr
85 xd3_close_stream sptr 88 xd3_close_stream sptr
86 xd3_free_stream sptr 89 xd3_free_stream sptr
87 seq srcvar $ seq s $ return () -- Keep array s alive until the ffi functions finish. 90 keepAlive srcvar s
88 fp <- newForeignPtr sptr finalize 91 fp <- newForeignPtr sptr finalize
89 return Stream 92 return Stream
90 { streamArray = s 93 { streamArray = s
@@ -149,8 +152,12 @@ pokeCurrentBlock stream blk = do
149withByteString :: PrimBase m => B.ByteString -> (Ptr b -> Usize_t -> m a) -> m a 152withByteString :: PrimBase m => B.ByteString -> (Ptr b -> Usize_t -> m a) -> m a
150withByteString d act = 153withByteString d act =
151 let (fp,off,len) = B.toForeignPtr d 154 let (fp,off,len) = B.toForeignPtr d
152 in unsafeIOToPrim $ withForeignPtr fp $ \ptr -> unsafePrimToIO $ do 155 in do ptr <- unsafeIOToPrim $ withForeignPtr fp $ return
153 act (ptr `plusPtr` off) (fromIntegral len) 156 a <- case fromIntegral len of
157 0 -> act nullPtr 0
158 l -> act (ptr `plusPtr` off) l
159 unsafeIOToPrim $ touchForeignPtr fp
160 return a
154 161
155xdelta :: (PrimBase m, Monoid u) => XDeltaMethods m u -> (Stream m -> m ErrorCode) -> [B.ByteString] -> m u 162xdelta :: (PrimBase m, Monoid u) => XDeltaMethods m u -> (Stream m -> m ErrorCode) -> [B.ByteString] -> m u
156xdelta x xxcode_input ds = do 163xdelta x xxcode_input ds = do