summaryrefslogtreecommitdiffstats
path: root/xlators
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2018-08-08 21:36:28 +0300
committerAmar Tumballi <amarts@redhat.com>2018-09-04 05:09:09 +0000
commit5276e8f27e3021d5da3d3055caed6f9a1d964c93 (patch)
tree23f6a1fedc8d5bf4b752eab2ae5a451c57a80fa0 /xlators
parent81cbbfd1d870bea49b8aafe7bebb9e8251190918 (diff)
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
Diffstat (limited to 'xlators')
-rw-r--r--xlators/cluster/afr/src/afr-inode-read.c24
-rw-r--r--xlators/cluster/afr/src/afr.c4
-rw-r--r--xlators/cluster/dht/src/dht-common.c30
-rw-r--r--xlators/cluster/dht/src/dht-helper.c21
-rw-r--r--xlators/cluster/dht/src/dht-layout.c4
-rw-r--r--xlators/cluster/dht/src/tier.c6
-rw-r--r--xlators/cluster/stripe/src/stripe-helpers.c2
7 files changed, 50 insertions, 41 deletions
diff --git a/xlators/cluster/afr/src/afr-inode-read.c b/xlators/cluster/afr/src/afr-inode-read.c
index 2675af2bf99..d0b07e9064f 100644
--- a/xlators/cluster/afr/src/afr-inode-read.c
+++ b/xlators/cluster/afr/src/afr-inode-read.c
@@ -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;
diff --git a/xlators/cluster/afr/src/afr.c b/xlators/cluster/afr/src/afr.c
index dac714d5d2c..bde9a97a4e7 100644
--- a/xlators/cluster/afr/src/afr.c
+++ b/xlators/cluster/afr/src/afr.c
@@ -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) {
diff --git a/xlators/cluster/dht/src/dht-common.c b/xlators/cluster/dht/src/dht-common.c
index 8a7b4bf5ff5..c7c7fbf22ba 100644
--- a/xlators/cluster/dht/src/dht-common.c
+++ b/xlators/cluster/dht/src/dht-common.c
@@ -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;
diff --git a/xlators/cluster/dht/src/dht-helper.c b/xlators/cluster/dht/src/dht-helper.c
index fc67a45c7ce..8bd3f564221 100644
--- a/xlators/cluster/dht/src/dht-helper.c
+++ b/xlators/cluster/dht/src/dht-helper.c
@@ -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:
diff --git a/xlators/cluster/dht/src/dht-layout.c b/xlators/cluster/dht/src/dht-layout.c
index 97b98e01451..6d9d4e693ff 100644
--- a/xlators/cluster/dht/src/dht-layout.c
+++ b/xlators/cluster/dht/src/dht-layout.c
@@ -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;
diff --git a/xlators/cluster/dht/src/tier.c b/xlators/cluster/dht/src/tier.c
index 13ce7f4fb2b..63eb65be302 100644
--- a/xlators/cluster/dht/src/tier.c
+++ b/xlators/cluster/dht/src/tier.c
@@ -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,
diff --git a/xlators/cluster/stripe/src/stripe-helpers.c b/xlators/cluster/stripe/src/stripe-helpers.c
index 18f70fe6d7e..71ab608118f 100644
--- a/xlators/cluster/stripe/src/stripe-helpers.c
+++ b/xlators/cluster/stripe/src/stripe-helpers.c
@@ -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;