From e6dcaa276f295a5baa6ea5109a943d2299ade757 Mon Sep 17 00:00:00 2001 From: Zhangfei Gao Date: Tue, 6 Jun 2023 05:06:32 +0000 Subject: [PATCH 2/7] uadk_engine: use calloc to replace malloc+memset malloc + memset(0) can be replaced by calloc Signed-off-by: Zhangfei Gao --- src/uadk_async.c | 5 +---- src/uadk_ecx.c | 6 ++---- src/uadk_sm2.c | 4 +--- src/v1/alg/dh/hpre_dh_wd.c | 3 +-- src/v1/async/async_task_queue.c | 4 +--- src/v1/utils/engine_log.c | 6 ++---- src/v1/wdmngr/wd_alg_queue.c | 6 ++---- src/v1/wdmngr/wd_queue_memory.c | 11 ++++------- 8 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/uadk_async.c b/src/uadk_async.c index f21eabb..86aa9e5 100644 --- a/src/uadk_async.c +++ b/src/uadk_async.c @@ -362,13 +362,10 @@ int async_module_init(void) if (pthread_mutex_init(&(poll_queue.async_task_mutex), NULL) < 0) return 0; - poll_queue.head = malloc(sizeof(struct async_poll_task) * ASYNC_QUEUE_TASK_NUM); + poll_queue.head = calloc(ASYNC_QUEUE_TASK_NUM, sizeof(struct async_poll_task)); if (poll_queue.head == NULL) return 0; - memset(poll_queue.head, 0, - sizeof(struct async_poll_task) * ASYNC_QUEUE_TASK_NUM); - if (sem_init(&poll_queue.empty_sem, 0, ASYNC_QUEUE_TASK_NUM) != 0) goto err; diff --git a/src/uadk_ecx.c b/src/uadk_ecx.c index 2824a0d..0537890 100644 --- a/src/uadk_ecx.c +++ b/src/uadk_ecx.c @@ -89,12 +89,11 @@ static int x25519_init(EVP_PKEY_CTX *ctx) return UADK_E_FAIL; } - x25519_ctx = malloc(sizeof(struct ecx_ctx)); + x25519_ctx = calloc(1, sizeof(struct ecx_ctx)); if (!x25519_ctx) { fprintf(stderr, "failed to alloc x25519 ctx\n"); return UADK_E_FAIL; } - memset(x25519_ctx, 0, sizeof(struct ecx_ctx)); setup.alg = "x25519"; setup.key_bits = X25519_KEYBITS; @@ -146,13 +145,12 @@ static int x448_init(EVP_PKEY_CTX *ctx) return UADK_E_FAIL; } - x448_ctx = malloc(sizeof(struct ecx_ctx)); + x448_ctx = calloc(1, sizeof(struct ecx_ctx)); if (!x448_ctx) { fprintf(stderr, "failed to alloc x448 ctx\n"); return UADK_E_FAIL; } - memset(x448_ctx, 0, sizeof(struct ecx_ctx)); setup.alg = "x448"; setup.key_bits = X448_KEYBITS; params.numa_id = uadk_e_ecc_get_numa_id(); diff --git a/src/uadk_sm2.c b/src/uadk_sm2.c index 01186ef..63d4fdf 100644 --- a/src/uadk_sm2.c +++ b/src/uadk_sm2.c @@ -1157,14 +1157,12 @@ static int sm2_init(EVP_PKEY_CTX *ctx) struct sm2_ctx *smctx; int ret; - smctx = malloc(sizeof(*smctx)); + smctx = calloc(1, sizeof(*smctx)); if (!smctx) { fprintf(stderr, "failed to alloc sm2 ctx\n"); return 0; } - memset(smctx, 0, sizeof(*smctx)); - ret = uadk_e_ecc_get_support_state(SM2_SUPPORT); if (!ret) { fprintf(stderr, "sm2 is not supported\n"); diff --git a/src/v1/alg/dh/hpre_dh_wd.c b/src/v1/alg/dh/hpre_dh_wd.c index 38a3d09..8dbb9b4 100644 --- a/src/v1/alg/dh/hpre_dh_wd.c +++ b/src/v1/alg/dh/hpre_dh_wd.c @@ -265,12 +265,11 @@ static hpre_dh_engine_ctx_t *hpre_dh_new_eng_ctx(DH *alg) { hpre_dh_engine_ctx_t *eng_ctx = NULL; - eng_ctx = (hpre_dh_engine_ctx_t *)OPENSSL_malloc(sizeof(hpre_dh_engine_ctx_t)); + eng_ctx = (hpre_dh_engine_ctx_t *)OPENSSL_calloc(1, sizeof(hpre_dh_engine_ctx_t)); if (eng_ctx == NULL) { US_ERR("hpre engine_ctx malloc fail"); return NULL; } - kae_memset(eng_ctx, 0, sizeof(hpre_dh_engine_ctx_t)); eng_ctx->priv_ctx.ssl_alg = alg; eng_ctx->qlist = kae_get_node_from_pool(g_hpre_dh_qnode_pool); diff --git a/src/v1/async/async_task_queue.c b/src/v1/async/async_task_queue.c index 2feea7e..0ef1e84 100644 --- a/src/v1/async/async_task_queue.c +++ b/src/v1/async/async_task_queue.c @@ -60,13 +60,11 @@ int async_poll_task_init_v1(void) kae_memset(&g_async_poll_queue, 0, sizeof(g_async_poll_queue)); g_async_poll_queue.async_poll_task_queue_head = - (async_poll_task *)malloc(sizeof(async_poll_task) * ASYNC_POLL_TASK_NUM); + (async_poll_task *)calloc(ASYNC_POLL_TASK_NUM, sizeof(async_poll_task)); if (g_async_poll_queue.async_poll_task_queue_head == NULL) { US_ERR("no enough memory for task queue, errno=%d", errno); //lint !e666 return 0; } - kae_memset(g_async_poll_queue.async_poll_task_queue_head, 0, - sizeof(async_poll_task) * ASYNC_POLL_TASK_NUM); g_async_poll_queue.left_task = ASYNC_POLL_TASK_NUM; ret = sem_init(&g_async_poll_queue.empty_sem, 0, (unsigned int)g_async_poll_queue.left_task); diff --git a/src/v1/utils/engine_log.c b/src/v1/utils/engine_log.c index 6e32b97..ef009e5 100644 --- a/src/v1/utils/engine_log.c +++ b/src/v1/utils/engine_log.c @@ -60,12 +60,10 @@ static void kae_set_conf_debuglevel(void) if (conf_path == NULL || strlen(conf_path) > MAX_CONFIG_LEN) goto err; - file_path = (char *)kae_malloc(strlen(conf_path) + strlen(filename) + 1); - debuglev = (char *)kae_malloc(MAX_LEVEL_LEN); + file_path = (char *)kae_calloc(1, strlen(conf_path) + strlen(filename) + 1); + debuglev = (char *)kae_calloc(1, MAX_LEVEL_LEN); if (!file_path || !debuglev) goto err; - memset(debuglev, 0, MAX_LEVEL_LEN); - memset(file_path, 0, sizeof(conf_path) + sizeof(filename) + 1); strcat(file_path, conf_path); strcat(file_path, filename); ret = kae_drv_get_item(file_path, "LogSection", "debug_level", debuglev); diff --git a/src/v1/wdmngr/wd_alg_queue.c b/src/v1/wdmngr/wd_alg_queue.c index 5cd33ae..098231f 100644 --- a/src/v1/wdmngr/wd_alg_queue.c +++ b/src/v1/wdmngr/wd_alg_queue.c @@ -21,16 +21,14 @@ struct wd_queue *wd_new_queue(int algtype) { - struct wd_queue *queue = (struct wd_queue *)kae_malloc(sizeof(struct wd_queue)); int ret; - + struct wd_queue *queue = (struct wd_queue *)kae_calloc(1, + sizeof(struct wd_queue)); if (queue == NULL) { US_ERR("malloc failed"); return NULL; } - kae_memset(queue, 0, sizeof(struct wd_queue)); - switch (algtype) { case WCRYPTO_RSA: queue->capa.alg = "rsa"; diff --git a/src/v1/wdmngr/wd_queue_memory.c b/src/v1/wdmngr/wd_queue_memory.c index e6e7a2c..5f28357 100644 --- a/src/v1/wdmngr/wd_queue_memory.c +++ b/src/v1/wdmngr/wd_queue_memory.c @@ -62,13 +62,12 @@ struct wd_queue_mempool *wd_queue_mempool_create(struct wd_queue *q, unsigned in kae_memset(addr, 0, rsv_mm_sz); bitmap_sz = (block_num / BLOCKS_PER_BITMAP + 1) * sizeof(unsigned int); - pool = - (struct wd_queue_mempool *)kae_malloc(sizeof(struct wd_queue_mempool) + bitmap_sz); + pool = (struct wd_queue_mempool *)kae_calloc(1, + sizeof(struct wd_queue_mempool) + bitmap_sz); if (pool == NULL) { US_ERR("Alloc pool handle fail!"); return NULL; } - kae_memset(pool, 0, sizeof(struct wd_queue_mempool) + bitmap_sz); pool->base = addr; sem_init(&pool->mempool_sem, 0, 1); @@ -190,13 +189,12 @@ KAE_QUEUE_POOL_HEAD_S *kae_init_queue_pool(int algtype) /* malloc a pool */ kae_pool->kae_queue_pool = (KAE_QUEUE_POOL_NODE_S *) - kae_malloc(KAE_QUEUE_POOL_MAX_SIZE * sizeof(KAE_QUEUE_POOL_NODE_S)); + kae_calloc(KAE_QUEUE_POOL_MAX_SIZE, sizeof(KAE_QUEUE_POOL_NODE_S)); if (kae_pool->kae_queue_pool == NULL) { US_ERR("malloc failed"); kae_free(kae_pool); return NULL; } - kae_memset(kae_pool->kae_queue_pool, 0, KAE_QUEUE_POOL_MAX_SIZE * sizeof(KAE_QUEUE_POOL_NODE_S)); pthread_mutex_init(&kae_pool->kae_queue_mutex, NULL); pthread_mutex_init(&kae_pool->destroy_mutex, NULL); @@ -271,12 +269,11 @@ static KAE_QUEUE_DATA_NODE_S *kae_new_wd_queue_memory(int algtype) { KAE_QUEUE_DATA_NODE_S *queue_node = NULL; - queue_node = (KAE_QUEUE_DATA_NODE_S *)kae_malloc(sizeof(KAE_QUEUE_DATA_NODE_S)); + queue_node = (KAE_QUEUE_DATA_NODE_S *)kae_calloc(1, sizeof(KAE_QUEUE_DATA_NODE_S)); if (queue_node == NULL) { US_ERR("malloc failed"); return NULL; } - kae_memset(queue_node, 0, sizeof(KAE_QUEUE_DATA_NODE_S)); queue_node->kae_wd_queue = wd_new_queue(algtype); if (queue_node->kae_wd_queue == NULL) { -- 2.25.1