diff options
author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-17 22:29:05 +0300 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2018-08-27 13:15:05 +0000 |
commit | b76a84be697074e4132120200320671d87f815ae (patch) | |
tree | fb85afef0306622e0e70851ef0f61f0e4451a91d /libglusterfs/src/dict.c | |
parent | c416327b31d4a5dad619007c13c3e9a06e3cd398 (diff) |
libglusterfs/src/dict.{c,h}: Introduce dict_setn, dict_addn, dict_addn and others.
They all take as a parameter the key length, instead of strlen() it.
In most cases, we know the key length, we just never bothered to save and pass it along.
(We most likely sprintf'ed it earlier and the return value could have been used).
A more interesting addition is dict_set_nstrn() [horrible name. Ideas are welcome].
It accepts both the string length and the key length and avoids strlen() both.
Some of it can be calculated on compile-time, btw.
For example:
dict_set_str (dict, "key", "all");
Should become:
dict_set_nstrn (dict, "key", sizeof ("key"), "all", sizeof ("all"));
Compile-tested only!
Change-Id: Ic2667f445f6c2e22e279505f5ad435788b4b668c
updates: bz#1193929
Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'libglusterfs/src/dict.c')
-rw-r--r-- | libglusterfs/src/dict.c | 146 |
1 files changed, 140 insertions, 6 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index 0554ee20d79..57be4e76d63 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -491,6 +491,16 @@ dict_set (dict_t *this, char *key, data_t *value) { + const int keylen = strlen(key); + return dict_setn(this, key, keylen, value); +} + +int32_t +dict_setn (dict_t *this, + char *key, + const int keylen, + data_t *value) +{ int32_t ret; uint32_t key_hash = 0; @@ -502,7 +512,7 @@ dict_set (dict_t *this, } if (key) { - key_hash = SuperFastHash (key, strlen(key)); + key_hash = SuperFastHash (key, keylen); } LOCK (&this->lock); @@ -518,6 +528,13 @@ dict_set (dict_t *this, int32_t dict_add (dict_t *this, char *key, data_t *value) { + const int keylen = strlen (key); + return dict_addn(this, key, keylen, value); +} + +int32_t +dict_addn (dict_t *this, char *key, const int keylen, data_t *value) +{ int32_t ret; uint32_t key_hash = 0; @@ -529,7 +546,7 @@ dict_add (dict_t *this, char *key, data_t *value) } if (key) { - key_hash = SuperFastHash (key, strlen(key)); + key_hash = SuperFastHash (key, keylen); } LOCK (&this->lock); @@ -545,6 +562,13 @@ dict_add (dict_t *this, char *key, data_t *value) data_t * dict_get (dict_t *this, char *key) { + const int keylen = strlen(key); + return dict_getn(this, key, keylen); +} + +data_t * +dict_getn (dict_t *this, char *key, const int keylen) +{ data_pair_t *pair; uint32_t hash; @@ -555,7 +579,7 @@ dict_get (dict_t *this, char *key) return NULL; } - hash = SuperFastHash (key, strlen (key)); + hash = SuperFastHash (key, keylen); LOCK (&this->lock); { @@ -592,6 +616,13 @@ dict_key_count (dict_t *this) void dict_del (dict_t *this, char *key) { + const int keylen = strlen(key); + return dict_deln(this, key, keylen); +} + +void +dict_deln (dict_t *this, char *key, const int keylen) +{ int hashval = 0; uint32_t hash; @@ -601,7 +632,7 @@ dict_del (dict_t *this, char *key) return; } - hash = SuperFastHash (key, strlen (key)); + hash = SuperFastHash (key, keylen); LOCK (&this->lock); @@ -1002,6 +1033,28 @@ str_to_data (char *value) } data_t * +strn_to_data (char *value, const int vallen) +{ + if (!value) { + gf_msg_callingfn ("dict", GF_LOG_WARNING, EINVAL, + LG_MSG_INVALID_ARG, "value is NULL"); + return NULL; + } + data_t *data = get_new_data (); + + if (!data) { + return NULL; + } + data->len = vallen + 1; + data->data_type = GF_DATA_TYPE_STR; + + data->data = value; + data->is_static = 1; + + return data; +} + +static data_t * data_from_dynstr (char *value) { if (!value) { @@ -1507,6 +1560,13 @@ fail: int dict_get_with_ref (dict_t *this, char *key, data_t **data) { + const int keylen = strlen(key); + return dict_get_with_refn(this, key, keylen, data); +} + +int +dict_get_with_refn (dict_t *this, char *key, const int keylen, data_t **data) +{ data_pair_t * pair = NULL; int ret = -ENOENT; uint32_t hash; @@ -1519,7 +1579,7 @@ dict_get_with_ref (dict_t *this, char *key, data_t **data) goto err; } - hash = SuperFastHash (key, strlen (key)); + hash = SuperFastHash (key, keylen); LOCK (&this->lock); { @@ -2465,6 +2525,31 @@ err: return ret; } +/* Get string - with known key length */ +int +dict_get_strn (dict_t *this, char *key, const int keylen, char **str) +{ + data_t * data = NULL; + int ret = -EINVAL; + + if (!this || !key || !str) { + goto err; + } + ret = dict_get_with_refn (this, key, keylen, &data); + if (ret < 0) { + goto err; + } + + VALIDATE_DATA_AND_LOG(data, GF_DATA_TYPE_STR, key, -EINVAL); + + *str = data->data; + +err: + if (data) + data_unref (data); + + return ret; +} int dict_get_str (dict_t *this, char *key, char **str) @@ -2511,6 +2596,48 @@ err: return ret; } +/* Set string - with known key length */ +int +dict_set_strn (dict_t *this, char *key, const int keylen, char *str) +{ + data_t * data = NULL; + int ret = 0; + + data = str_to_data (str); + if (!data) { + ret = -EINVAL; + goto err; + } + + ret = dict_setn (this, key, keylen, data); + if (ret < 0) + data_destroy (data); + +err: + return ret; +} + +/* Set string - with known key length and known value length */ +int +dict_set_nstrn (dict_t *this, char *key, const int keylen, char *str, const int vallen) +{ + data_t * data = NULL; + int ret = 0; + + data = strn_to_data (str, vallen); + if (!data) { + ret = -EINVAL; + goto err; + } + + ret = dict_setn (this, key, keylen, data); + if (ret < 0) + data_destroy (data); + +err: + return ret; +} + int dict_set_dynstr_with_alloc (dict_t *this, char *key, const char *str) { @@ -2531,6 +2658,13 @@ dict_set_dynstr_with_alloc (dict_t *this, char *key, const char *str) int dict_set_dynstr (dict_t *this, char *key, char *str) { + const int keylen = strlen(key); + return dict_set_dynstrn(this, key, keylen, str); +} + +int +dict_set_dynstrn (dict_t *this, char *key, const int keylen, char *str) +{ data_t * data = NULL; int ret = 0; @@ -2540,7 +2674,7 @@ dict_set_dynstr (dict_t *this, char *key, char *str) goto err; } - ret = dict_set (this, key, data); + ret = dict_setn (this, key, keylen, data); if (ret < 0) data_destroy (data); |