]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/examples/streaming_memory_usage.c
2 * Copyright (c) 2017-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 /*=== Tuning parameter ===*/
13 #ifndef MAX_TESTED_LEVEL
14 #define MAX_TESTED_LEVEL 12
18 /*=== Dependencies ===*/
19 #include <stdio.h> /* printf */
20 #define ZSTD_STATIC_LINKING_ONLY
26 /*! readU32FromChar() :
27 @return : unsigned integer value read from input in `char` format
28 allows and interprets K, KB, KiB, M, MB and MiB suffix.
29 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
30 Note : function result can overflow if digit string > MAX_UINT */
31 static unsigned readU32FromChar(const char** stringPtr
)
34 while ((**stringPtr
>='0') && (**stringPtr
<='9'))
35 result
*= 10, result
+= **stringPtr
- '0', (*stringPtr
)++ ;
36 if ((**stringPtr
=='K') || (**stringPtr
=='M')) {
38 if (**stringPtr
=='M') result
<<= 10;
40 if (**stringPtr
=='i') (*stringPtr
)++;
41 if (**stringPtr
=='B') (*stringPtr
)++;
47 int main(int argc
, char const *argv
[]) {
49 printf("\n Zstandard (v%u) memory usage for streaming contexts : \n\n", ZSTD_versionNumber());
53 const char* valStr
= argv
[1];
54 wLog
= readU32FromChar(&valStr
);
58 for (compressionLevel
= 1; compressionLevel
<= MAX_TESTED_LEVEL
; compressionLevel
++) {
60 #define COMPRESSED_SIZE 128
61 char const dataToCompress
[INPUT_SIZE
] = "abcde";
62 char compressedData
[COMPRESSED_SIZE
];
63 char decompressedData
[INPUT_SIZE
];
64 ZSTD_CStream
* const cstream
= ZSTD_createCStream();
66 printf("Level %i : ZSTD_CStream Memory allocation failure \n", compressionLevel
);
70 /* forces compressor to use maximum memory size for given compression level,
71 * by not providing any information on input size */
72 ZSTD_parameters params
= ZSTD_getParams(compressionLevel
, 0, 0);
73 if (wLog
) { /* special mode : specific wLog */
74 printf("Using custom compression parameter : level 1 + wLog=%u \n", wLog
);
75 params
= ZSTD_getParams(1, 1 << wLog
, 0);
76 size_t const error
= ZSTD_initCStream_advanced(cstream
, NULL
, 0, params
, 0);
77 if (ZSTD_isError(error
)) {
78 printf("ZSTD_initCStream_advanced error : %s \n", ZSTD_getErrorName(error
));
82 size_t const error
= ZSTD_initCStream(cstream
, compressionLevel
);
83 if (ZSTD_isError(error
)) {
84 printf("ZSTD_initCStream error : %s \n", ZSTD_getErrorName(error
));
89 size_t compressedSize
;
90 { ZSTD_inBuffer inBuff
= { dataToCompress
, sizeof(dataToCompress
), 0 };
91 ZSTD_outBuffer outBuff
= { compressedData
, sizeof(compressedData
), 0 };
92 size_t const cError
= ZSTD_compressStream(cstream
, &outBuff
, &inBuff
);
93 if (ZSTD_isError(cError
)) {
94 printf("ZSTD_compressStream error : %s \n", ZSTD_getErrorName(cError
));
97 size_t const fError
= ZSTD_endStream(cstream
, &outBuff
);
98 if (ZSTD_isError(fError
)) {
99 printf("ZSTD_endStream error : %s \n", ZSTD_getErrorName(fError
));
102 compressedSize
= outBuff
.pos
;
105 ZSTD_DStream
* dstream
= ZSTD_createDStream();
107 printf("Level %i : ZSTD_DStream Memory allocation failure \n", compressionLevel
);
110 { size_t const error
= ZSTD_initDStream(dstream
);
111 if (ZSTD_isError(error
)) {
112 printf("ZSTD_initDStream error : %s \n", ZSTD_getErrorName(error
));
116 /* forces decompressor to use maximum memory size, as decompressed size is not known */
117 { ZSTD_inBuffer inBuff
= { compressedData
, compressedSize
, 0 };
118 ZSTD_outBuffer outBuff
= { decompressedData
, sizeof(decompressedData
), 0 };
119 size_t const dResult
= ZSTD_decompressStream(dstream
, &outBuff
, &inBuff
);
120 if (ZSTD_isError(dResult
)) {
121 printf("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(dResult
));
125 printf("ZSTD_decompressStream error : unfinished decompression \n");
128 if (outBuff
.pos
!= sizeof(dataToCompress
)) {
129 printf("ZSTD_decompressStream error : incorrect decompression \n");
134 size_t const cstreamSize
= ZSTD_sizeof_CStream(cstream
);
135 size_t const cstreamEstimatedSize
= wLog
?
136 ZSTD_estimateCStreamSize_usingCParams(params
.cParams
) :
137 ZSTD_estimateCStreamSize(compressionLevel
);
138 size_t const dstreamSize
= ZSTD_sizeof_DStream(dstream
);
140 printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB \n",
142 (unsigned)(cstreamSize
>>10), (unsigned)(cstreamEstimatedSize
>>10), (unsigned)(dstreamSize
>>10));
144 ZSTD_freeDStream(dstream
);
145 ZSTD_freeCStream(cstream
);
146 if (wLog
) break; /* single test */