changed createCCtx() to split into initialization and creation

This commit is contained in:
Paul Cruz 2017-07-18 17:32:36 -07:00
parent 2c4e4ddc50
commit 3d7f1afadd

View File

@ -187,14 +187,8 @@ static int initCond(cond_t* cond)
return ret; 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; ctx->compressionLevel = g_compressionLevel;
{ {
int pthreadError = 0; int pthreadError = 0;
@ -208,7 +202,7 @@ static adaptCCtx* createCCtx(unsigned numJobs)
pthreadError |= initCond(&ctx->jobWrite_cond); pthreadError |= initCond(&ctx->jobWrite_cond);
pthreadError |= initMutex(&ctx->completion_mutex); pthreadError |= initMutex(&ctx->completion_mutex);
pthreadError |= initMutex(&ctx->stats_mutex); pthreadError |= initMutex(&ctx->stats_mutex);
if (pthreadError) return NULL; if (pthreadError) return pthreadError;
} }
ctx->numJobs = numJobs; ctx->numJobs = numJobs;
ctx->jobReadyID = 0; ctx->jobReadyID = 0;
@ -216,6 +210,12 @@ static adaptCCtx* createCCtx(unsigned numJobs)
ctx->jobWriteID = 0; ctx->jobWriteID = 0;
ctx->lastDictSize = 0; ctx->lastDictSize = 0;
ctx->jobs = calloc(1, numJobs*sizeof(jobDescription)); 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 */ /* initializing jobs */
{ {
unsigned jobNum; unsigned jobNum;
@ -226,33 +226,51 @@ static adaptCCtx* createCCtx(unsigned numJobs)
job->lastJob = 0; job->lastJob = 0;
if (!job->src.start || !job->dst.start) { if (!job->src.start || !job->dst.start) {
DISPLAY("Could not allocate buffers for jobs\n"); DISPLAY("Could not allocate buffers for jobs\n");
return NULL; return 1;
} }
job->src.capacity = FILE_CHUNK_SIZE; job->src.capacity = FILE_CHUNK_SIZE;
job->dst.capacity = ZSTD_compressBound(FILE_CHUNK_SIZE); job->dst.capacity = ZSTD_compressBound(FILE_CHUNK_SIZE);
} }
} }
ctx->nextJobID = 0; ctx->nextJobID = 0;
ctx->threadError = 0; ctx->threadError = 0;
ctx->allJobsCompleted = 0; ctx->allJobsCompleted = 0;
ctx->adaptParam = DEFAULT_ADAPT_PARAM; ctx->adaptParam = DEFAULT_ADAPT_PARAM;
ctx->cctx = ZSTD_createCCtx(); ctx->cctx = ZSTD_createCCtx();
if (!ctx->cctx) {
DISPLAY("Error: could not allocate ZSTD_CCtx\n");
return 1;
}
ctx->input.filled = 0; ctx->input.filled = 0;
ctx->input.buffer.capacity = 2 * FILE_CHUNK_SIZE; ctx->input.buffer.capacity = 2 * FILE_CHUNK_SIZE;
ctx->input.buffer.start = malloc(ctx->input.buffer.capacity); ctx->input.buffer.start = malloc(ctx->input.buffer.capacity);
if (!ctx->input.buffer.start) { if (!ctx->input.buffer.start) {
DISPLAY("Error: could not allocate input buffer\n"); DISPLAY("Error: could not allocate input buffer\n");
return 1;
}
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; return NULL;
} }
if (!ctx->cctx) { {
DISPLAY("Error: could not allocate ZSTD_CCtx\n"); int const error = initCCtx(ctx, numJobs);
return NULL; if (error) {
freeCCtx(ctx);
return NULL;
}
return ctx;
} }
if (!ctx->jobs) {
DISPLAY("Error: could not allocate space for jobs during context creation\n");
return NULL;
}
return ctx;
} }
static void signalErrorToThreads(adaptCCtx* ctx) static void signalErrorToThreads(adaptCCtx* ctx)