diff options
author | Yann Collet <cyan@fb.com> | 2018-02-01 01:36:38 -0800 |
---|---|---|
committer | Yann Collet <cyan@fb.com> | 2018-02-01 01:38:14 -0800 |
commit | 25efdd80c5aee15a425f7e6bea00b4c34bfb8db2 (patch) | |
tree | 8fa7b288090c1203ba8cf10c5718c18e8a344550 /examples | |
parent | 886a4858451800f6ebd621a2816a4698486d7ff1 (diff) | |
download | lz4-25efdd80c5aee15a425f7e6bea00b4c34bfb8db2.tar.gz lz4-25efdd80c5aee15a425f7e6bea00b4c34bfb8db2.tar.bz2 lz4-25efdd80c5aee15a425f7e6bea00b4c34bfb8db2.zip |
refactored ressource allocation to avoid goto
Diffstat (limited to 'examples')
-rw-r--r-- | examples/frameCompress.c | 35 |
1 files changed, 12 insertions, 23 deletions
diff --git a/examples/frameCompress.c b/examples/frameCompress.c index 5275ffe..d62053f 100644 --- a/examples/frameCompress.c +++ b/examples/frameCompress.c @@ -116,40 +116,29 @@ compress_file_internal(FILE* in, FILE* out, } static compressResult_t -compress_file(FILE* in, FILE* out) +compress_file(FILE* f_in, FILE* f_out) { - compressResult_t result = { 1, 0, 0 }; /* == error, default (early exit) */ + compressResult_t result = { 1, 0, 0 }; /* == error (default) */ - assert(in != NULL); - assert(out != NULL); + assert(f_in != NULL); + assert(f_out != NULL); - /* allocate ressources */ + /* ressource allocation */ LZ4F_compressionContext_t ctx; - if (LZ4F_isError( LZ4F_createCompressionContext(&ctx, LZ4F_VERSION) )) { - printf("error: failed to create context \n"); - return result; - } - - char* outbuff = NULL; + size_t const ctxCreation = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); void* const src = malloc(IN_CHUNK_SIZE); - if (!src) { - printf("Not enough memory\n"); - goto cleanup; - } - size_t const outbufCapacity = LZ4F_compressBound(IN_CHUNK_SIZE, &kPrefs); /* large enough for any input <= IN_CHUNK_SIZE */ - outbuff = malloc(outbufCapacity); - if (!outbuff) { - printf("Not enough memory\n"); - goto cleanup; - } + void* const outbuff = malloc(outbufCapacity); - result = compress_file_internal(in, out, + if (!LZ4F_isError(ctxCreation) && src && outbuff) { + result = compress_file_internal(f_in, f_out, ctx, src, IN_CHUNK_SIZE, outbuff, outbufCapacity); + } else { + printf("error : ressource allocation failed \n"); + } - cleanup: LZ4F_freeCompressionContext(ctx); /* supports free on NULL */ free(src); free(outbuff); |