From 5276e8f27e3021d5da3d3055caed6f9a1d964c93 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Wed, 8 Aug 2018 21:36:28 +0300 Subject: multiple files: calloc -> malloc xlators/cluster/stripe/src/stripe-helpers.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/dht/src/tier.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/dht/src/dht-layout.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/dht/src/dht-helper.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/dht/src/dht-common.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/afr/src/afr.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible xlators/cluster/afr/src/afr-inode-read.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible tests/bugs/replicate/bug-1250170-fsync.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible tests/basic/gfapi/gfapi-async-calls-test.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible tests/basic/ec/ec-fast-fgetxattr.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible rpc/xdr/src/glusterfs3.h: Move to GF_MALLOC() instead of GF_CALLOC() when possible rpc/rpc-transport/socket/src/socket.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible rpc/rpc-lib/src/rpc-clnt.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible extras/geo-rep/gsync-sync-gfid.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-xml-output.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-rpc-ops.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-cmd-volume.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-cmd-system.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-cmd-snapshot.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-cmd-peer.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible cli/src/cli-cmd-global.c: Move to GF_MALLOC() instead of GF_CALLOC() when possible It doesn't make sense to calloc (allocate and clear) memory when the code right away fills that memory with data. It may be optimized by the compiler, or have a microscopic performance improvement. In some cases, also changed allocation size to be sizeof some struct or type instead of a pointer - easier to read. In some cases, removed redundant strlen() calls by saving the result into a variable. 1. Only done for the straightforward cases. There's room for improvement. 2. Please review carefully, especially for string allocation, with the terminating NULL string. Only compile-tested! updates: bz#1193929 Original-Author: Yaniv Kaul Signed-off-by: Yaniv Kaul Signed-off-by: Amar Tumballi Change-Id: I16274dca4078a1d06ae09a0daf027d734b631ac2 --- cli/src/cli-cmd-global.c | 2 +- cli/src/cli-cmd-peer.c | 2 +- cli/src/cli-cmd-snapshot.c | 2 +- cli/src/cli-cmd-system.c | 2 +- cli/src/cli-cmd-volume.c | 14 ++++++++------ cli/src/cli-rpc-ops.c | 4 ++-- cli/src/cli-xml-output.c | 2 +- 7 files changed, 15 insertions(+), 13 deletions(-) (limited to 'cli/src') diff --git a/cli/src/cli-cmd-global.c b/cli/src/cli-cmd-global.c index fb2ecb1d3de..3c8ae227ca7 100644 --- a/cli/src/cli-cmd-global.c +++ b/cli/src/cli-cmd-global.c @@ -57,7 +57,7 @@ cli_cmd_global_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *global_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (global_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (global_cmds), cli_mt_cli_cmd); memcpy (cmd, global_cmds, sizeof (global_cmds)); count = (sizeof (global_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); diff --git a/cli/src/cli-cmd-peer.c b/cli/src/cli-cmd-peer.c index 7df60bcb2be..a50ea7c66b5 100644 --- a/cli/src/cli-cmd-peer.c +++ b/cli/src/cli-cmd-peer.c @@ -284,7 +284,7 @@ cli_cmd_peer_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, cli_out ("\ngluster peer commands"); cli_out ("======================\n"); - cmd = GF_CALLOC (1, sizeof (cli_probe_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (cli_probe_cmds), cli_mt_cli_cmd); memcpy (cmd, cli_probe_cmds, sizeof (cli_probe_cmds)); count = (sizeof (cli_probe_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); diff --git a/cli/src/cli-cmd-snapshot.c b/cli/src/cli-cmd-snapshot.c index 06e00c96b7e..64cbd42ce5a 100644 --- a/cli/src/cli-cmd-snapshot.c +++ b/cli/src/cli-cmd-snapshot.c @@ -131,7 +131,7 @@ cli_cmd_snapshot_help_cbk (struct cli_state *state, struct cli_cmd *snap_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (snapshot_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (snapshot_cmds), cli_mt_cli_cmd); memcpy (cmd, snapshot_cmds, sizeof (snapshot_cmds)); count = (sizeof (snapshot_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); diff --git a/cli/src/cli-cmd-system.c b/cli/src/cli-cmd-system.c index c41594bfa59..995e1b1ad5c 100644 --- a/cli/src/cli-cmd-system.c +++ b/cli/src/cli-cmd-system.c @@ -592,7 +592,7 @@ cli_cmd_system_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *system_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (cli_system_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (cli_system_cmds), cli_mt_cli_cmd); memcpy (cmd, cli_system_cmds, sizeof (cli_system_cmds)); count = (sizeof (cli_system_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c index b9fd56362cf..d8b9e25f198 100644 --- a/cli/src/cli-cmd-volume.c +++ b/cli/src/cli-cmd-volume.c @@ -897,6 +897,7 @@ cli_event_remove_brick_str (dict_t *options, char **event_str, int32_t i = 1; int32_t count = 0; int32_t eventstrlen = 1; + int bricklen = 0; char *tmp_ptr = NULL; if (!options || !event_str || !event) @@ -971,8 +972,9 @@ cli_event_remove_brick_str (dict_t *options, char **event_str, break; } snprintf (tmp_ptr, eventstrlen, "%s ", brick); - eventstrlen -= (strlen (brick) + 1); - tmp_ptr += (strlen (brick) + 1); + bricklen = strlen (brick); + eventstrlen -= (bricklen + 1); + tmp_ptr += (bricklen + 1); i++; } @@ -3504,7 +3506,7 @@ cli_cmd_quota_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *quota_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (quota_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (quota_cmds), cli_mt_cli_cmd); memcpy (cmd, quota_cmds, sizeof (quota_cmds)); count = (sizeof (quota_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); @@ -3531,7 +3533,7 @@ cli_cmd_bitrot_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *bitrot_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (bitrot_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (bitrot_cmds), cli_mt_cli_cmd); memcpy (cmd, bitrot_cmds, sizeof (bitrot_cmds)); count = (sizeof (bitrot_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); @@ -3558,7 +3560,7 @@ cli_cmd_tier_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *tier_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (tier_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (tier_cmds), cli_mt_cli_cmd); memcpy (cmd, tier_cmds, sizeof (tier_cmds)); count = (sizeof (tier_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); @@ -3584,7 +3586,7 @@ cli_cmd_volume_help_cbk (struct cli_state *state, struct cli_cmd_word *in_word, struct cli_cmd *vol_cmd = NULL; int count = 0; - cmd = GF_CALLOC (1, sizeof (volume_cmds), cli_mt_cli_cmd); + cmd = GF_MALLOC (sizeof (volume_cmds), cli_mt_cli_cmd); memcpy (cmd, volume_cmds, sizeof (volume_cmds)); count = (sizeof (volume_cmds) / sizeof (struct cli_cmd)); cli_cmd_sort (cmd, count); diff --git a/cli/src/cli-rpc-ops.c b/cli/src/cli-rpc-ops.c index 1e95836e4af..40bd64b983c 100644 --- a/cli/src/cli-rpc-ops.c +++ b/cli/src/cli-rpc-ops.c @@ -5848,7 +5848,7 @@ gf_cli_gsync_status_output (dict_t *dict, gf_boolean_t is_detail) /* gsync_count = number of nodes reporting output. each sts_val object will store output of each node */ - sts_vals = GF_CALLOC (gsync_count, sizeof (gf_gsync_status_t *), + sts_vals = GF_MALLOC (gsync_count * sizeof (gf_gsync_status_t *), gf_common_mt_char); if (!sts_vals) { ret = -1; @@ -8326,7 +8326,7 @@ xml_end: goto out; } - status.brick = GF_CALLOC (1, PATH_MAX + 256, gf_common_mt_strdup); + status.brick = GF_MALLOC (PATH_MAX + 256, gf_common_mt_strdup); if (!status.brick) { errno = ENOMEM; ret = -1; diff --git a/cli/src/cli-xml-output.c b/cli/src/cli-xml-output.c index dcb1715e625..fd42192162b 100644 --- a/cli/src/cli-xml-output.c +++ b/cli/src/cli-xml-output.c @@ -4138,7 +4138,7 @@ cli_xml_output_vol_gsync_status (dict_t *dict, if (ret) goto out; - status_values = GF_CALLOC (count, sizeof (gf_gsync_status_t *), + status_values = GF_MALLOC (count * sizeof (gf_gsync_status_t *), gf_common_mt_char); if (!status_values) { ret = -1; -- cgit