From: Florian Forster Date: Wed, 27 Feb 2008 11:20:42 +0000 (+0100) Subject: src/utils_cache.c: Fix SPARC alignment problems. X-Git-Tag: collectd-4.3.1~7 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=06af487fe7030f8fb7edbc9776b16d10c6d6c34b;p=collectd.git src/utils_cache.c: Fix SPARC alignment problems. --- diff --git a/src/utils_cache.c b/src/utils_cache.c index 0d6961e7..9a9e9be7 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -56,6 +56,43 @@ static int cache_compare (const cache_entry_t *a, const cache_entry_t *b) 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; @@ -206,7 +243,7 @@ int uc_check_timeout (void) 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 */ { @@ -312,8 +349,6 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) 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); @@ -324,22 +359,15 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) 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++) {