From: inikep Date: Mon, 23 May 2016 14:24:52 +0000 (+0200) Subject: added ZSTD_createDCtx_advanced X-Git-Tag: v0.7.0^2~54^2^2~9^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=107e2431955a7bb0d78f31d74bddd880f2250f75;p=thirdparty%2Fzstd.git added ZSTD_createDCtx_advanced --- diff --git a/lib/common/zstd_static.h b/lib/common/zstd_static.h index 912eb1335..e0f50cf40 100644 --- a/lib/common/zstd_static.h +++ b/lib/common/zstd_static.h @@ -103,9 +103,12 @@ typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; } * Advanced functions ***************************************/ /*! ZSTD_createCCtx_advanced() : - * Create ZSTD context using external alloc and free functions */ + * Create a ZSTD compression context using external alloc and free functions */ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); +/*! ZSTD_createDCtx_advanced() : + * Create a ZSTD decompression context using external alloc and free functions */ +ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); ZSTDLIB_API unsigned ZSTD_maxCLevel (void); diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 00106f5fe..966d9acbd 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -120,20 +120,25 @@ struct ZSTD_CCtx_s ZSTD_CCtx* ZSTD_createCCtx(void) { - ZSTD_CCtx* ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx)); - if (!ctx) return NULL; - - ctx->customAlloc = malloc; - ctx->customFree = free; - return ctx; + ZSTD_customMem customMem = { NULL, NULL }; + return ZSTD_createCCtx_advanced(customMem); } ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) { + ZSTD_CCtx* ctx; + if (!customMem.customAlloc || !customMem.customFree) - return ZSTD_createCCtx(); + { + ctx = (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx)); + if (!ctx) return NULL; + + ctx->customAlloc = malloc; + ctx->customFree = free; + return ctx; + } - ZSTD_CCtx* ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx)); + ctx = (ZSTD_CCtx*) customMem.customAlloc(sizeof(ZSTD_CCtx)); if (!ctx) return NULL; memset(ctx, 0, sizeof(ZSTD_CCtx)); @@ -307,7 +312,9 @@ size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) { if (srcCCtx->stage!=1) return ERROR(stage_wrong); - dstCCtx->hashLog3 = srcCCtx->hashLog3; /* must be before ZSTD_resetCCtx_advanced */ + dstCCtx->hashLog3 = srcCCtx->hashLog3; /* must be before ZSTD_resetCCtx_advanced */ + dstCCtx->customAlloc = srcCCtx->customAlloc; + dstCCtx->customFree = srcCCtx->customFree; ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params, 0); dstCCtx->params.fParams.contentSizeFlag = 0; /* content size different from the one set during srcCCtx init */ diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 177e9c8a2..a3eee709e 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -116,6 +116,8 @@ struct ZSTD_DCtx_s size_t expected; size_t headerSize; ZSTD_frameParams fParams; + ZSTD_allocFunction customAlloc; + ZSTD_freeFunction customFree; blockType_t bType; /* used in ZSTD_decompressContinue(), to transfer blockType between header decoding and block decoding stages */ ZSTD_dStage stage; U32 flagRepeatTable; @@ -143,15 +145,38 @@ size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx) ZSTD_DCtx* ZSTD_createDCtx(void) { - ZSTD_DCtx* dctx = (ZSTD_DCtx*)malloc(sizeof(ZSTD_DCtx)); - if (dctx==NULL) return NULL; + ZSTD_customMem customMem = { NULL, NULL }; + return ZSTD_createDCtx_advanced(customMem); +} + +ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem) +{ + ZSTD_DCtx* dctx; + + if (!customMem.customAlloc || !customMem.customFree) + { + dctx = (ZSTD_DCtx*) malloc(sizeof(ZSTD_DCtx)); + if (!dctx) return NULL; + dctx->customAlloc = malloc; + dctx->customFree = free; + + ZSTD_decompressBegin(dctx); + return dctx; + } + + dctx = (ZSTD_DCtx*) customMem.customAlloc(sizeof(ZSTD_DCtx)); + if (!dctx) return NULL; + dctx->customAlloc = customMem.customAlloc; + dctx->customFree = customMem.customFree; + ZSTD_decompressBegin(dctx); return dctx; } + size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx) { - free(dctx); + dctx->customFree(dctx); return 0; /* reserved as a potential error code in the future */ }