@@ -4,7 +4,7 @@
44{-# LANGUAGE ExistentialQuantification #-}
55
66-- |
7−-- Module : Streamly.RingBuffer
7+-- Module : Streamly.Mem.Ring
88-- Copyright : (c) 2019 Composewell Technologies
99-- License : BSD3
1010-- Maintainer : harendra.kumar@gmail.com
@@ -12,8 +12,8 @@
1212-- Portability : GHC
1313--
1414
15−module Streamly.RingBuffer
16− ( RingBuffer(..)
15+module Streamly.Mem.Ring
16+ ( Ring(..)
1717
1818 -- * Construction
1919 , new
@@ -40,7 +40,7 @@ import GHC.ForeignPtr (mallocPlainForeignPtrAlignedBytes)
4040import GHC.Ptr (Ptr(..))
4141import Prelude hiding (length, concat)
4242
43−import qualified Streamly.Array.Types as A
43+import qualified Streamly.Mem.Array.Types as A
4444
4545-- | A ring buffer is a mutable array of fixed size. Initially the array is
4646-- empty, with ringStart pointing at the start of allocated memory. We call the
@@ -54,7 +54,7 @@ import qualified Streamly.Array.Types as A
5454-- When using it we should keep in mind that a ringBuffer is a mutable data
5555-- structure. We should not leak out references to it for immutable use.
5656--
57−data RingBuffer a = RingBuffer
57+data Ring a = Ring
5858 { ringStart :: !(ForeignPtr a) -- first address
5959 , ringBound :: !(Ptr a) -- first address beyond allocated memory
6060 }
@@ -62,12 +62,12 @@ data RingBuffer a = RingBuffer
6262-- | Create a new ringbuffer and return the ring buffer and the ringHead.
6363-- Returns the ring and the ringHead, the ringHead is same as ringStart.
6464{-# INLINE new #-}
65−new :: forall a. Storable a => Int -> IO (RingBuffer a, Ptr a)
65+new :: forall a. Storable a => Int -> IO (Ring a, Ptr a)
6666new count = do
6767 let size = count * sizeOf (undefined :: a)
6868 fptr <- mallocPlainForeignPtrAlignedBytes size (alignment (undefined :: a))
6969 let p = unsafeForeignPtrToPtr fptr
70− return $ (RingBuffer
70+ return $ (Ring
7171 { ringStart = fptr
7272 , ringBound = p `plusPtr` size
7373 }, p)
@@ -75,8 +75,8 @@ new count = do
7575-- | Advance the ringHead by 1 item, wrap around if we hit the end of the
7676-- array.
7777{-# INLINE advance #-}
78−advance :: forall a. Storable a => RingBuffer a -> Ptr a -> Ptr a
79−advance RingBuffer{..} ringHead =
78+advance :: forall a. Storable a => Ring a -> Ptr a -> Ptr a
79+advance Ring{..} ringHead =
8080 let ptr = ringHead `plusPtr` sizeOf (undefined :: a)
8181 in if ptr < ringBound
8282 then ptr
@@ -84,10 +84,10 @@ advance RingBuffer{..} ringHead =
8484
8585-- | Insert an item at the head of the ring, when the ring is full this
8686-- replaces the oldest item in the ring with the new item. This is unsafe
87−-- beause ringHead supplied is not verified to be within the RingBuffer. Also,
87+-- beause ringHead supplied is not verified to be within the Ring. Also,
8888-- the ringStart foreignPtr must be guaranteed to be alive by the caller.
8989{-# INLINE unsafeInsert #-}
90−unsafeInsert :: Storable a => RingBuffer a -> Ptr a -> a -> IO (Ptr a)
90+unsafeInsert :: Storable a => Ring a -> Ptr a -> a -> IO (Ptr a)
9191unsafeInsert rb ringHead newVal = do
9292 poke ringHead newVal
9393 -- touchForeignPtr (ringStart rb)
@@ -99,8 +99,8 @@ unsafeInsert rb ringHead newVal = do
9999-- the ring buffer. This is unsafe because the ringHead Ptr is not checked to
100100-- be in range.
101101{-# INLINE unsafeEqArrayN #-}
102−unsafeEqArrayN :: RingBuffer a -> Ptr a -> A.Array a -> Int -> Bool
103−unsafeEqArrayN RingBuffer{..} rh A.Array{..} n =
102+unsafeEqArrayN :: Ring a -> Ptr a -> A.Array a -> Int -> Bool
103+unsafeEqArrayN Ring{..} rh A.Array{..} n =
104104 let !res = A.unsafeInlineIO $ do
105105 let rs = unsafeForeignPtrToPtr ringStart
106106 let as = unsafeForeignPtrToPtr aStart
@@ -125,8 +125,8 @@ unsafeEqArrayN RingBuffer{..} rh A.Array{..} n =
125125-- supplied array must be equal to or bigger than the ringBuffer, ARRAY BOUNDS
126126-- ARE NOT CHECKED.
127127{-# INLINE unsafeEqArray #-}
128−unsafeEqArray :: RingBuffer a -> Ptr a -> A.Array a -> Bool
129−unsafeEqArray RingBuffer{..} rh A.Array{..} =
128+unsafeEqArray :: Ring a -> Ptr a -> A.Array a -> Bool
129+unsafeEqArray Ring{..} rh A.Array{..} =
130130 let !res = A.unsafeInlineIO $ do
131131 let rs = unsafeForeignPtrToPtr ringStart
132132 let as = unsafeForeignPtrToPtr aStart
@@ -151,8 +151,8 @@ unsafeEqArray RingBuffer{..} rh A.Array{..} =
151151-- Unsafe because the supplied Ptr is not checked to be in range.
152152{-# INLINE unsafeFoldRing #-}
153153unsafeFoldRing :: forall a b. Storable a
154− => Ptr a -> (b -> a -> b) -> b -> RingBuffer a -> b
155−unsafeFoldRing ptr f z RingBuffer{..} =
154+ => Ptr a -> (b -> a -> b) -> b -> Ring a -> b
155+unsafeFoldRing ptr f z Ring{..} =
156156 let !res = A.unsafeInlineIO $ withForeignPtr ringStart $ \p ->
157157 go z p ptr
158158 in res
@@ -166,8 +166,8 @@ unsafeFoldRing ptr f z RingBuffer{..} =
166166-- | Like unsafeFoldRing but with a monadic step function.
167167{-# INLINE unsafeFoldRingM #-}
168168unsafeFoldRingM :: forall m a b. (Monad m, Storable a)
169− => Ptr a -> (b -> a -> m b) -> b -> RingBuffer a -> m b
170−unsafeFoldRingM ptr f z RingBuffer{..} = go z (unsafeForeignPtrToPtr ringStart) ptr
169+ => Ptr a -> (b -> a -> m b) -> b -> Ring a -> m b
170+unsafeFoldRingM ptr f z Ring{..} = go z (unsafeForeignPtrToPtr ringStart) ptr
171171 where
172172 go !acc !start !end
173173 | start == end = return acc
@@ -182,8 +182,8 @@ unsafeFoldRingM ptr f z RingBuffer{..} = go z (unsafeForeignPtrToPtr ringStart)
182182-- the ring.
183183{-# INLINE unsafeFoldRingFullM #-}
184184unsafeFoldRingFullM :: forall m a b. (Monad m, Storable a)
185− => Ptr a -> (b -> a -> m b) -> b -> RingBuffer a -> m b
186−unsafeFoldRingFullM rh f z rb@RingBuffer{..} = go z rh
185+ => Ptr a -> (b -> a -> m b) -> b -> Ring a -> m b
186+unsafeFoldRingFullM rh f z rb@Ring{..} = go z rh
187187 where
188188 go !acc !start = do
189189 let !x = A.unsafeInlineIO $ peek start