File: Types.hsc

package info (click to toggle)
haskell-zstd 0.1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,992 kB
  • sloc: ansic: 26,812; haskell: 1,085; makefile: 7
file content (113 lines) | stat: -rw-r--r-- 3,458 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- 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

#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>

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 _     = #const sizeof(ZSTD_inBuffer)
    alignment _  = alignment (undefined :: CInt)

    peek p = do
      ptr <- (#peek ZSTD_inBuffer, src) p
      size <- (#peek ZSTD_inBuffer, size) p
      pos <- (#peek ZSTD_inBuffer, pos) p
      return (Buffer ptr size pos)

    poke p (Buffer ptr size pos) = do
      (#poke ZSTD_inBuffer, src) p ptr
      (#poke ZSTD_inBuffer, size) p size
      (#poke ZSTD_inBuffer, pos) p pos

-- | Read the 'bufPtr' value from a 'Buffer'.
peekPtr :: Ptr (Buffer io) -> IO CSize
peekPtr p = (#peek ZSTD_inBuffer, src) p

-- | Write to the 'bufPtr' value in a 'Buffer'.
pokePtr :: Ptr (Buffer io) -> Ptr a -> IO ()
pokePtr dst p = (#poke ZSTD_inBuffer, src) dst p

-- | Read the 'bufSize' value from a 'Buffer'.
peekSize :: Ptr (Buffer io) -> IO CSize
peekSize p = (#peek ZSTD_inBuffer, size) p

-- | Write to the 'bufSize' value in a 'Buffer'.
pokeSize :: Ptr (Buffer io) -> CSize -> IO ()
pokeSize dst s = (#poke ZSTD_inBuffer, size) dst s

-- | Read the 'bufPos' value from a 'Buffer'.
peekPos :: Ptr (Buffer io) -> IO CSize
peekPos p = (#peek ZSTD_inBuffer, pos) p

-- | Write to the 'bufPos' value in a 'Buffer'.
pokePos :: Ptr (Buffer io) -> CSize -> IO ()
pokePos dst s = (#poke ZSTD_inBuffer, pos) dst s