* collectd - src/utils_cache.c
* Copyright (C) 2007-2010 Florian octo Forster
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
*
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
*
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
*
- * Author:
+ * Authors:
* Florian octo Forster <octo at collectd.org>
**/
static int cache_compare (const cache_entry_t *a, const cache_entry_t *b)
{
+#if COLLECT_DEBUG
assert ((a != NULL) && (b != NULL));
+#endif
return (strcmp (a->name, b->name));
} /* int cache_compare */
return (ret);
} /* gauge_t *uc_get_rate */
+size_t uc_get_size() {
+ size_t size_arrays = 0;
+
+ pthread_mutex_lock (&cache_lock);
+ size_arrays = (size_t) c_avl_size (cache_tree);
+ pthread_mutex_unlock (&cache_lock);
+
+ return (size_arrays);
+}
+
int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number)
{
c_avl_iterator_t *iter;
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.
- */
- #define size_increment 102400
-
int status = 0;
if ((ret_names == NULL) || (ret_number == NULL))
pthread_mutex_lock (&cache_lock);
+ size_arrays = (size_t) c_avl_size (cache_tree);
+ if (size_arrays < 1)
+ {
+ /* Handle the "no values" case here, to avoid the error message when
+ * calloc() returns NULL. */
+ pthread_mutex_unlock (&cache_lock);
+ return (0);
+ }
+
+ names = calloc (size_arrays, sizeof (*names));
+ times = calloc (size_arrays, sizeof (*times));
+ if ((names == NULL) || (times == NULL))
+ {
+ ERROR ("uc_get_names: calloc failed.");
+ sfree (names);
+ sfree (times);
+ pthread_mutex_unlock (&cache_lock);
+ return (ENOMEM);
+ }
+
iter = c_avl_get_iterator (cache_tree);
while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
{
- char **temp;
-
/* remove missing values when list values */
if (value->state == STATE_MISSING)
continue;
+ /* c_avl_size does not return a number smaller than the number of elements
+ * returned by c_avl_iterator_next. */
+ assert (number < size_arrays);
+
if (ret_times != NULL)
- {
- cdtime_t *tmp_times;
-
- if(number <= size_arrays) {
- tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (size_arrays + size_increment));
- if (tmp_times == NULL)
- {
- status = -1;
- break;
- }
- times = tmp_times;
- }
times[number] = value->last_time;
- }
- if(number <= size_arrays) {
- temp = (char **) realloc (names, sizeof (char *) * (size_arrays + size_increment));
- if (temp == NULL)
- {
- status = -1;
- break;
- }
- names = temp;
- size_arrays += size_increment;
- }
names[number] = strdup (key);
if (names[number] == NULL)
{
status = -1;
break;
}
+
number++;
} /* while (c_avl_iterator_next) */