@@ -362,17 +362,19 @@ data MutArray a =
362-- overhead is a copy if the input array is unpinned.
362-- overhead is a copy if the input array is unpinned.
363{-# INLINE pin #-}
363{-# INLINE pin #-}
364pin :: MutArray a -> IO (MutArray a)
364pin :: MutArray a -> IO (MutArray a)
365−pin arr@MutArray{..} = do
365+pin arr@MutArray{..} =
366− contents <- Unboxed.pin arrContents
366+ if Unboxed.isPinned arrContents
367− return $ arr {arrContents = contents}
367+ then pure arr
368+ else pinnedClone arr
369-- | Copy the 'MutArray' to unpinned memory if pinned, else do nothing. The
370-- | Copy the 'MutArray' to unpinned memory if pinned, else do nothing. The
370-- overhead is a copy if the input array is pinned.
371-- overhead is a copy if the input array is pinned.
371{-# INLINE unpin #-}
372{-# INLINE unpin #-}
372unpin :: MutArray a -> IO (MutArray a)
373unpin :: MutArray a -> IO (MutArray a)
373−unpin arr@MutArray{..} = do
374+unpin arr@MutArray{..} =
374− contents <- Unboxed.unpin arrContents
375+ if Unboxed.isPinned arrContents
375− return $ arr {arrContents = contents}
376+ then clone arr
377{-# INLINE isPinned #-}
379{-# INLINE isPinned #-}
378isPinned :: MutArray a -> Bool
380isPinned :: MutArray a -> Bool