From: Florian Forster Date: Thu, 4 Oct 2012 08:16:56 +0000 (+0200) Subject: Merge branch 'ym/utils_cache_bugfix' X-Git-Tag: collectd-5.2.0~47 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=2fb1d3a320cba6abe16fe1e1d22ba085c465b6a8;hp=d447fc0e1d185b2430be20322ea142a97ba4e2c2;p=collectd.git Merge branch 'ym/utils_cache_bugfix' --- diff --git a/src/utils_cache.c b/src/utils_cache.c index dd5bcb59..82567f33 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -572,6 +572,19 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) char **names = NULL; cdtime_t *times = NULL; size_t number = 0; + size_t size_arrays = 0; + + /* Increment size for the 2 arrays of values + * Because realloc is time consuming, it's better to + * realloc by blocks and not by units. + * To see the difference, set this value to 1. + * + * To change this value at compile time: + * ./configure CPPFLAGS="-DLISTVAL_INCREASE=102400" + */ +#ifndef LISTVAL_INCREASE +# define LISTVAL_INCREASE 1024 +#endif int status = 0; @@ -593,23 +606,28 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) { cdtime_t *tmp_times; - tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1)); - if (tmp_times == NULL) - { - status = -1; - break; + if(number <= size_arrays) { + tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (size_arrays + LISTVAL_INCREASE)); + if (tmp_times == NULL) + { + status = -1; + break; + } + times = tmp_times; } - times = tmp_times; times[number] = value->last_time; } - temp = (char **) realloc (names, sizeof (char *) * (number + 1)); - if (temp == NULL) - { - status = -1; - break; + if(number <= size_arrays) { + temp = (char **) realloc (names, sizeof (char *) * (size_arrays + LISTVAL_INCREASE)); + if (temp == NULL) + { + status = -1; + break; + } + names = temp; + size_arrays += LISTVAL_INCREASE; } - names = temp; names[number] = strdup (key); if (names[number] == NULL) {