]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
changed createCCtx() to split into initialization and creation
authorPaul Cruz <paulcruz74@fb.com>
Wed, 19 Jul 2017 00:32:36 +0000 (17:32 -0700)
committerPaul Cruz <paulcruz74@fb.com>
Wed, 19 Jul 2017 00:32:36 +0000 (17:32 -0700)
contrib/adaptive-compression/adapt.c

index 35e26abb74833a4c73ec1df5051643bb74f49ca5..7eb4333a110c3d6ee2c92cc50fdac55789c6af32 100644 (file)
@@ -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)