From 3b201e8cfc8306e84dc51cbeccd86510e9c0da50 Mon Sep 17 00:00:00 2001
From: Wenkai Lin <linwenkai6@hisilicon.com>
Date: Thu, 10 Mar 2022 20:03:17 +0800
Subject: [PATCH 093/109] uadk: v1: clean code for wd

1. get_int_attr should return INT_MAX instead of
INT_MAX_SIZE.
2. qinfo should be set to NULL if memory is freed,
q_info memory should be freed at last.
3. optimize is_alg_support

Signed-off-by: Wenkai Lin <linwenkai6@hisilicon.com>
---
 v1/wd.c | 23 ++++++++++++++++-------
 v1/wd.h |  1 -
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/v1/wd.c b/v1/wd.c
index 26f6692..39c4167 100644
--- a/v1/wd.c
+++ b/v1/wd.c
@@ -27,6 +27,7 @@
 #include <assert.h>
 #include <dirent.h>
 #include <sys/poll.h>
+#include <limits.h>
 
 #include "v1/wd.h"
 #include "v1/wd_util.h"
@@ -118,8 +119,10 @@ static int get_int_attr(struct dev_info *dinfo, const char *attr)
 	 * When the value is bigger than INT_MAX, it returns INT_MAX
 	 */
 	size = get_raw_attr(dinfo->dev_root, attr, buf, MAX_ATTR_STR_SIZE);
-	if (size < 0 || size >= INT_MAX_SIZE)
+	if (size < 0)
 		return size;
+	else if (size >= INT_MAX_SIZE)
+		return INT_MAX;
 	/* Handing the read string's end tails '\n' to '\0' */
 	buf[size] = '\0';
 	return atoi((char *)buf);
@@ -186,19 +189,23 @@ static int get_ul_vec_attr(struct dev_info *dinfo, const char *attr,
 	return 0;
 }
 
-static int is_alg_support(struct dev_info *dinfo, const char *alg)
+static bool is_alg_support(struct dev_info *dinfo, const char *alg)
 {
-	int alg_support_flag = 0;
 	char *alg_save = NULL;
 	char *alg_tmp;
 
+	if (!alg)
+		return false;
+
 	alg_tmp = strtok_r(dinfo->algs, "\n", &alg_save);
 	while (alg_tmp != NULL) {
-		if (alg && !strcmp(alg_tmp, alg))
-			alg_support_flag++;
+		if (!strcmp(alg_tmp, alg))
+			return true;
+
 		alg_tmp = strtok_r(NULL, "\n", &alg_save);
 	}
-	return  alg_support_flag;
+
+	return false;
 }
 
 static bool is_weight_more(unsigned int new, unsigned int old)
@@ -279,7 +286,7 @@ static int get_str_attr_all(struct dev_info *dinfo, const char *alg)
 
 	/* Add algorithm check to cut later pointless logic */
 	ret = is_alg_support(dinfo, alg);
-	if (ret == 0)
+	if (!ret)
 		return -EPFNOSUPPORT;
 
 	ret = get_str_attr(dinfo, "api", dinfo->api, WD_NAME_SIZE);
@@ -605,6 +612,7 @@ err_with_fd:
 	wd_close_queue(q);
 err_with_dev:
 	free(dinfop);
+	q->qinfo = NULL;
 	return ret;
 }
 
@@ -647,6 +655,7 @@ void wd_release_queue(struct wd_queue *q)
 
 	wd_close_queue(q);
 	free((void *)qinfo->dev_info);
+	q->qinfo = NULL;
 }
 
 int wd_send(struct wd_queue *q, void *req)
diff --git a/v1/wd.h b/v1/wd.h
index 3dd69eb..429c6b6 100644
--- a/v1/wd.h
+++ b/v1/wd.h
@@ -28,7 +28,6 @@
 #include <fcntl.h>
 #include <stdint.h>
 #include <unistd.h>
-#include <limits.h>
 #include <assert.h>
 #include "uacce.h"
 
-- 
2.27.0

