{-# LANGUAGE MagicHash, UnboxedTuples #-}

module Agda.Utils.CompactRegion where

import GHC.Exts
import GHC.Types
import GHC.Word
import Control.DeepSeq

data Compact = Compact Compact#

instance NFData Compact where
  rnf :: Compact -> ()
rnf !Compact
x = ()

-- | Create a new compact region with given initial block size.
new :: Word -> IO Compact
new :: Word -> IO Compact
new (W# Word#
size) = (State# RealWorld -> (# State# RealWorld, Compact #)) -> IO Compact
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO \State# RealWorld
s -> case Word# -> State# RealWorld -> (# State# RealWorld, Compact# #)
compactNew# Word#
size State# RealWorld
s of
  (# State# RealWorld
s, Compact#
com #) -> (# State# RealWorld
s, Compact# -> Compact
Compact Compact#
com #)
{-# INLINE new #-}

-- | Add a value to a compact region.
add :: Compact -> a -> IO a
add :: forall a. Compact -> a -> IO a
add (Compact Compact#
com) a
a = (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO (Compact# -> a -> State# RealWorld -> (# State# RealWorld, a #)
forall a.
Compact# -> a -> State# RealWorld -> (# State# RealWorld, a #)
compactAdd# Compact#
com a
a)
{-# INLINE add #-}

{-# INLINE compactWithSharing #-}
compactWithSharing :: a -> IO a
compactWithSharing :: forall a. a -> IO a
compactWithSharing a
a = do
  !(Compact c) <- Word -> IO Compact
new Word
31268
  IO (compactAddWithSharing# c a)