{-# OPTIONS_GHC -optc-DZSTD_STATIC_LINKING_ONLY #-}
{-# LINE 1 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
-- Copyright (c) 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in
-- the LICENSE file in the root directory of this source tree. An
-- additional grant of patent rights can be found in the PATENTS file
-- in the same directory.

{-# LANGUAGE ExistentialQuantification #-}

-- |
-- Module      : Codec.Compression.Zstd.FFI
-- Copyright   : (c) 2016-present, Facebook, Inc. All rights reserved.
--
-- License     : BSD3
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : GHC
--
-- Types and functions that support the low-level FFI bindings.

module Codec.Compression.Zstd.FFI.Types
    (
      Buffer(..)
    , In
    , Out
    , CCtx
    , DCtx
    , CDict
    , DDict
    , peekPtr
    , pokePtr
    , peekSize
    , pokeSize
    , peekPos
    , pokePos
    ) where




import Foreign.C.Types (CInt(..), CSize(..))
import Foreign.Storable
import GHC.Ptr (Ptr(..))

-- | An opaque compression context structure.
data CCtx
-- | An opaque decompression context structure.
data DCtx

-- | An opaque pre-digested compression dictionary structure.
data CDict
-- | An opaque pre-digested decompression dictionary structure.
data DDict

-- | A tag type to indicate that a 'Buffer' is used for tracking input.
data In
-- | A tag type to indicate that a 'Buffer' is used for tracking output.
data Out

-- | A streaming buffer type. The type parameter statically indicates
-- whether the buffer is used to track an input or output buffer.
data Buffer io = forall a. Buffer {
      -- | Pointer to the start of the buffer.  This can be set once
      -- by the caller, and read by the streaming function.
      bufPtr  :: {-# UNPACK #-} !(Ptr a)
      -- | Size of the buffer (in bytes).  This can be set once by the
      -- caller, and is read by the streaming function.
    , bufSize :: {-# UNPACK #-} !CSize
      -- | Current offset into the buffer (in bytes).  This must be
      -- initially set to zero by the caller, and is updated by the
      -- streaming function.
    , bufPos  :: {-# UNPACK #-} !CSize
    }

instance Storable (Buffer io) where
    sizeOf _     = 24
{-# LINE 78 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
    alignment _  = alignment (undefined :: CInt)

    peek p = do
      ptr <- ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 82 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
      size <- ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 83 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
      pos <- ((\hsc_ptr -> peekByteOff hsc_ptr 16)) p
{-# LINE 84 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
      return (Buffer ptr size pos)

    poke p (Buffer ptr size pos) = do
      ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p ptr
{-# LINE 88 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p size
{-# LINE 89 "Codec/Compression/Zstd/FFI/Types.hsc" #-}
      ((\hsc_ptr -> pokeByteOff hsc_ptr 16)) p pos
{-# LINE 90 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Read the 'bufPtr' value from a 'Buffer'.
peekPtr :: Ptr (Buffer io) -> IO CSize
peekPtr p = ((\hsc_ptr -> peekByteOff hsc_ptr 0)) p
{-# LINE 94 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Write to the 'bufPtr' value in a 'Buffer'.
pokePtr :: Ptr (Buffer io) -> Ptr a -> IO ()
pokePtr dst p = ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) dst p
{-# LINE 98 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Read the 'bufSize' value from a 'Buffer'.
peekSize :: Ptr (Buffer io) -> IO CSize
peekSize p = ((\hsc_ptr -> peekByteOff hsc_ptr 8)) p
{-# LINE 102 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Write to the 'bufSize' value in a 'Buffer'.
pokeSize :: Ptr (Buffer io) -> CSize -> IO ()
pokeSize dst s = ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) dst s
{-# LINE 106 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Read the 'bufPos' value from a 'Buffer'.
peekPos :: Ptr (Buffer io) -> IO CSize
peekPos p = ((\hsc_ptr -> peekByteOff hsc_ptr 16)) p
{-# LINE 110 "Codec/Compression/Zstd/FFI/Types.hsc" #-}

-- | Write to the 'bufPos' value in a 'Buffer'.
pokePos :: Ptr (Buffer io) -> CSize -> IO ()
pokePos dst s = ((\hsc_ptr -> pokeByteOff hsc_ptr 16)) dst s
{-# LINE 114 "Codec/Compression/Zstd/FFI/Types.hsc" #-}