From 68b25403404c54a53c3718db531f2d99c89cd81d Mon Sep 17 00:00:00 2001 From: Gymee <8039110+Gymee@user.noreply.gitee.com> Date: Wed, 16 Sep 2020 02:37:12 +0800 Subject: [PATCH] tweaks code for optimization --- kal/timer/src/kal.c | 7 ++-- kv_store/src/kvstore_impl_hal/kv_store.c | 37 +++++++++------------- kv_store/src/kvstore_impl_posix/kv_store.c | 23 ++++++-------- 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/kal/timer/src/kal.c b/kal/timer/src/kal.c index 2ee83c4..7693e67 100755 --- a/kal/timer/src/kal.c +++ b/kal/timer/src/kal.c @@ -143,12 +143,9 @@ KalErrCode KalTimerDelete(KalTimerId timerId) } KalTimer* tmpPtr = (KalTimer *)timerId; int ret = timer_delete(tmpPtr->timerPtr); - if (ret != 0) { - free(timerId); - return KAL_ERR_INNER; - } free(timerId); - return KAL_OK; + timerId = NULL; + return (ret != 0) ? KAL_ERR_INNER : KAL_OK; } unsigned int KalTimerIsRunning(KalTimerId timerId) diff --git a/kv_store/src/kvstore_impl_hal/kv_store.c b/kv_store/src/kvstore_impl_hal/kv_store.c index 976dbd4..3711e37 100755 --- a/kv_store/src/kvstore_impl_hal/kv_store.c +++ b/kv_store/src/kvstore_impl_hal/kv_store.c @@ -40,11 +40,12 @@ static int GetValueByFile(const char* key, char* value, unsigned int len) if (fd < 0) { return EC_FAILURE; } - if (UtilsFileRead(fd, value, valueLen) < 0) { - UtilsFileClose(fd); + int ret = UtilsFileRead(fd, value, valueLen); + UtilsFileClose(fd); + fd = -1; + if (ret < 0) { return EC_FAILURE; } - UtilsFileClose(fd); value[valueLen] = '\0'; return valueLen; } @@ -55,12 +56,10 @@ static int SetValueToFile(const char* key, const char* value) if (fd < 0) { return EC_FAILURE; } - if (UtilsFileWrite(fd, value, strlen(value)) < 0) { - UtilsFileClose(fd); - return EC_FAILURE; - } + int ret = UtilsFileWrite(fd, value, strlen(value); UtilsFileClose(fd); - return EC_SUCCESS; + fd = -1; + return (ret < 0) ? EC_FAILURE : EC_SUCCESS; } static int GetCurrentItem(void) @@ -70,13 +69,10 @@ static int GetCurrentItem(void) return 0; } char value[KV_SUM_INDEX] = {0}; - if (UtilsFileRead(fd, value, KV_SUM_INDEX) < 0) { - UtilsFileClose(fd); - return 0; - } + int ret = UtilsFileRead(fd, value, KV_SUM_INDEX); UtilsFileClose(fd); - int sum = atoi(value); - return sum; + fd = -1; + return (ret < 0) ? 0 : atoi(value); } static int SetCurrentItem(const int num) @@ -89,12 +85,11 @@ static int SetCurrentItem(const int num) if (fd < 0) { return EC_FAILURE; } - if (UtilsFileWrite(fd, value, KV_SUM_INDEX) < 0) { - UtilsFileClose(fd); - return EC_FAILURE; - } + + int ret = UtilsFileWrite(fd, value, KV_SUM_INDEX); UtilsFileClose(fd); - return EC_SUCCESS; + fd = -1; + return (ret < 0) ? EC_FAILURE : EC_SUCCESS; } static boolean NewItem(const char* key) @@ -159,10 +154,8 @@ int UtilsDeleteValue(const char* key) DeleteKVCache(key); #endif int ret = UtilsFileDelete(key); - int currentNum = GetCurrentItem(); if (ret == EC_SUCCESS) { - currentNum--; - ret = SetCurrentItem(currentNum); + ret = SetCurrentItem(GetCurrentItem() - 1); } return ret; } diff --git a/kv_store/src/kvstore_impl_posix/kv_store.c b/kv_store/src/kvstore_impl_posix/kv_store.c index bfa7cdc..9a3bf99 100755 --- a/kv_store/src/kvstore_impl_posix/kv_store.c +++ b/kv_store/src/kvstore_impl_posix/kv_store.c @@ -86,12 +86,12 @@ static int GetValueByFile(const char* dataPath, const char* key, char* value, un if (fd < 0) { return EC_FAILURE; } - if (read(fd, value, info.st_size) < 0) { - close(fd); - return EC_FAILURE; - } + int ret = read(fd, value, info.st_size); close(fd); fd = -1; + if (ret < 0) { + return EC_FAILURE; + } value[info.st_size] = '\0'; return info.st_size; } @@ -112,12 +112,10 @@ static int SetValueToFile(const char* dataPath, const char* key, const char* val if (fd < 0) { return EC_FAILURE; } - if (write(fd, value, strlen(value)) < 0) { - close(fd); - return EC_FAILURE; - } + int ret = write(fd, value, strlen(value); close(fd); - return EC_SUCCESS; + fd = -1; + return (ret < 0) ? EC_FAILURE : EC_SUCCESS; } static int DeleteValueFromFile(const char* dataPath, const char* key) @@ -317,10 +315,7 @@ int UtilsSetEnv(const char* path) return EC_FAILURE; } pthread_mutex_lock(&g_kvGlobalMutex); - if (strcpy_s(g_dataPath, MAX_KEY_PATH + 1, path) != EOK) { - pthread_mutex_unlock(&g_kvGlobalMutex); - return EC_FAILURE; - } + int ret = strcpy_s(g_dataPath, MAX_KEY_PATH + 1, path); pthread_mutex_unlock(&g_kvGlobalMutex); - return EC_SUCCESS; + return (ret != EOK) ? EC_FAILURE : EC_SUCCESS; } \ No newline at end of file -- Gitee