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 <ykaul@redhat.com>
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Signed-off-by: Amar Tumballi <amarts@redhat.com>

Change-Id: I16274dca4078a1d06ae09a0daf027d734b631ac2
This commit is contained in:
Yaniv Kaul 2018-08-08 21:36:28 +03:00 committed by Amar Tumballi
parent 81cbbfd1d8
commit 5276e8f27e
21 changed files with 77 additions and 64 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -80,7 +80,7 @@ main (int argc, char *argv[])
/* gfid + '\0' + bname + '\0' */
len = UUID_CANONICAL_FORM_LEN + 1 + strlen (bname) + 1;
blob = calloc (1, len);
blob = malloc (len);
memcpy (blob, gfid, UUID_CANONICAL_FORM_LEN);

View File

@ -1592,7 +1592,7 @@ rpcclnt_cbk_program_register (struct rpc_clnt *clnt,
goto out;
}
tmp = GF_CALLOC (1, sizeof (*tmp),
tmp = GF_MALLOC (sizeof (*tmp),
gf_common_mt_rpcclnt_cb_program_t);
if (tmp == NULL) {
goto out;

View File

@ -4522,7 +4522,7 @@ socket_init (rpc_transport_t *this)
return -1;
}
priv = GF_CALLOC (1, sizeof (*priv), gf_common_mt_socket_private_t);
priv = GF_MALLOC (sizeof (*priv), gf_common_mt_socket_private_t);
if (!priv) {
return -1;
}

View File

@ -812,7 +812,7 @@ xdr_to_dict (gfx_dict *dict, dict_t **to)
xpair->value.gfx_value_u.value_dbl);
break;
case GF_DATA_TYPE_STR:
value = GF_CALLOC (1, xpair->value.gfx_value_u.val_string.val_string_len + 1,
value = GF_MALLOC (xpair->value.gfx_value_u.val_string.val_string_len + 1,
gf_common_mt_char);
if (!value) {
errno = ENOMEM;
@ -820,11 +820,12 @@ xdr_to_dict (gfx_dict *dict, dict_t **to)
}
memcpy (value, xpair->value.gfx_value_u.val_string.val_string_val,
xpair->value.gfx_value_u.val_string.val_string_len);
value[xpair->value.gfx_value_u.val_string.val_string_len] = '\0';
free (xpair->value.gfx_value_u.val_string.val_string_val);
ret = dict_set_dynstr (this, key, value);
break;
case GF_DATA_TYPE_GFUUID:
uuid = GF_CALLOC (1, sizeof (uuid_t), gf_common_mt_uuid_t);
uuid = GF_MALLOC (sizeof (uuid_t), gf_common_mt_uuid_t);
if (!uuid) {
errno = ENOMEM;
goto out;
@ -842,7 +843,7 @@ xdr_to_dict (gfx_dict *dict, dict_t **to)
ret = dict_set_iatt (this, key, iatt, false);
break;
case GF_DATA_TYPE_PTR:
value = GF_CALLOC (1, xpair->value.gfx_value_u.other.other_len + 1,
value = GF_MALLOC (xpair->value.gfx_value_u.other.other_len + 1,
gf_common_mt_char);
if (!value) {
errno = ENOMEM;
@ -850,6 +851,7 @@ xdr_to_dict (gfx_dict *dict, dict_t **to)
}
memcpy (value, xpair->value.gfx_value_u.other.other_val,
xpair->value.gfx_value_u.other.other_len);
value[xpair->value.gfx_value_u.other.other_len] = '\0';
free (xpair->value.gfx_value_u.other.other_val);
ret = dict_set_dynptr (this, key, value,
xpair->value.gfx_value_u.other.other_len);

View File

@ -16,7 +16,7 @@ fill_iov (struct iovec *iov, char fillchar, int count)
{
int ret = -1;
iov->iov_base = calloc (count + 1, sizeof(fillchar));
iov->iov_base = malloc (count + 1);
if (iov->iov_base == NULL) {
return ret;
} else {

View File

@ -21,7 +21,7 @@ fill_iov (struct iovec *iov, char fillchar, int count)
{
int ret = -1;
iov->iov_base = calloc (count + 1, sizeof(fillchar));
iov->iov_base = malloc (count + 1);
if (iov->iov_base == NULL) {
return ret;
} else {

View File

@ -26,9 +26,9 @@ int main (int argc, char **argv)
file = argv[1];
buf_size = 1024;
buffer = calloc(1, buf_size);
buffer = malloc(buf_size);
if (!buffer) {
perror("calloc");
perror("malloc");
return -1;
}
memset (buffer, 'R', buf_size);

View File

@ -1186,7 +1186,7 @@ afr_fgetxattr_pathinfo_cbk (call_frame_t *frame, void *cookie,
xattr = gf_strdup (xattr);
(void)snprintf (xattr_cky, 1024, "%s-%ld",
(void)snprintf (xattr_cky, sizeof(xattr_cky), "%s-%ld",
local->cont.getxattr.name, cky);
ret = dict_set_dynstr (local->dict,
xattr_cky, xattr);
@ -1217,20 +1217,21 @@ unlock:
+ SLEN (AFR_PATHINFO_HEADER) + 4;
local->cont.getxattr.xattr_len += (padding + 2);
xattr_serz = GF_CALLOC (local->cont.getxattr.xattr_len,
sizeof (char), gf_common_mt_char);
xattr_serz = GF_MALLOC (local->cont.getxattr.xattr_len,
gf_common_mt_char);
if (!xattr_serz)
goto unwind;
/* the xlator info */
(void) sprintf (xattr_serz, "(<"AFR_PATHINFO_HEADER"%s> ",
this->name);
int xattr_serz_len = sprintf (xattr_serz,
"(<"AFR_PATHINFO_HEADER"%s> ",
this->name);
/* actual series of pathinfo */
ret = dict_serialize_value_with_delim (local->dict,
xattr_serz
+ strlen (xattr_serz),
+ xattr_serz_len,
&tlen, ' ');
if (ret) {
goto unwind;
@ -1342,19 +1343,20 @@ unlock:
padding += strlen (this->name) + SLEN (AFR_PATHINFO_HEADER) + 4;
local->cont.getxattr.xattr_len += (padding + 2);
xattr_serz = GF_CALLOC (local->cont.getxattr.xattr_len,
sizeof (char), gf_common_mt_char);
xattr_serz = GF_MALLOC (local->cont.getxattr.xattr_len,
gf_common_mt_char);
if (!xattr_serz)
goto unwind;
/* the xlator info */
(void) sprintf (xattr_serz, "(<"AFR_PATHINFO_HEADER"%s> ",
this->name);
int xattr_serz_len = sprintf (xattr_serz,
"(<"AFR_PATHINFO_HEADER"%s> ",
this->name);
/* actual series of pathinfo */
ret = dict_serialize_value_with_delim (local->dict,
xattr_serz + strlen (xattr_serz),
xattr_serz + xattr_serz_len,
&tlen, ' ');
if (ret) {
goto unwind;

View File

@ -542,8 +542,8 @@ init (xlator_t *this)
priv->child_up = GF_CALLOC (sizeof (unsigned char), child_count,
gf_afr_mt_char);
priv->child_latency = GF_CALLOC (sizeof (*priv->child_latency),
child_count,
priv->child_latency = GF_MALLOC (sizeof (*priv->child_latency)
* child_count,
gf_afr_mt_child_latency_t);
if (!priv->child_up || !priv->child_latency) {

View File

@ -4335,23 +4335,28 @@ fill_layout_info (dht_layout_t *layout, char *buf)
}
}
void
static void
dht_fill_pathinfo_xattr (xlator_t *this, dht_local_t *local,
char *xattr_buf, int32_t alloc_len,
int flag, char *layout_buf)
{
if (flag && local->xattr_val)
snprintf (xattr_buf, alloc_len,
if (flag) {
if (local->xattr_val) {
snprintf (xattr_buf, alloc_len,
"((<"DHT_PATHINFO_HEADER"%s> %s) (%s-layout %s))",
this->name, local->xattr_val, this->name,
layout_buf);
else if (local->xattr_val)
} else {
snprintf (xattr_buf, alloc_len, "(%s-layout %s)",
this->name, layout_buf);
}
} else if (local->xattr_val) {
snprintf (xattr_buf, alloc_len,
"(<"DHT_PATHINFO_HEADER"%s> %s)",
this->name, local->xattr_val);
else if (flag)
snprintf (xattr_buf, alloc_len, "(%s-layout %s)",
this->name, layout_buf);
} else {
xattr_buf[0] = '\0';
}
}
int
@ -4360,7 +4365,6 @@ dht_vgetxattr_alloc_and_fill (dht_local_t *local, dict_t *xattr, xlator_t *this,
{
int ret = -1;
char *value = NULL;
int32_t plen = 0;
ret = dict_get_str (xattr, local->xsel, &value);
if (ret) {
@ -4375,16 +4379,17 @@ dht_vgetxattr_alloc_and_fill (dht_local_t *local, dict_t *xattr, xlator_t *this,
local->alloc_len += strlen(value);
if (!local->xattr_val) {
local->alloc_len += (strlen (DHT_PATHINFO_HEADER) + 10);
local->xattr_val = GF_CALLOC (local->alloc_len, sizeof (char),
local->alloc_len += sizeof (DHT_PATHINFO_HEADER) + 10;
local->xattr_val = GF_MALLOC (local->alloc_len,
gf_common_mt_char);
if (!local->xattr_val) {
ret = -1;
goto out;
}
local->xattr_val[0] = '\0';
}
plen = strlen (local->xattr_val);
int plen = strlen (local->xattr_val);
if (plen) {
/* extra byte(s) for \0 to be safe */
local->alloc_len += (plen + 2);
@ -4436,8 +4441,7 @@ dht_vgetxattr_fill_and_set (dht_local_t *local, dict_t **dict, xlator_t *this,
local->alloc_len += (2 * strlen (this->name))
+ strlen (layout_buf)
+ 40;
xattr_buf = GF_CALLOC (local->alloc_len, sizeof (char),
gf_common_mt_char);
xattr_buf = GF_MALLOC (local->alloc_len, gf_common_mt_char);
if (!xattr_buf)
goto out;

View File

@ -622,6 +622,9 @@ dht_filter_loc_subvol_key (xlator_t *this, loc_t *loc, loc_t *new_loc,
xlator_list_t *trav = NULL;
char key[1024] = {0,};
int ret = 0; /* not found */
int keylen = 0;
int name_len = 0;
int path_len = 0;
/* Why do other tasks if first required 'char' itself is not there */
if (!new_loc || !loc || !loc->name || !strchr (loc->name, '@')) {
@ -631,24 +634,24 @@ dht_filter_loc_subvol_key (xlator_t *this, loc_t *loc, loc_t *new_loc,
trav = this->children;
while (trav) {
snprintf (key, sizeof (key), "*@%s:%s", this->name, trav->xlator->name);
keylen = snprintf (key, sizeof (key), "*@%s:%s", this->name, trav->xlator->name);
if (fnmatch (key, loc->name, FNM_NOESCAPE) == 0) {
new_name = GF_CALLOC(strlen (loc->name),
sizeof (char),
name_len = strlen (loc->name);
new_name = GF_MALLOC(name_len,
gf_common_mt_char);
if (!new_name)
goto out;
if (fnmatch (key, loc->path, FNM_NOESCAPE) == 0) {
new_path = GF_CALLOC(strlen (loc->path),
sizeof (char),
path_len = strlen (loc->path);
new_path = GF_MALLOC(path_len,
gf_common_mt_char);
if (!new_path)
goto out;
strncpy (new_path, loc->path, (strlen (loc->path) -
strlen (key) + 1));
strncpy (new_path, loc->path, (path_len -
keylen + 1));
}
strncpy (new_name, loc->name, (strlen (loc->name) -
strlen (key) + 1));
strncpy (new_name, loc->name, (name_len -
keylen + 1));
if (new_loc) {
new_loc->path = ((new_path) ? new_path:

View File

@ -201,8 +201,8 @@ dht_layouts_init (xlator_t *this, dht_conf_t *conf)
if (!conf)
goto out;
conf->file_layouts = GF_CALLOC (conf->subvolume_cnt,
sizeof (dht_layout_t *),
conf->file_layouts = GF_MALLOC (conf->subvolume_cnt
* sizeof (dht_layout_t *),
gf_dht_mt_dht_layout_t);
if (!conf->file_layouts) {
goto out;

View File

@ -74,8 +74,8 @@ qfile_array_new (ssize_t array_size)
goto out;
}
qfile_array->fd_array = GF_CALLOC (array_size, sizeof (int),
gf_dht_mt_int32_t);
qfile_array->fd_array = GF_MALLOC (array_size * sizeof (int),
gf_dht_mt_int32_t);
if (!qfile_array->fd_array) {
gf_msg ("tier", GF_LOG_ERROR, 0, DHT_MSG_LOG_TIER_ERROR,
"Failed to allocate memory for "
@ -2157,7 +2157,7 @@ tier_get_bricklist (xlator_t *xl, struct list_head *local_bricklist_head)
brickname);
local_brick->brick_db_path =
GF_CALLOC (PATH_MAX, 1, gf_common_mt_char);
GF_MALLOC (PATH_MAX, gf_common_mt_char);
if (!local_brick->brick_db_path) {
gf_msg ("tier", GF_LOG_ERROR, 0,
DHT_MSG_LOG_TIER_STATUS,

View File

@ -269,7 +269,7 @@ stripe_fill_pathinfo_xattr (xlator_t *this, stripe_local_t *local,
+ strlen (stripe_size_str) + 7;
local->xattr_total_len += (padding + 2);
pathinfo_serz = GF_CALLOC (local->xattr_total_len, sizeof (char),
pathinfo_serz = GF_MALLOC (local->xattr_total_len,
gf_common_mt_char);
if (!pathinfo_serz)
goto out;