diff options
author | Joe Crayne <joe@jerkface.net> | 2018-10-28 23:58:22 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-10-28 23:58:22 -0400 |
commit | 062db545636881f694e6c0c1eaef1eb973da1b0d (patch) | |
tree | 4876e6dc9bdf89cf165113377198dded312810ac | |
parent | 9b02922914a90c7a0b5751ca5d4293e00caf366b (diff) |
Send empty bytestrings as null pointer.
-rw-r--r-- | haskell/Data/VCDIFF.hs | 15 |
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 | |||
47 | import Data.Word | 47 | import Data.Word |
48 | import Foreign.C.Types | 48 | import Foreign.C.Types |
49 | import Foreign.C.String | 49 | import Foreign.C.String |
50 | import Foreign.ForeignPtr (withForeignPtr) | 50 | import Foreign.ForeignPtr (withForeignPtr,touchForeignPtr) |
51 | import Foreign.Ptr | 51 | import Foreign.Ptr |
52 | import Foreign.Concurrent | 52 | import Foreign.Concurrent |
53 | import Foreign.Storable | 53 | import 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 | ||
70 | keepAlive 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 | |||
149 | withByteString :: PrimBase m => B.ByteString -> (Ptr b -> Usize_t -> m a) -> m a | 152 | withByteString :: PrimBase m => B.ByteString -> (Ptr b -> Usize_t -> m a) -> m a |
150 | withByteString d act = | 153 | withByteString 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 | ||
155 | xdelta :: (PrimBase m, Monoid u) => XDeltaMethods m u -> (Stream m -> m ErrorCode) -> [B.ByteString] -> m u | 162 | xdelta :: (PrimBase m, Monoid u) => XDeltaMethods m u -> (Stream m -> m ErrorCode) -> [B.ByteString] -> m u |
156 | xdelta x xxcode_input ds = do | 163 | xdelta x xxcode_input ds = do |