From: Paul Cruz Date: Wed, 19 Jul 2017 00:32:36 +0000 (-0700) Subject: changed createCCtx() to split into initialization and creation X-Git-Tag: v1.3.1^2~13^2^2~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d7f1afadd508de925783df775414b6e8e24e7bf;p=thirdparty%2Fzstd.git changed createCCtx() to split into initialization and creation --- diff --git a/contrib/adaptive-compression/adapt.c b/contrib/adaptive-compression/adapt.c index 35e26abb7..7eb4333a1 100644 --- a/contrib/adaptive-compression/adapt.c +++ b/contrib/adaptive-compression/adapt.c @@ -187,14 +187,8 @@ static int initCond(cond_t* cond) return ret; } -static adaptCCtx* createCCtx(unsigned numJobs) +static int initCCtx(adaptCCtx* ctx, unsigned numJobs) { - - adaptCCtx* const ctx = calloc(1, sizeof(adaptCCtx)); - if (ctx == NULL) { - DISPLAY("Error: could not allocate space for context\n"); - return NULL; - } ctx->compressionLevel = g_compressionLevel; { int pthreadError = 0; @@ -208,7 +202,7 @@ static adaptCCtx* createCCtx(unsigned numJobs) pthreadError |= initCond(&ctx->jobWrite_cond); pthreadError |= initMutex(&ctx->completion_mutex); pthreadError |= initMutex(&ctx->stats_mutex); - if (pthreadError) return NULL; + if (pthreadError) return pthreadError; } ctx->numJobs = numJobs; ctx->jobReadyID = 0; @@ -216,6 +210,12 @@ static adaptCCtx* createCCtx(unsigned numJobs) ctx->jobWriteID = 0; ctx->lastDictSize = 0; ctx->jobs = calloc(1, numJobs*sizeof(jobDescription)); + + if (!ctx->jobs) { + DISPLAY("Error: could not allocate space for jobs during context creation\n"); + return 1; + } + /* initializing jobs */ { unsigned jobNum; @@ -226,33 +226,51 @@ static adaptCCtx* createCCtx(unsigned numJobs) job->lastJob = 0; if (!job->src.start || !job->dst.start) { DISPLAY("Could not allocate buffers for jobs\n"); - return NULL; + return 1; } job->src.capacity = FILE_CHUNK_SIZE; job->dst.capacity = ZSTD_compressBound(FILE_CHUNK_SIZE); } } + ctx->nextJobID = 0; ctx->threadError = 0; ctx->allJobsCompleted = 0; ctx->adaptParam = DEFAULT_ADAPT_PARAM; + ctx->cctx = ZSTD_createCCtx(); + if (!ctx->cctx) { + DISPLAY("Error: could not allocate ZSTD_CCtx\n"); + return 1; + } + ctx->input.filled = 0; ctx->input.buffer.capacity = 2 * FILE_CHUNK_SIZE; + ctx->input.buffer.start = malloc(ctx->input.buffer.capacity); if (!ctx->input.buffer.start) { DISPLAY("Error: could not allocate input buffer\n"); - return NULL; + return 1; } - if (!ctx->cctx) { - DISPLAY("Error: could not allocate ZSTD_CCtx\n"); + return 0; +} + +static adaptCCtx* createCCtx(unsigned numJobs) +{ + + adaptCCtx* const ctx = calloc(1, sizeof(adaptCCtx)); + if (ctx == NULL) { + DISPLAY("Error: could not allocate space for context\n"); return NULL; } - if (!ctx->jobs) { - DISPLAY("Error: could not allocate space for jobs during context creation\n"); - return NULL; + { + int const error = initCCtx(ctx, numJobs); + if (error) { + freeCCtx(ctx); + return NULL; + } + return ctx; } - return ctx; } static void signalErrorToThreads(adaptCCtx* ctx)