summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1c57a24)
raw | patch | inline | side by side (parent: 1c57a24)
author | Florian Forster <octo@noris.net> | |
Wed, 27 Feb 2008 11:20:42 +0000 (12:20 +0100) | ||
committer | Florian Forster <octo@noris.net> | |
Wed, 27 Feb 2008 11:20:42 +0000 (12:20 +0100) |
src/utils_cache.c | patch | blob | history |
diff --git a/src/utils_cache.c b/src/utils_cache.c
index 0d6961e7372a1d6d949e1d0ca45520616ff7f0ad..9a9e9be711327ba7d58da27d2c0eec2a4272a4ee 100644 (file)
--- a/src/utils_cache.c
+++ b/src/utils_cache.c
return (strcmp (a->name, b->name));
} /* int cache_compare */
+static cache_entry_t *cache_alloc (int values_num)
+{
+ cache_entry_t *ce;
+
+ ce = (cache_entry_t *) malloc (sizeof (cache_entry_t));
+ if (ce == NULL)
+ {
+ ERROR ("utils_cache: cache_alloc: malloc failed.");
+ return (NULL);
+ }
+ memset (ce, '\0', sizeof (cache_entry_t));
+ ce->values_num = values_num;
+
+ ce->values_gauge = (gauge_t *) calloc (values_num, sizeof (gauge_t));
+ ce->values_counter = (counter_t *) calloc (values_num, sizeof (counter_t));
+ if ((ce->values_gauge == NULL) || (ce->values_counter == NULL))
+ {
+ sfree (ce->values_gauge);
+ sfree (ce->values_counter);
+ sfree (ce);
+ ERROR ("utils_cache: cache_alloc: calloc failed.");
+ return (NULL);
+ }
+
+ return (ce);
+} /* cache_entry_t *cache_alloc */
+
+static void cache_free (cache_entry_t *ce)
+{
+ if (ce == NULL)
+ return;
+
+ sfree (ce->values_gauge);
+ sfree (ce->values_counter);
+ sfree (ce);
+} /* void cache_free */
+
static int uc_send_notification (const char *name)
{
cache_entry_t *ce = NULL;
ERROR ("uc_check_timeout: c_avl_remove (%s) failed.", keys[i]);
}
sfree (keys[i]);
- sfree (ce);
+ cache_free (ce);
}
else /* (status > 0); ``service'' is interesting */
{
else /* key is not found */
{
int i;
- size_t ce_size = sizeof (cache_entry_t)
- + ds->ds_num * (sizeof (counter_t) + sizeof (gauge_t));
char *key;
key = strdup (name);
return (-1);
}
- ce = (cache_entry_t *) malloc (ce_size);
+ ce = cache_alloc (ds->ds_num);
if (ce == NULL)
{
pthread_mutex_unlock (&cache_lock);
- ERROR ("uc_insert: malloc (%u) failed.", (unsigned int) ce_size);
+ ERROR ("uc_insert: cache_alloc (%i) failed.", ds->ds_num);
return (-1);
}
- memset (ce, '\0', ce_size);
-
- strncpy (ce->name, name, sizeof (ce->name));
- ce->name[sizeof (ce->name) - 1] = '\0';
-
- ce->values_num = ds->ds_num;
- ce->values_gauge = (gauge_t *) (ce + 1);
- ce->values_counter = (counter_t *) (ce->values_gauge + ce->values_num);
+ sstrncpy (ce->name, name, sizeof (ce->name));
for (i = 0; i < ds->ds_num; i++)
{