edit README.md and remove simple_compressionCCtx.c

This commit is contained in:
Yi Jin 2018-12-16 21:09:21 -08:00
parent ea4859ec71
commit 595401e4c7
4 changed files with 13 additions and 122 deletions

2
examples/.gitignore vendored
View File

@ -1,7 +1,7 @@
#build
simple_compression
simple_decompression
simple_compressionCCtx
multiple_simple_compression
dictionary_compression
dictionary_decompression
streaming_compression

View File

@ -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

View File

@ -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.

View File

@ -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 <stdlib.h> // malloc, free, exit
#include <stdio.h> // fprintf, perror, fopen, etc.
#include <string.h> // strlen, strcat, memset, strerror
#include <errno.h> // errno
#include <sys/stat.h> // stat
#include <zstd.h> // 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;
}