Skip to content

Commit

Permalink
Use CPP macros instead of the legacy pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Jan 29, 2024
1 parent 7767484 commit ca83e05
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Streamly/External/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import GHC.Exts
, plusAddr#
, unsafeCoerce#
)
import GHC.ForeignPtr (ForeignPtr(..), ForeignPtrContents(..), plusForeignPtr)
import GHC.ForeignPtr (ForeignPtr(..), ForeignPtrContents(..))
import GHC.Int (Int(..))
import GHC.Ptr (Ptr(..), nullPtr, plusPtr)
import Streamly.Data.Fold (Fold)
Expand All @@ -42,6 +42,10 @@ import Streamly.Internal.System.IO (unsafeInlineIO)
import qualified Streamly.Data.Array as Array
import qualified Streamly.Internal.Data.Unfold as Unfold (fold, mkUnfoldrM)

#if !(MIN_VERSION_bytestring(0,11,0))
import GHC.ForeignPtr (plusForeignPtr)
#endif

#if MIN_VERSION_streamly_core(0,2,0)
import Streamly.Internal.Data.Array (Array(..))
import Streamly.Internal.Data.MutByteArray (MutByteArray(..))
Expand All @@ -62,6 +66,14 @@ import Prelude hiding (read)
#define MUT_BYTE_ARRAY MutableByteArray
#endif

#if MIN_VERSION_bytestring(0,11,0)
#define CONSTRUCTOR(a, b, c) BS a c
#define WHEN_0_10_12(x)
#else
#define CONSTRUCTOR(a, b, c) PS a b c
#define WHEN_0_10_12(x) x
#endif

{-# INLINE mutableByteArrayContents# #-}
mutableByteArrayContents# :: MutableByteArray# RealWorld -> Addr#
mutableByteArrayContents# marr# = byteArrayContents# (unsafeCoerce# marr#)
Expand All @@ -79,14 +91,15 @@ makeForeignPtr (MUT_BYTE_ARRAY marr#) (I# off#) =
-- there is a copy involved.
{-# INLINE toArray #-}
toArray :: ByteString -> Array Word8
toArray (PS (ForeignPtr addr# _) _ _)
toArray (CONSTRUCTOR((ForeignPtr addr# _), _, _))
| Ptr addr# == nullPtr = Array MutBA.nil 0 0

Check failure on line 95 in src/Streamly/External/ByteString.hs

View workflow job for this annotation

GitHub Actions / 9.6.2+Werror

In the use of ‘nil’

Check warning on line 95 in src/Streamly/External/ByteString.hs

View workflow job for this annotation

GitHub Actions / hlint

In the use of ‘nil’

Check warning on line 95 in src/Streamly/External/ByteString.hs

View workflow job for this annotation

GitHub Actions / hlint

In the use of ‘nil’
toArray (PS (ForeignPtr addr# (PlainPtr marr#)) off0 len) =
let off = I# (addr# `minusAddr#` mutableByteArrayContents# marr#) + off0
toArray (CONSTRUCTOR((ForeignPtr addr# (PlainPtr marr#)), off0, len)) =
let off = I# (addr# `minusAddr#` mutableByteArrayContents# marr#)
WHEN_0_10_12(+ off0)
in Array (MUT_BYTE_ARRAY marr#) off (off + len)
toArray (PS fptr off len) =
toArray (CONSTRUCTOR(fptr, off, len)) =
unsafeInlineIO
$ withForeignPtr (fptr `plusForeignPtr` off)
$ withForeignPtr (fptr WHEN_0_10_12(`plusForeignPtr` off))
$ Unfold.fold (Array.writeN len) generator

where
Expand All @@ -102,7 +115,7 @@ toArray (PS fptr off len) =
fromArray :: Array Word8 -> ByteString
fromArray (Array {..})
| aLen == 0 = mempty
| otherwise = PS (makeForeignPtr arrContents arrStart) 0 aLen
| otherwise = CONSTRUCTOR((makeForeignPtr arrContents arrStart), 0, aLen)

where

Expand Down

0 comments on commit ca83e05

Please sign in to comment.