* 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);
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));
{
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 */
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;
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 */
}