From d5fef2a50cf4e3458e800d59021a97c0b5fc39db Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Mon, 9 Dec 2019 23:17:16 +0200 Subject: dht-hashfn.c: remove a strlen() We already have the length of the name, or when we munge it, we can return the length of it instead of strlen() again. Also, reduce a bit the code under the lock. Change-Id: I0141b0725ed1a4134d8d9f81ed1187b551b038b5 updates: bz#1193929 Signed-off-by: Yaniv Kaul --- xlators/cluster/dht/src/dht-hashfn.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'xlators/cluster/dht/src') diff --git a/xlators/cluster/dht/src/dht-hashfn.c b/xlators/cluster/dht/src/dht-hashfn.c index c1405edbdd4..1dfe2a77907 100644 --- a/xlators/cluster/dht/src/dht-hashfn.c +++ b/xlators/cluster/dht/src/dht-hashfn.c @@ -11,8 +11,9 @@ #include "dht-common.h" #include -int -dht_hash_compute_internal(int type, const char *name, uint32_t *hash_p) +static int +dht_hash_compute_internal(int type, const char *name, const int len, + uint32_t *hash_p) { int ret = 0; uint32_t hash = 0; @@ -20,7 +21,7 @@ dht_hash_compute_internal(int type, const char *name, uint32_t *hash_p) switch (type) { case DHT_HASH_TYPE_DM: case DHT_HASH_TYPE_DM_USER: - hash = gf_dm_hashfn(name, strlen(name)); + hash = gf_dm_hashfn(name, len); break; default: ret = -1; @@ -34,7 +35,12 @@ dht_hash_compute_internal(int type, const char *name, uint32_t *hash_p) return ret; } -static gf_boolean_t +/* The function returns: + * 0 : in case no munge took place + * >0 : the length (inc. terminating NULL!) of the newly modified string, + * if it was munged. + */ +static int dht_munge_name(const char *original, char *modified, size_t len, regex_t *re) { regmatch_t matches[2] = { @@ -52,14 +58,14 @@ dht_munge_name(const char *original, char *modified, size_t len, regex_t *re) if (new_len < len) { memcpy(modified, original + matches[1].rm_so, new_len); modified[new_len] = '\0'; - return _gf_true; + return new_len + 1; /* +1 for the terminating NULL */ } } } /* This is guaranteed safe because of how the dest was allocated. */ strcpy(modified, original); - return _gf_false; + return 0; } int @@ -68,7 +74,7 @@ dht_hash_compute(xlator_t *this, int type, const char *name, uint32_t *hash_p) char *rsync_friendly_name = NULL; dht_conf_t *priv = NULL; size_t len = 0; - gf_boolean_t munged = _gf_false; + int munged = 0; priv = this->private; @@ -86,19 +92,16 @@ dht_hash_compute(xlator_t *this, int type, const char *name, uint32_t *hash_p) gf_msg_trace(this->name, 0, "trying regex for %s", name); munged = dht_munge_name(name, rsync_friendly_name, len, &priv->rsync_regex); - if (munged) { - UNLOCK(&priv->lock); - gf_msg_debug(this->name, 0, "munged down to %s", - rsync_friendly_name); - goto post_unlock; - } } } UNLOCK(&priv->lock); -post_unlock: - if (!munged) { + if (munged) { + gf_msg_debug(this->name, 0, "munged down to %s", rsync_friendly_name); + len = munged; + } else { rsync_friendly_name = (char *)name; } - return dht_hash_compute_internal(type, rsync_friendly_name, hash_p); + return dht_hash_compute_internal(type, rsync_friendly_name, len - 1, + hash_p); } -- cgit