1
0
mirror of https://github.com/facebook/zstd.git synced 2025-08-07 06:23:00 +03:00

[examples] Update streaming_memory_usage.c

Update to use the new streaming API. Making progress on Issue #1548.

Tested that the checks don't fail.
Tested with window log 9-32. The lowest and highest fail as expected.
This commit is contained in:
Nick Terrell
2019-04-01 18:05:49 -07:00
parent 00679da22b
commit cdc8ae2e9b

View File

@@ -19,6 +19,7 @@
#include <stdio.h> /* printf */ #include <stdio.h> /* printf */
#define ZSTD_STATIC_LINKING_ONLY #define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h" #include "zstd.h"
#include "utils.h"
/*=== functions ===*/ /*=== functions ===*/
@@ -61,90 +62,75 @@ int main(int argc, char const *argv[]) {
char const dataToCompress[INPUT_SIZE] = "abcde"; char const dataToCompress[INPUT_SIZE] = "abcde";
char compressedData[COMPRESSED_SIZE]; char compressedData[COMPRESSED_SIZE];
char decompressedData[INPUT_SIZE]; char decompressedData[INPUT_SIZE];
ZSTD_CStream* const cstream = ZSTD_createCStream(); /* the ZSTD_CCtx_params structure is a way to save parameters and use
if (cstream==NULL) { * them across multiple contexts. We use them here so we can call the
printf("Level %i : ZSTD_CStream Memory allocation failure \n", compressionLevel); * function ZSTD_estimateCStreamSize_usingCCtxParams().
return 1; */
} ZSTD_CCtx_params* const cctxParams = ZSTD_createCCtxParams();
CHECK(cctxParams != NULL, "ZSTD_createCCtxParams() failed!");
/* forces compressor to use maximum memory size for given compression level, /* Set the compression level. */
* by not providing any information on input size */ CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_compressionLevel, compressionLevel) );
ZSTD_parameters params = ZSTD_getParams(compressionLevel, ZSTD_CONTENTSIZE_UNKNOWN, 0); /* Set the window log.
if (wLog) { /* special mode : specific wLog */ * The value 0 means use the default window log, which is equivalent to
printf("Using custom compression parameter : level 1 + wLog=%u \n", wLog); * not setting it.
params = ZSTD_getParams(1 /*compressionLevel*/, */
1 << wLog /*estimatedSrcSize*/, CHECK_ZSTD( ZSTD_CCtxParams_setParameter(cctxParams, ZSTD_c_windowLog, wLog) );
0 /*no dictionary*/);
size_t const error = ZSTD_initCStream_advanced(cstream, NULL, 0, params, ZSTD_CONTENTSIZE_UNKNOWN);
if (ZSTD_isError(error)) {
printf("ZSTD_initCStream_advanced error : %s \n", ZSTD_getErrorName(error));
return 1;
}
} else {
size_t const error = ZSTD_initCStream(cstream, compressionLevel);
if (ZSTD_isError(error)) {
printf("ZSTD_initCStream error : %s \n", ZSTD_getErrorName(error));
return 1;
}
}
/* Force the compressor to allocate the maximum memory size for a given
* level by not providing the pledged source size, or calling
* ZSTD_compressStream2() with ZSTD_e_end.
*/
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
CHECK_ZSTD( ZSTD_CCtx_setParametersUsingCCtxParams(cctx, cctxParams) );
size_t compressedSize; size_t compressedSize;
{ ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 }; {
ZSTD_inBuffer inBuff = { dataToCompress, sizeof(dataToCompress), 0 };
ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 }; ZSTD_outBuffer outBuff = { compressedData, sizeof(compressedData), 0 };
size_t const cError = ZSTD_compressStream(cstream, &outBuff, &inBuff); CHECK_ZSTD( ZSTD_compressStream(cctx, &outBuff, &inBuff) );
if (ZSTD_isError(cError)) { size_t const remaining = ZSTD_endStream(cctx, &outBuff);
printf("ZSTD_compressStream error : %s \n", ZSTD_getErrorName(cError)); CHECK_ZSTD(remaining);
return 1; CHECK(remaining == 0, "Frame not flushed!");
}
size_t const fError = ZSTD_endStream(cstream, &outBuff);
if (ZSTD_isError(fError)) {
printf("ZSTD_endStream error : %s \n", ZSTD_getErrorName(fError));
return 1;
}
compressedSize = outBuff.pos; compressedSize = outBuff.pos;
} }
ZSTD_DStream* dstream = ZSTD_createDStream(); ZSTD_DCtx* const dctx = ZSTD_createDCtx();
if (dstream==NULL) { CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
printf("Level %i : ZSTD_DStream Memory allocation failure \n", compressionLevel); /* Set the maximum allowed window log.
return 1; * The value 0 means use the default window log, which is equivalent to
} * not setting it.
{ size_t const error = ZSTD_initDStream(dstream); */
if (ZSTD_isError(error)) { CHECK_ZSTD( ZSTD_DCtx_setParameter(dctx, ZSTD_d_windowLogMax, wLog) );
printf("ZSTD_initDStream error : %s \n", ZSTD_getErrorName(error)); /* forces decompressor to use maximum memory size, since the
return 1; * decompressed size is not stored in the frame header.
} */
}
/* forces decompressor to use maximum memory size, as decompressed size is not known */
{ ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 }; { ZSTD_inBuffer inBuff = { compressedData, compressedSize, 0 };
ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 }; ZSTD_outBuffer outBuff = { decompressedData, sizeof(decompressedData), 0 };
size_t const dResult = ZSTD_decompressStream(dstream, &outBuff, &inBuff); size_t const remaining = ZSTD_decompressStream(dctx, &outBuff, &inBuff);
if (ZSTD_isError(dResult)) { CHECK_ZSTD(remaining);
printf("ZSTD_decompressStream error : %s \n", ZSTD_getErrorName(dResult)); CHECK(remaining == 0, "Frame not complete!");
return 1; CHECK(outBuff.pos == sizeof(dataToCompress), "Bad decompression!");
}
if (dResult != 0) {
printf("ZSTD_decompressStream error : unfinished decompression \n");
return 1;
}
if (outBuff.pos != sizeof(dataToCompress)) {
printf("ZSTD_decompressStream error : incorrect decompression \n");
return 1;
}
} }
size_t const cstreamSize = ZSTD_sizeof_CStream(cstream); size_t const cstreamSize = ZSTD_sizeof_CStream(cctx);
size_t const cstreamEstimatedSize = wLog ? size_t const cstreamEstimatedSize = ZSTD_estimateCStreamSize_usingCCtxParams(cctxParams);
ZSTD_estimateCStreamSize_usingCParams(params.cParams) : size_t const dstreamSize = ZSTD_sizeof_DStream(dctx);
ZSTD_estimateCStreamSize(compressionLevel); size_t const dstreamEstimatedSize = ZSTD_estimateDStreamSize_fromFrame(compressedData, compressedSize);
size_t const dstreamSize = ZSTD_sizeof_DStream(dstream);
printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB \n", CHECK(cstreamSize <= cstreamEstimatedSize, "Compression mem (%u) > estimated (%u)",
(unsigned)cstreamSize, (unsigned)cstreamEstimatedSize);
CHECK(dstreamSize <= dstreamEstimatedSize, "Decompression mem (%u) > estimated (%u)",
(unsigned)dstreamSize, (unsigned)dstreamEstimatedSize);
printf("Level %2i : Compression Mem = %5u KB (estimated : %5u KB) ; Decompression Mem = %4u KB (estimated : %5u KB)\n",
compressionLevel, compressionLevel,
(unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10), (unsigned)(dstreamSize>>10)); (unsigned)(cstreamSize>>10), (unsigned)(cstreamEstimatedSize>>10),
(unsigned)(dstreamSize>>10), (unsigned)(dstreamEstimatedSize>>10));
ZSTD_freeDStream(dstream); ZSTD_freeDCtx(dctx);
ZSTD_freeCStream(cstream); ZSTD_freeCCtx(cctx);
ZSTD_freeCCtxParams(cctxParams);
if (wLog) break; /* single test */ if (wLog) break; /* single test */
} }
return 0; return 0;