Cleanup: Remove M_EVP_MD_* macros
[oweals/openssl.git] / crypto / async / async.c
index c18c5c4517b3567bcd10bf46e2452d55e61fc3d8..031ca039e485cac2c353846d1a2c3a128c53199d 100644 (file)
  */
 #undef _FORTIFY_SOURCE
 
+/* This must be the first #include file */
+#include "async_locl.h"
+
 #include <openssl/err.h>
-#include <openssl/async.h>
 #include <string.h>
-#include "async_locl.h"
 
 #define ASYNC_JOB_RUNNING   0
 #define ASYNC_JOB_PAUSING   1
@@ -168,8 +169,11 @@ static ASYNC_JOB *async_get_pool_job(void) {
             return NULL;
 
         job = async_job_new();
-        if (job) {
-            async_fibre_makecontext(&job->fibrectx);
+        if (job != NULL) {
+            if (! async_fibre_makecontext(&job->fibrectx)) {
+                async_job_free(job);
+                return NULL;
+            }
             pool->curr_size++;
         }
     }
@@ -330,7 +334,7 @@ static void async_empty_pool(async_pool *pool)
 
 int ASYNC_init(int init_thread, size_t max_size, size_t init_size)
 {
-    if (!async_thread_local_init())
+    if (!async_global_init())
         return 0;
 
     if (init_thread)
@@ -349,6 +353,10 @@ int ASYNC_init_thread(size_t max_size, size_t init_size)
         return 0;
     }
 
+    if (!async_local_init()) {
+        ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_INIT_FAILED);
+        return 0;
+    }
     pool = OPENSSL_zalloc(sizeof *pool);
     if (pool == NULL) {
         ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ERR_R_MALLOC_FAILURE);
@@ -365,25 +373,22 @@ int ASYNC_init_thread(size_t max_size, size_t init_size)
     pool->max_size = max_size;
 
     /* Pre-create jobs as required */
-    while (init_size) {
+    while (init_size--) {
         ASYNC_JOB *job;
         job = async_job_new();
-        if (job) {
-            async_fibre_makecontext(&job->fibrectx);
-            job->funcargs = NULL;
-            sk_ASYNC_JOB_push(pool->jobs, job);
-            curr_size++;
-            init_size--;
-        } else {
+        if (job == NULL || !async_fibre_makecontext(&job->fibrectx)) {
             /*
-             * Not actually fatal because we already created the pool, just skip
-             * creation of any more jobs
+             * Not actually fatal because we already created the pool, just
+             * skip creation of any more jobs
              */
-            init_size = 0;
+            async_job_free(job);
+            break;
         }
+        job->funcargs = NULL;
+        sk_ASYNC_JOB_push(pool->jobs, job);
+        curr_size++;
     }
     pool->curr_size = curr_size;
-
     if (!async_set_pool(pool)) {
         ASYNCerr(ASYNC_F_ASYNC_INIT_THREAD, ASYNC_R_FAILED_TO_SET_POOL);
         goto err;
@@ -404,6 +409,7 @@ static void async_free_pool_internal(async_pool *pool)
     sk_ASYNC_JOB_free(pool->jobs);
     OPENSSL_free(pool);
     (void)async_set_pool(NULL);
+    async_local_cleanup();
     async_ctx_free();
 }