2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
21 #include <zstd.h> // presumes zstd library is installed
22 #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
27 /*! readU32FromChar() :
28 @return : unsigned integer value read from input in `char` format
29 allows and interprets K, KB, KiB, M, MB and MiB suffix.
30 Will also modify `*stringPtr`, advancing it to position where it stopped reading.
31 Note : function result can overflow if digit string > MAX_UINT */
32 static unsigned readU32FromChar(const char** stringPtr)
35 while ((**stringPtr >='0') && (**stringPtr <='9'))
36 result *= 10, result += **stringPtr - '0', (*stringPtr)++ ;
37 if ((**stringPtr=='K') || (**stringPtr=='M')) {
39 if (**stringPtr=='M') result <<= 10;
41 if (**stringPtr=='i') (*stringPtr)++;
42 if (**stringPtr=='B') (*stringPtr)++;
48 int main(int argc, char const *argv[]) {
50 printf("\n Zstandard (v%s) memory usage for streaming : \n\n", ZSTD_versionString());
54 const char* valStr = argv[1];
55 wLog = readU32FromChar(&valStr);
59 for (compressionLevel = 1; compressionLevel <= MAX_TESTED_LEVEL; compressionLevel++) {
61 #define COMPRESSED_SIZE 128
62 char const dataToCompress[INPUT_SIZE] = "abcde";
63 char compressedData[COMPRESSED_SIZE];
64 char decompressedData[INPUT_SIZE];
65 /* the ZSTD_CCtx_params structure is a way to save parameters and use
66 * them across multiple contexts. We use them here so we can call the
67 * function ZSTD_estimateCStreamSize_usingCCtxParams().
69 ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams();
70 CHECK(cctxParams != NULL, "ZSTD_createCCtxParams() failed!");
72 /* Set the compression level. */
73 CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_compressionLevel, compressionLevel) );
74 /* Set the window log.
75 * The value 0 means use the default window log, which is equivalent to
78 CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, wLog) );
80 /* Force the compressor to allocate the maximum memory size for a given
81 * level by not providing the pledged source size, or calling
82 * ZSTD_compressStream2() with ZSTD_e_end.
84 ZSTD_CCtx* const cctx = ZSTD_createCCtx();
85 CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
86 CHECK_ZSTD( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) );
87 size_t compressedSize;
89 ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
90 ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
91 CHECK_ZSTD( ZSTD_compressStream(cctx, &outBuff, &inBuff) );
92 size_t const remaining = ZSTD_endStream(cctx, &outBuff);
93 CHECK_ZSTD(remaining);
94 CHECK(remaining == 0, "Frame not flushed!");
95 compressedSize = outBuff.pos;
98 ZSTD_DCtx* const dctx = ZSTD_createDCtx();
99 CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
100 /* Set the maximum allowed window log.
101 * The value 0 means use the default window log, which is equivalent to
104 CHECK_ZSTD( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, wLog) );
105 /* forces decompressor to use maximum memory size, since the
106 * decompressed size is not stored in the frame header.
108 { ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
109 ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
110 size_t const remaining = ZSTD_decompressStream(dctx, &outBuff, &inBuff);
111 CHECK_ZSTD(remaining);
112 CHECK(remaining == 0, "Frame not complete!");
113 CHECK(outBuff.pos == sizeof(dataToCompress), "Bad decompression!");
116 size_t const cstreamSize = ZSTD_sizeof_CStream(cctx);
117 size_t const cstreamEstimatedSize = ZSTD_estimateCStreamSize_usingCCtxParams(cctxParams);
118 size_t const dstreamSize = ZSTD_sizeof_DStream(dctx);
119 size_t const dstreamEstimatedSize = ZSTD_estimateDStreamSize_fromFrame(compressedData, compressedSize);
121 CHECK(cstreamSize <= cstreamEstimatedSize, "Compression mem (%u) > estimated (%u)",
122 (unsigned)cstreamSize, (unsigned)cstreamEstimatedSize);
123 CHECK(dstreamSize <= dstreamEstimatedSize, "Decompression mem (%u) > estimated (%u)",
124 (unsigned)dstreamSize, (unsigned)dstreamEstimatedSize);
126 printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB (estimated : %5u KB)\n",
128 (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10),
129 (unsigned)(dstreamSize>>10), (unsigned)(dstreamEstimatedSize>>10));
133 ZSTD_freeCCtxParams(cctxParams);
134 if (wLog) break; /* single test */