summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7f90e30)
raw | patch | inline | side by side (parent: 7f90e30)
author | Florian Forster <octo@collectd.org> | |
Fri, 12 Jul 2013 16:19:29 +0000 (18:19 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Fri, 12 Jul 2013 16:19:29 +0000 (18:19 +0200) |
Faster version without snprintf().
src/rrdcached.c | patch | blob | history | |
src/rrdtool.c | patch | blob | history |
diff --git a/src/rrdcached.c b/src/rrdcached.c
index cc991816cc25ab2e920fa54f55b8a084fb7d2ffa..cf23803138dec2bb8ee59cc8a6819785a72c5fb8 100644 (file)
--- a/src/rrdcached.c
+++ b/src/rrdcached.c
return (0);
} /* int value_list_to_string */
-static int value_list_to_filename (char *buffer, int buffer_len,
- const data_set_t *ds, const value_list_t *vl)
+static int value_list_to_filename (char *buffer, size_t buffer_size,
+ value_list_t const *vl)
{
- int offset = 0;
+ char const suffix[] = ".rrd";
int status;
+ size_t len;
- assert (0 == strcmp (ds->type, vl->type));
-
- if (datadir != NULL)
- {
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", datadir);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
- }
-
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", vl->host);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
-
- if (strlen (vl->plugin_instance) > 0)
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s-%s/", vl->plugin, vl->plugin_instance);
- else
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", vl->plugin);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
+ status = FORMAT_VL (buffer, buffer_size, vl);
+ if (status != 0)
+ return (status);
- if (strlen (vl->type_instance) > 0)
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s-%s", vl->type, vl->type_instance);
- else
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s", vl->type);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
+ len = strlen (buffer);
+ assert (len < buffer_size);
+ buffer += len;
+ buffer_size -= len;
- strncpy (buffer + offset, ".rrd", buffer_len - offset);
- buffer[buffer_len - 1] = 0;
+ if (buffer_size <= sizeof (suffix))
+ return (ENOMEM);
+ memcpy (buffer, suffix, sizeof (suffix));
return (0);
} /* int value_list_to_filename */
return (-1);
}
- if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
+ if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
{
ERROR ("rrdcached plugin: value_list_to_filename failed.");
return (-1);
diff --git a/src/rrdtool.c b/src/rrdtool.c
index b1d13ee3c19ee279c836741739b521359872c63b..916ce01d65f03585341eab2fd0ab3cae70669dd4 100644 (file)
--- a/src/rrdtool.c
+++ b/src/rrdtool.c
return (0);
} /* int value_list_to_string */
-static int value_list_to_filename (char *buffer, int buffer_len,
- const data_set_t __attribute__((unused)) *ds, const value_list_t *vl)
+static int value_list_to_filename (char *buffer, size_t buffer_size,
+ value_list_t const *vl)
{
- int offset = 0;
+ char const suffix[] = ".rrd";
int status;
+ size_t len;
- if (datadir != NULL)
- {
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", datadir);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
- }
-
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", vl->host);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
+ status = FORMAT_VL (buffer, buffer_size, vl);
+ if (status != 0)
+ return (status);
- if (strlen (vl->plugin_instance) > 0)
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s-%s/", vl->plugin, vl->plugin_instance);
- else
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s/", vl->plugin);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
+ len = strlen (buffer);
+ assert (len < buffer_size);
+ buffer += len;
+ buffer_size -= len;
- if (strlen (vl->type_instance) > 0)
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s-%s.rrd", vl->type, vl->type_instance);
- else
- status = ssnprintf (buffer + offset, buffer_len - offset,
- "%s.rrd", vl->type);
- if ((status < 1) || (status >= buffer_len - offset))
- return (-1);
- offset += status;
+ if (buffer_size <= sizeof (suffix))
+ return (ENOMEM);
+ memcpy (buffer, suffix, sizeof (suffix));
return (0);
} /* int value_list_to_filename */
return -1;
}
- if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
+ if (value_list_to_filename (filename, sizeof (filename), vl) != 0)
return (-1);
if (value_list_to_string (values, sizeof (values), ds, vl) != 0)