From 595401e4c7bcd90e6cf8eeefdd0d39302d8840b7 Mon Sep 17 00:00:00 2001 From: Yi Jin Date: Sun, 16 Dec 2018 21:09:21 -0800 Subject: [PATCH] edit README.md and remove simple_compressionCCtx.c --- examples/.gitignore | 2 +- examples/Makefile | 8 +-- examples/README.md | 10 ++- examples/simple_compressionCCtx.c | 115 ------------------------------ 4 files changed, 13 insertions(+), 122 deletions(-) delete mode 100644 examples/simple_compressionCCtx.c diff --git a/examples/.gitignore b/examples/.gitignore index c92c6989..d682cae3 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -1,7 +1,7 @@ #build simple_compression simple_decompression -simple_compressionCCtx +multiple_simple_compression dictionary_compression dictionary_decompression streaming_compression diff --git a/examples/Makefile b/examples/Makefile index 7af6176b..25a0a62c 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -17,7 +17,7 @@ LIB = ../lib/libzstd.a default: all all: simple_compression simple_decompression \ - simple_compressionCCtx\ + multiple_simple_compression\ dictionary_compression dictionary_decompression \ streaming_compression streaming_decompression \ multiple_streaming_compression streaming_memory_usage @@ -31,7 +31,7 @@ simple_compression : simple_compression.c utils.h $(LIB) simple_decompression : simple_decompression.c utils.h $(LIB) $(CC) $(CPPFLAGS) $(CFLAGS) $< $(LIB) $(LDFLAGS) -o $@ -simple_compressionCCtx : simple_compressionCCtx.c utils.h $(LIB) +multiple_simple_compression : multiple_simple_compression.c utils.h $(LIB) $(CC) $(CPPFLAGS) $(CFLAGS) $< $(LIB) $(LDFLAGS) -o $@ dictionary_compression : dictionary_compression.c utils.h $(LIB) @@ -55,7 +55,7 @@ streaming_memory_usage : streaming_memory_usage.c $(LIB) clean: @rm -f core *.o tmp* result* *.zst \ simple_compression simple_decompression \ - simple_compressionCCtx \ + multiple_simple_compression \ dictionary_compression dictionary_decompression \ streaming_compression streaming_decompression \ multiple_streaming_compression streaming_memory_usage @@ -67,7 +67,7 @@ test: all @echo -- Simple compression tests ./simple_compression tmp ./simple_decompression tmp.zst - ./simple_compressionCCtx *.c + ./multiple_simple_compression *.c ./streaming_decompression tmp.zst > /dev/null @echo -- Streaming memory usage ./streaming_memory_usage diff --git a/examples/README.md b/examples/README.md index eba50c99..0bff7ac1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,8 +11,14 @@ Zstandard library : usage examples Result remains in memory. Introduces usage of : `ZSTD_decompress()` +- [Multiple simple compression](multiple_simple_compression.c) : + Compress multiple files (in simple mode) in a single command line. + Demonstrates memory preservation technique that + minimizes malloc()/free() calls by re-using existing resources. + Introduces usage of : `ZSTD_compressCCtx()` + - [Streaming memory usage](streaming_memory_usage.c) : - Provides amount of memory used by streaming context + Provides amount of memory used by streaming context. Introduces usage of : `ZSTD_sizeof_CStream()` - [Streaming compression](streaming_compression.c) : @@ -20,7 +26,7 @@ Zstandard library : usage examples Introduces usage of : `ZSTD_compressStream()` - [Multiple Streaming compression](multiple_streaming_compression.c) : - Compress multiple files in a single command line. + Compress multiple files (in streaming mode) in a single command line. Introduces memory usage preservation technique, reducing impact of malloc()/free() and memset() by re-using existing resources. diff --git a/examples/simple_compressionCCtx.c b/examples/simple_compressionCCtx.c deleted file mode 100644 index 00279e4e..00000000 --- a/examples/simple_compressionCCtx.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. - * All rights reserved. - * - * This source code is licensed under both the BSD-style license (found in the - * LICENSE file in the root directory of this source tree) and the GPLv2 (found - * in the COPYING file in the root directory of this source tree). - * You may select, at your option, one of the above-listed licenses. - */ - -#include // malloc, free, exit -#include // fprintf, perror, fopen, etc. -#include // strlen, strcat, memset, strerror -#include // errno -#include // stat -#include // presumes zstd library is installed -#include "utils.h" - -/* compress with pre-allocated context (ZSTD_CCtx) and input/output buffers*/ -static void compressExpress_orDie(const char* fname, const char* oname, - ZSTD_CCtx* cctx, void* cBuff, size_t cBuffSize, void* fBuff, size_t fBuffSize) -{ - size_t fSize; - loadFile_orDie(fname, &fSize, fBuff, fBuffSize); - - size_t const cSize = ZSTD_compressCCtx(cctx, cBuff, cBuffSize, fBuff, fSize, 1); - if (ZSTD_isError(cSize)) { - fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize)); - exit(8); - } - - saveFile_orDie(oname, cBuff, cSize); - - /* success */ - printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); -} - -static void getOutFilename(const char* const filename, char* const outFilename) -{ - memset(outFilename, 0, 1); - strcat(outFilename, filename); - strcat(outFilename, ".zst"); -} - -/* allocate memory for buffers big enough to compress all files - * as well as memory for output file names (outFileName) - */ -void allocMemory_orDie(int argc, const char** argv, char** outFilename, - void** cBuffer, size_t* cBufferSize, void** fBuffer, size_t* fBufferSize) { - size_t maxFilenameLength=0; - size_t maxFileSize = 0; - - int argNb; - for (argNb = 1; argNb < argc; argNb++) { - const char* const filename = argv[argNb]; - size_t const filenameLength = strlen(filename); - size_t const fileSize = fsize_orDie(filename); - - if (filenameLength > maxFilenameLength) maxFilenameLength = filenameLength; - if (fileSize > maxFileSize) maxFileSize = fileSize; - } - *cBufferSize = ZSTD_compressBound(maxFileSize); - *fBufferSize = maxFileSize; - - /* allocate memory for output file name, input/output buffers for all compression tasks */ - *outFilename = (char*)malloc_orDie(maxFilenameLength + 5); - *cBuffer = malloc_orDie(*cBufferSize); - *fBuffer = malloc_orDie(*fBufferSize); -} - -int main(int argc, const char** argv) -{ - const char* const exeName = argv[0]; - - if (argc<2) { - printf("wrong arguments\n"); - printf("usage:\n"); - printf("%s FILE(s)\n", exeName); - return 1; - } - - /* allocate memory for buffers big enough to compress all files - * as well as memory for output file name (outFileName) - * fBuffer - buffer for input file data - * cBuffer - buffer for compressed data - */ - char* outFilename; - void* fBuffer; - void* cBuffer; - size_t fBufferSize; - size_t cBufferSize; - allocMemory_orDie(argc, argv, &outFilename, &cBuffer, &cBufferSize, &fBuffer, &fBufferSize); - - /* create a compression context (ZSTD_CCtx) for all compression tasks */ - ZSTD_CCtx* const cctx = ZSTD_createCCtx(); - if (cctx==NULL) { fprintf(stderr, "ZSTD_createCCtx() error \n"); exit(10); } - - /* compress files with shared context, input and output buffers */ - int argNb; - for (argNb = 1; argNb < argc; argNb++) { - const char* const inFilename = argv[argNb]; - getOutFilename(inFilename, outFilename); - compressExpress_orDie(inFilename, outFilename, cctx, cBuffer, cBufferSize, fBuffer, fBufferSize); - } - - /* free momery resources */ - free(outFilename); - free(fBuffer); - free(cBuffer); - ZSTD_freeCCtx(cctx); /* never fails */ - - printf("compressed %i files \n", argc-1); - - return 0; -}