[PORT FROM R2] atomisp: optimize atomisp_acc_get_fw/index() to satisfy klockwork
authorDavid Cohen <david.a.cohen@intel.com>
Mon, 7 Nov 2011 17:50:47 +0000 (19:50 +0200)
committerbuildbot <buildbot@intel.com>
Mon, 19 Dec 2011 13:28:04 +0000 (05:28 -0800)
BZ: 17272

Due to a (not well optimized) logic Klockwork doesn't understand, it is
complaining about possible memory leak. This patch changes/optimizes
this logic to make Klockwork happier.

Change-Id: Ife10eb9f0aec2a164b93f28d29ae0a72c62e7bc6
Orig-Change-Id: Ie303427f4d16486f268940745b73692fa5d781f8
Signed-off-by: David Cohen <david.a.cohen@intel.com>
Reviewed-on: http://android.intel.com:8080/23490
Reviewed-by: Wang, Wen W <wen.w.wang@intel.com>
Reviewed-by: Zhang, Xiaolin <xiaolin.zhang@intel.com>
Reviewed-by: Hu, Gang A <gang.a.hu@intel.com>
Reviewed-by: Koskinen, Ilkka <ilkka.koskinen@intel.com>
Tested-by: Koski, Anttu <anttu.koski@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
Reviewed-on: http://android.intel.com:8080/27993
Reviewed-by: Tuominen, TeemuX <teemux.tuominen@intel.com>
Reviewed-by: Koski, Anttu <anttu.koski@intel.com>
drivers/media/video/atomisp/atomisp_cmd.c

index e286ea4..dd96039 100644 (file)
@@ -3211,19 +3211,24 @@ int atomisp_set_fmt_file(struct video_device *vdev, struct v4l2_format *f)
  *
  * This function will search for given handle until:
  * - Handle is found
- * - Reached end of isp->acc_fw[] array
  * - Function has found total number of non-NULL slots
+ *
+ * It's ensured by atomisp_acc_fw_alloc() number of used slots is never bigger
+ * then ATOMISP_ACC_FW_MAX.
  */
 static struct sh_css_acc_fw *
 atomisp_acc_get_fw(struct atomisp_device *isp, unsigned int handle)
 {
-       int i, count;
-
-       if (isp->acc_fw_count == 0)
-               return NULL;
+       int i = -1;
+       int count = isp->acc_fw_count;
 
-       for (i = 0, count = isp->acc_fw_count;
-            count > 0 && i < ATOMISP_ACC_FW_MAX; i++) {
+       while (count) {
+               i++;
+               if (unlikely(i == ATOMISP_ACC_FW_MAX)) {
+                       /* Sanity check. If code reaches here, we've a bug. */
+                       WARN_ON(1);
+                       return NULL;
+               }
                if (isp->acc_fw[i] == NULL)
                        continue;
                if (isp->acc_fw[i]->header.handle == handle)
@@ -3231,7 +3236,7 @@ atomisp_acc_get_fw(struct atomisp_device *isp, unsigned int handle)
                count--;
        }
 
-       return i < ATOMISP_ACC_FW_MAX || count > 0 ? isp->acc_fw[i] : NULL;
+       return count ? isp->acc_fw[i] : NULL;
 }
 
 /*
@@ -3239,26 +3244,32 @@ atomisp_acc_get_fw(struct atomisp_device *isp, unsigned int handle)
  *
  * This function will search for firmware index until:
  * - Given firmware is found
- * - Reached end of isp->acc_fw[] array
  * - Function has searched all non-NULL slots
+ *
+ * It's ensured by atomisp_acc_fw_alloc() number of used slots is never bigger
+ * then ATOMISP_ACC_FW_MAX.
  */
 static int
 atomisp_acc_get_index(struct atomisp_device *isp, struct sh_css_acc_fw *fw)
 {
-       int i, count;
-
-       if (isp->acc_fw_count == 0)
-               return -EINVAL;
+       int i = -1;
+       int count = isp->acc_fw_count;
 
-       for (i = 0, count = isp->acc_fw_count;
-            count > 0 && i < ATOMISP_ACC_FW_MAX; i++) {
+       while (count) {
+               i++;
+               if (unlikely(i == ATOMISP_ACC_FW_MAX)) {
+                       /* Sanity check. If code reaches here, we've a bug. */
+                       WARN_ON(1);
+                       return -EINVAL;
+               }
+               if (isp->acc_fw[i] == NULL)
+                       continue;
                if (isp->acc_fw[i] == fw)
                        break;
-               if (isp->acc_fw[i] != NULL)
-                       count--;
+               count--;
        }
 
-       return i < ATOMISP_ACC_FW_MAX || count > 0 ? i : -EINVAL;
+       return count ? i : -EINVAL;
 }
 
 static void