From 3c721721a66ef05042e2aa77e65f49a861bb0bb4 Mon Sep 17 00:00:00 2001 From: Ruben Kerkhof Date: Fri, 1 Apr 2016 18:31:11 +0200 Subject: [PATCH] utils: malloc + memset -> calloc --- src/utils_db_query.c | 17 ++++++----------- src/utils_fbhash.c | 3 +-- src/utils_latency.c | 3 +-- src/utils_mount.c | 21 ++++++++------------- src/utils_rrdcreate.c | 13 +++++-------- src/utils_vl_lookup.c | 22 +++++++++------------- 6 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/utils_db_query.c b/src/utils_db_query.c index de435cb4..74c3a81f 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -550,13 +550,12 @@ static int udb_result_create (const char *query_name, /* {{{ */ ci->values_num, (ci->values_num == 1) ? "" : "s"); } - r = malloc (sizeof (*r)); + r = calloc (1, sizeof (*r)); if (r == NULL) { - ERROR ("db query utils: malloc failed."); + ERROR ("db query utils: calloc failed."); return (-1); } - memset (r, 0, sizeof (*r)); r->type = NULL; r->instance_prefix = NULL; r->instances = NULL; @@ -678,13 +677,12 @@ int udb_query_create (udb_query_t ***ret_query_list, /* {{{ */ return (-1); } - q = malloc (sizeof (*q)); + q = calloc (1, sizeof (*q)); if (q == NULL) { - ERROR ("db query utils: malloc failed."); + ERROR ("db query utils: calloc failed."); return (-1); } - memset (q, 0, sizeof (*q)); q->min_version = 0; q->max_version = UINT_MAX; @@ -1072,17 +1070,16 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */ udb_result_preparation_area_t **next_r_area; udb_result_t *r; - q_area = malloc (sizeof (*q_area)); + q_area = calloc (1, sizeof (*q_area)); if (q_area == NULL) return NULL; - memset (q_area, 0, sizeof (*q_area)); next_r_area = &q_area->result_prep_areas; for (r = q->results; r != NULL; r = r->next) { udb_result_preparation_area_t *r_area; - r_area = malloc (sizeof (*r_area)); + r_area = calloc (1, sizeof (*r_area)); if (r_area == NULL) { udb_result_preparation_area_t *a = q_area->result_prep_areas; @@ -1098,8 +1095,6 @@ udb_query_allocate_preparation_area (udb_query_t *q) /* {{{ */ return NULL; } - memset (r_area, 0, sizeof (*r_area)); - *next_r_area = r_area; next_r_area = &r_area->next; } diff --git a/src/utils_fbhash.c b/src/utils_fbhash.c index cbd15061..f27c0827 100644 --- a/src/utils_fbhash.c +++ b/src/utils_fbhash.c @@ -209,10 +209,9 @@ fbhash_t *fbh_create (const char *file) /* {{{ */ if (file == NULL) return (NULL); - h = malloc (sizeof (*h)); + h = calloc (1, sizeof (*h)); if (h == NULL) return (NULL); - memset (h, 0, sizeof (*h)); h->filename = strdup (file); if (h->filename == NULL) diff --git a/src/utils_latency.c b/src/utils_latency.c index ca7d5fab..41af00cb 100644 --- a/src/utils_latency.c +++ b/src/utils_latency.c @@ -121,10 +121,9 @@ latency_counter_t *latency_counter_create (void) /* {{{ */ { latency_counter_t *lc; - lc = malloc (sizeof (*lc)); + lc = calloc (1, sizeof (*lc)); if (lc == NULL) return (NULL); - memset (lc, 0, sizeof (*lc)); latency_counter_reset (lc); lc->bin_width = HISTOGRAM_DEFAULT_BIN_WIDTH; diff --git a/src/utils_mount.c b/src/utils_mount.c index ecf011d8..a36882c4 100644 --- a/src/utils_mount.c +++ b/src/utils_mount.c @@ -467,9 +467,8 @@ static cu_mount_t *cu_mount_getfsstat (void) return (NULL); } - if ((buf = malloc (bufsize * sizeof (*buf))) == NULL) + if ((buf = calloc (bufsize, sizeof (*buf))) == NULL) return (NULL); - memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS)); /* The bufsize needs to be passed in bytes. Really. This is not in the * manpage.. -octo */ @@ -486,10 +485,9 @@ static cu_mount_t *cu_mount_getfsstat (void) for (i = 0; i < num; i++) { - if ((new = malloc (sizeof (*new))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mnttab' */ new->dir = sstrdup (buf[i].f_mntonname); new->spec_device = sstrdup (buf[i].f_mntfromname); @@ -540,10 +538,9 @@ static cu_mount_t *cu_mount_gen_getmntent (void) while (getmntent (fp, &mt) == 0) { - if ((new = malloc (sizeof (*new))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mnttab' */ new->dir = sstrdup (mt.mnt_mountp); new->spec_device = sstrdup (mt.mnt_special); @@ -598,9 +595,8 @@ static cu_mount_t *cu_mount_getmntent (void) while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) )) { - if ((new = malloc (sizeof (*new))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); /* Copy values from `struct mntent *' */ new->dir = sstrdup (me.mnt_dir); @@ -655,10 +651,9 @@ static cu_mount_t *cu_mount_getmntent (void) while ((me = getmntent (fp)) != NULL) { - if ((new = malloc (sizeof (*new))) == NULL) + if ((new = calloc (1, sizeof (*new))) == NULL) break; - memset (new, '\0', sizeof (cu_mount_t)); - + /* Copy values from `struct mntent *' */ new->dir = sstrdup (me->mnt_dir); new->spec_device = sstrdup (me->mnt_fsname); diff --git a/src/utils_rrdcreate.c b/src/utils_rrdcreate.c index 44c2386f..fb04201c 100644 --- a/src/utils_rrdcreate.c +++ b/src/utils_rrdcreate.c @@ -113,13 +113,12 @@ static srrd_create_args_t *srrd_create_args_create (const char *filename, { srrd_create_args_t *args; - args = malloc (sizeof (*args)); + args = calloc (1, sizeof (*args)); if (args == NULL) { - ERROR ("srrd_create_args_create: malloc failed."); + ERROR ("srrd_create_args_create: calloc failed."); return (NULL); } - memset (args, 0, sizeof (*args)); args->filename = NULL; args->pdp_step = pdp_step; args->last_up = last_up; @@ -216,9 +215,8 @@ static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */ rra_max = rts_num * rra_types_num; assert (rra_max > 0); - if ((rra_def = malloc ((rra_max + 1) * sizeof (*rra_def))) == NULL) + if ((rra_def = calloc (rra_max + 1, sizeof (*rra_def))) == NULL) return (-1); - memset (rra_def, 0, (rra_max + 1) * sizeof (*rra_def)); rra_num = 0; cdp_len = 0; @@ -292,15 +290,14 @@ static int ds_get (char ***ret, /* {{{ */ assert (ds->ds_num > 0); - ds_def = malloc (ds->ds_num * sizeof (*ds_def)); + ds_def = calloc (ds->ds_num, sizeof (*ds_def)); if (ds_def == NULL) { char errbuf[1024]; - ERROR ("rrdtool plugin: malloc failed: %s", + ERROR ("rrdtool plugin: calloc failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } - memset (ds_def, 0, ds->ds_num * sizeof (*ds_def)); for (ds_num = 0; ds_num < ds->ds_num; ds_num++) { diff --git a/src/utils_vl_lookup.c b/src/utils_vl_lookup.c index f85910e1..d3eeab3c 100644 --- a/src/utils_vl_lookup.c +++ b/src/utils_vl_lookup.c @@ -204,13 +204,12 @@ static void *lu_create_user_obj (lookup_t *obj, /* {{{ */ { user_obj_t *user_obj; - user_obj = malloc (sizeof (*user_obj)); + user_obj = calloc (1, sizeof (*user_obj)); if (user_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (user_obj, 0, sizeof (*user_obj)); user_obj->next = NULL; user_obj->user_obj = obj->cb_user_class (ds, vl, user_class->user_class); @@ -373,16 +372,15 @@ static by_type_entry_t *lu_search_by_type (lookup_t *obj, /* {{{ */ return (NULL); } - by_type = malloc (sizeof (*by_type)); + by_type = calloc (1, sizeof (*by_type)); if (by_type == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); sfree (type_copy); return (NULL); } - memset (by_type, 0, sizeof (*by_type)); by_type->wildcard_plugin_list = NULL; - + by_type->by_plugin_tree = c_avl_create ((void *) strcmp); if (by_type->by_plugin_tree == NULL) { @@ -544,13 +542,12 @@ lookup_t *lookup_create (lookup_class_callback_t cb_user_class, /* {{{ */ lookup_free_class_callback_t cb_free_class, lookup_free_obj_callback_t cb_free_obj) { - lookup_t *obj = malloc (sizeof (*obj)); + lookup_t *obj = calloc (1, sizeof (*obj)); if (obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (NULL); } - memset (obj, 0, sizeof (*obj)); obj->by_type_tree = c_avl_create ((void *) strcmp); if (obj->by_type_tree == NULL) @@ -605,13 +602,12 @@ int lookup_add (lookup_t *obj, /* {{{ */ if (by_type == NULL) return (-1); - user_class_obj = malloc (sizeof (*user_class_obj)); + user_class_obj = calloc (1, sizeof (*user_class_obj)); if (user_class_obj == NULL) { - ERROR ("utils_vl_lookup: malloc failed."); + ERROR ("utils_vl_lookup: calloc failed."); return (ENOMEM); } - memset (user_class_obj, 0, sizeof (*user_class_obj)); pthread_mutex_init (&user_class_obj->entry.lock, /* attr = */ NULL); user_class_obj->entry.user_class = user_class; lu_copy_ident_to_match (&user_class_obj->entry.match, ident, group_by); -- 2.30.2