[PATCH] rrd_lastupdate: make rrd_lastupdate_r parameters optimal

Frederik Kriewitz <[email protected]>
Newsgroups gmane.comp.db.rrdtool.devel
Message-ID <[email protected]>
This patch makes the parameters of rrd_lastupdate_r optimal.

It looks worse than it is. I just added a few != NULL ifs.

I'll make use of this in an upcoming patch.

_______________________________________________
rrd-developers mailing list
[email protected]
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers
rrd_lastupdate-make-rrd_lastupdate_r-parameters-optimal.patch (text/x-diff, 4.5 KB)
Index: src/rrd_lastupdate.c
===================================================================
--- src/rrd_lastupdate.c	(revision 2062)
+++ src/rrd_lastupdate.c	(working copy)
@@ -106,59 +106,81 @@
         return (-1);
     }
 
-    *ret_last_update = rrd.live_head->last_up;
-    *ret_ds_count = rrd.stat_head->ds_cnt;
-    *ret_ds_names = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
-    if (*ret_ds_names == NULL) {
-        rrd_set_error ("malloc fetch ret_ds_names array");
-        rrd_close (rrd_file);
-        rrd_free (&rrd);
-        return (-1);
+    if(ret_last_update != NULL)
+    {
+      *ret_last_update = rrd.live_head->last_up;
     }
-    memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
 
-    *ret_last_ds = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
-    if (*ret_last_ds == NULL) {
-        rrd_set_error ("malloc fetch ret_last_ds array");
-        free (*ret_ds_names);
-        *ret_ds_names = NULL;
-        rrd_close (rrd_file);
-        rrd_free (&rrd);
-        return (-1);
-    }
-    memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
+    if(ret_ds_count != NULL)
+    {
+      *ret_ds_count = rrd.stat_head->ds_cnt;
 
-    for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
-        (*ret_ds_names)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
-        (*ret_last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
+      if(ret_ds_names != NULL)
+      {
+        *ret_ds_names = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
+        if (*ret_ds_names == NULL) {
+            rrd_set_error ("malloc fetch ret_ds_names array");
+            rrd_close (rrd_file);
+            rrd_free (&rrd);
+            return (-1);
+        }
+        memset (*ret_ds_names, 0, rrd.stat_head->ds_cnt * sizeof(char *));
+      }
 
-        if (((*ret_ds_names)[i] == NULL) || ((*ret_last_ds)[i] == NULL))
-            break;
-    }
+      if(ret_last_ds != NULL)
+      {
+        *ret_last_ds = (char **) malloc (rrd.stat_head->ds_cnt * sizeof(char *));
+        if (*ret_last_ds == NULL) {
+            rrd_set_error ("malloc fetch ret_last_ds array");
+            free (*ret_ds_names);
+            *ret_ds_names = NULL;
+            rrd_close (rrd_file);
+            rrd_free (&rrd);
+            return (-1);
+        }
+        memset (*ret_last_ds, 0, rrd.stat_head->ds_cnt * sizeof(char *));
+      }
 
-    /* Check if all names and values could be copied and free everything if
-     * not. */
-    if (i < rrd.stat_head->ds_cnt) {
-        rrd_set_error ("sprintf_alloc failed");
-        for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
-            if ((*ret_ds_names)[i] != NULL)
-            {
-                free ((*ret_ds_names)[i]);
-                (*ret_ds_names)[i] = NULL;
-            }
-            if ((*ret_last_ds)[i] != NULL)
-            {
-                free ((*ret_last_ds)[i]);
-                (*ret_last_ds)[i] = NULL;
-            }
-        }
-        free (*ret_ds_names);
-        *ret_ds_names = NULL;
-        free (*ret_last_ds);
-        *ret_last_ds = NULL;
-        rrd_close (rrd_file);
-        rrd_free (&rrd);
-        return (-1);
+      for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
+          if(ret_ds_names != NULL)
+            (*ret_ds_names)[i] = sprintf_alloc("%s", rrd.ds_def[i].ds_nam);
+          if(ret_last_ds != NULL)
+            (*ret_last_ds)[i] = sprintf_alloc("%s", rrd.pdp_prep[i].last_ds);
+
+          if ((ret_ds_names != NULL && (*ret_ds_names)[i] == NULL) || (ret_last_ds != NULL && (*ret_last_ds)[i] == NULL))
+              break;
+      }
+
+      /* Check if all names and values could be copied and free everything if
+      * not. */
+      if (i < rrd.stat_head->ds_cnt) {
+          rrd_set_error ("sprintf_alloc failed");
+          for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
+              if (ret_ds_names != NULL && (*ret_ds_names)[i] != NULL)
+              {
+                  free ((*ret_ds_names)[i]);
+                  (*ret_ds_names)[i] = NULL;
+              }
+              if (ret_last_ds != NULL && (*ret_last_ds)[i] != NULL)
+              {
+                  free ((*ret_last_ds)[i]);
+                  (*ret_last_ds)[i] = NULL;
+              }
+          }
+          if(ret_ds_names != NULL)
+          {
+            free (*ret_ds_names);
+            *ret_ds_names = NULL;
+          }
+          if(ret_last_ds != NULL)
+          {
+            free (*ret_last_ds);
+            *ret_last_ds = NULL;
+          }
+          rrd_close (rrd_file);
+          rrd_free (&rrd);
+          return (-1);
+      }
     }
 
     rrd_free(&rrd);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.