@@ -438,10 +438,10 @@ import Prelude hiding
438438-------------------------------------------------------------------------------
439439
440440-- NOTE: Have to be "ccall unsafe" so that we can pass unpinned memory to these
441- -- NOTE: cannot pass Ptr, have to pass MutableByteArray# for safe unpinned
442- -- memory access in C code .
441+ -- NOTE: for passing unpinned memory safely we have to pass unlifted pointers
442+ -- in FFI so that GC cannot change the wrapper during the call setup .
443443foreign import ccall unsafe " string.h memcpy" c_memcpy
444- :: MutableByteArray # RealWorld -> Ptr Word8 -> CSize -> IO (Ptr Word8 )
444+ :: MutableByteArray # RealWorld -> Addr # -> CSize -> IO (Ptr Word8 )
445445
446446-- XXX We can pass an Addr# to memchr instead of writing a C wrapper.
447447foreign import ccall unsafe " memchr_index" c_memchr_index
@@ -2592,13 +2592,13 @@ fromPureStream xs =
25922592--
25932593-- /Unsafe:/
25942594--
2595- -- 1. The memory pointed by @addr@ must be pinned or static .
2595+ -- 1. The caller has to make sure the pointer is live during the call .
25962596-- 2. The caller is responsible to ensure that the pointer passed is valid up
25972597-- to the given length.
25982598--
25992599{-# INLINABLE fromPtrN #-}
26002600fromPtrN :: MonadIO m => Int -> Ptr Word8 -> m (MutArray Word8 )
2601- fromPtrN len addr = do
2601+ fromPtrN len ( Ptr addr) = do
26022602 -- memcpy is better than stream copy when the size is known.
26032603 -- XXX We can implement a stream copy in a similar way by streaming Word64
26042604 -- first and then remaining Word8.
@@ -2607,12 +2607,12 @@ fromPtrN len addr = do
26072607 _ <- liftIO $ c_memcpy mbarr addr (fromIntegral len)
26082608 pure (arr { arrEnd = len })
26092609
2610- -- | @fromW16CString # addr@ copies a C string consisting of bytes and
2610+ -- | @fromCString # addr@ copies a C string consisting of bytes and
26112611-- terminated by a null byte, into a Word8 array. The null byte is not copied.
26122612--
26132613-- /Unsafe:/
26142614--
2615- -- 1. The memory pointed by @addr@ must be pinned or static .
2615+ -- 1. The caller has to make sure that the @addr@ is alive during the call .
26162616-- 2. The caller is responsible to ensure that the pointer passed is valid up
26172617-- to the point where null byte is found.
26182618--
@@ -2641,7 +2641,7 @@ fromByteStr# = fromCString#
26412641--
26422642-- /Unsafe:/
26432643--
2644- -- 1. The memory pointed by @addr@ must be pinned or static .
2644+ -- 1. The caller has to make sure that the @addr@ is alive during the call .
26452645-- 2. The caller is responsible to ensure that the pointer passed is valid up
26462646-- to the point where null Word16 is found.
26472647--
@@ -2867,16 +2867,18 @@ unsafeSplice dst src = do
28672867 (arrContents src) startSrc (arrContents dst) endDst srcLen
28682868 return $ dst {arrEnd = endDst + srcLen}
28692869
2870- -- |
2870+ -- | Append specified number of bytes from a given pointer to the MutArray.
2871+ --
28712872-- /Unsafe:/
28722873--
28732874-- 1. Does not check the length of the MutArray.
2874- -- 2. The caller is responsible to ensure that the pointer passed is valid up
2875+ -- 2. The caller has to make sure that the pointer is alive during the call
2876+ -- 3. The caller is responsible to ensure that the pointer passed is valid up
28752877-- to the given length.
28762878--
28772879{-# INLINE unsafeAppendPtrN #-}
28782880unsafeAppendPtrN :: MonadIO m =>
2879- MutArray a -> Ptr a -> Int -> m (MutArray a )
2881+ MutArray Word8 -> Ptr Word8 -> Int -> m (MutArray Word8 )
28802882unsafeAppendPtrN dst ptr ptrLen = do
28812883 let newEnd = arrEnd dst + ptrLen
28822884 assertM(newEnd <= arrBound dst)
@@ -3094,6 +3096,7 @@ breakOn sep arr@MutArray{..} = liftIO $ do
30943096 -- Need efficient stream based primitives that work on Word64.
30953097 let marr = getMutByteArray# arrContents
30963098 len = fromIntegral (arrEnd - arrStart)
3099+ -- XXX pass Addr# to memchr instead of using a C wrapper over memchr
30973100 sepIndex <- c_memchr_index marr (fromIntegral arrStart) sep len
30983101 let intIndex = fromIntegral sepIndex
30993102 return $
0 commit comments