ensure zlibwrapper uses ZSTD_malloc() and ZSTD_free()

which is compatible with { NULL, NULL, NULL }
This commit is contained in:
Yann Collet 2017-06-02 14:01:21 -07:00
parent b877e834b1
commit dcb7535352
2 changed files with 27 additions and 29 deletions

View File

@ -61,7 +61,7 @@ void* ZSTD_calloc(size_t size, ZSTD_customMem customMem)
{ {
if (customMem.customAlloc) { if (customMem.customAlloc) {
/* calloc implemented as malloc+memset; /* calloc implemented as malloc+memset;
* not as efficient, but our best guess for custom malloc */ * not as efficient as calloc, but next best guess for custom malloc */
void* const ptr = customMem.customAlloc(customMem.opaque, size); void* const ptr = customMem.customAlloc(customMem.opaque, size);
memset(ptr, 0, size); memset(ptr, 0, size);
return ptr; return ptr;

View File

@ -17,7 +17,7 @@
#include "zstd_zlibwrapper.h" #include "zstd_zlibwrapper.h"
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */ #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */
#include "zstd.h" #include "zstd.h"
#include "zstd_internal.h" /* defaultCustomMem */ #include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
#define Z_INFLATE_SYNC 8 #define Z_INFLATE_SYNC 8
@ -100,7 +100,7 @@ size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
{ {
if (zwc==NULL) return 0; /* support free on NULL */ if (zwc==NULL) return 0; /* support free on NULL */
if (zwc->zbc) ZSTD_freeCStream(zwc->zbc); if (zwc->zbc) ZSTD_freeCStream(zwc->zbc);
zwc->customMem.customFree(zwc->customMem.opaque, zwc); ZSTD_free(zwc, zwc->customMem);
return 0; return 0;
} }
@ -472,8 +472,8 @@ size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
{ {
if (zwd==NULL) return 0; /* support free on null */ if (zwd==NULL) return 0; /* support free on null */
if (zwd->zbd) ZSTD_freeDStream(zwd->zbd); if (zwd->zbd) ZSTD_freeDStream(zwd->zbd);
if (zwd->version) zwd->customMem.customFree(zwd->customMem.opaque, zwd->version); ZSTD_free(zwd->version, zwd->customMem);
zwd->customMem.customFree(zwd->customMem.opaque, zwd); ZSTD_free(zwd, zwd->customMem);
return 0; return 0;
} }
@ -505,12 +505,11 @@ ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
return inflateInit(strm); return inflateInit(strm);
} }
{ { ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
ZWRAP_DCtx* zwd = ZWRAP_createDCtx(strm);
LOG_WRAPPERD("- inflateInit\n"); LOG_WRAPPERD("- inflateInit\n");
if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0); if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
zwd->version = zwd->customMem.customAlloc(zwd->customMem.opaque, strlen(version) + 1); zwd->version = ZSTD_malloc(strlen(version)+1, zwd->customMem);
if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0); if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
strcpy(zwd->version, version); strcpy(zwd->version, version);
@ -534,11 +533,10 @@ ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
return inflateInit2_(strm, windowBits, version, stream_size); return inflateInit2_(strm, windowBits, version, stream_size);
} }
{ { int ret = z_inflateInit_ (strm, version, stream_size);
int ret = z_inflateInit_ (strm, version, stream_size);
LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits); LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
if (ret == Z_OK) { if (ret == Z_OK) {
ZWRAP_DCtx* zwd = (ZWRAP_DCtx*)strm->state; ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
if (zwd == NULL) return Z_STREAM_ERROR; if (zwd == NULL) return Z_STREAM_ERROR;
zwd->windowBits = windowBits; zwd->windowBits = windowBits;
} }
@ -552,7 +550,7 @@ int ZWRAP_inflateReset_keepDict(z_streamp strm)
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
return inflateReset(strm); return inflateReset(strm);
{ ZWRAP_DCtx* zwd = (ZWRAP_DCtx*) strm->state; { ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
if (zwd == NULL) return Z_STREAM_ERROR; if (zwd == NULL) return Z_STREAM_ERROR;
ZWRAP_initDCtx(zwd); ZWRAP_initDCtx(zwd);
zwd->decompState = ZWRAP_useReset; zwd->decompState = ZWRAP_useReset;