summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/dict.c
diff options
context:
space:
mode:
authorYaniv Kaul <ykaul@redhat.com>2019-10-23 22:38:59 +0300
committerXavi Hernandez <xhernandez@redhat.com>2020-01-13 10:02:00 +0000
commita73baf4e50fd8468f726179d89ba90364c5b8329 (patch)
tree09e94182b1343a999fd068bb3ff3f1bdbe87feff /libglusterfs/src/dict.c
parent905db496aa622e421d807222dcc488488da9dbfe (diff)
dict: use the free_pair's key presence or NULL as a sign of use.
Instead of using a boolean parameter, we can use the key variable. If it's NULL, the pair is not used and can be used. Otherwise, it's in use - don't use. It saves this annoying boolean, which causes the compiler (or us explicitly) to pad with additional bytes the dict struct. Change-Id: I89f52db57f35b3ef8acf57b7de2cee37f5d18e06 updates: bz#1193929 Signed-off-by: Yaniv Kaul <ykaul@redhat.com>
Diffstat (limited to 'libglusterfs/src/dict.c')
-rw-r--r--libglusterfs/src/dict.c32
1 files changed, 15 insertions, 17 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c
index d786e557fff..2682e6afe05 100644
--- a/libglusterfs/src/dict.c
+++ b/libglusterfs/src/dict.c
@@ -97,6 +97,7 @@ get_new_dict_full(int size_hint)
}
}
+ dict->free_pair.key = NULL;
LOCK_INIT(&dict->lock);
return dict;
@@ -335,7 +336,7 @@ err_out:
* checked by callers.
*/
static data_pair_t *
-dict_lookup_common(dict_t *this, char *key, uint32_t hash)
+dict_lookup_common(const dict_t *this, const char *key, const uint32_t hash)
{
int hashval = 0;
data_pair_t *pair;
@@ -417,16 +418,15 @@ dict_set_lk(dict_t *this, char *key, const int key_len, data_t *value,
}
}
- if (this->free_pair_in_use) {
+ if (this->free_pair.key) { /* the free_pair is used */
pair = mem_get(THIS->ctx->dict_pair_pool);
if (!pair) {
if (key_free)
GF_FREE(key);
return -1;
}
- } else {
+ } else { /* assign the pair to the free pair */
pair = &this->free_pair;
- this->free_pair_in_use = _gf_true;
}
if (key_free) {
@@ -436,9 +436,7 @@ dict_set_lk(dict_t *this, char *key, const int key_len, data_t *value,
} else {
pair->key = (char *)GF_MALLOC(keylen + 1, gf_common_mt_char);
if (!pair->key) {
- if (pair == &this->free_pair) {
- this->free_pair_in_use = _gf_false;
- } else {
+ if (pair != &this->free_pair) {
mem_put(pair);
}
return -1;
@@ -656,7 +654,7 @@ dict_deln(dict_t *this, char *key, const int keylen)
GF_FREE(pair->key);
if (pair == &this->free_pair) {
- this->free_pair_in_use = _gf_false;
+ this->free_pair.key = NULL;
} else {
mem_put(pair);
}
@@ -696,6 +694,8 @@ dict_destroy(dict_t *this)
GF_FREE(prev->key);
if (prev != &this->free_pair) {
mem_put(prev);
+ } else {
+ this->free_pair.key = NULL;
}
total_pairs++;
prev = pair;
@@ -2125,7 +2125,7 @@ _dict_modify_flag(dict_t *this, char *key, int flag, int op)
else
BIT_CLEAR((unsigned char *)(data->data), flag);
- if (this->free_pair_in_use) {
+ if (this->free_pair.key) { /* the free pair is in use */
pair = mem_get0(THIS->ctx->dict_pair_pool);
if (!pair) {
gf_msg("dict", GF_LOG_ERROR, ENOMEM, LG_MSG_NO_MEMORY,
@@ -2133,9 +2133,8 @@ _dict_modify_flag(dict_t *this, char *key, int flag, int op)
ret = -ENOMEM;
goto err;
}
- } else {
+ } else { /* use the free pair */
pair = &this->free_pair;
- this->free_pair_in_use = _gf_true;
}
pair->key = (char *)GF_MALLOC(strlen(key) + 1, gf_common_mt_char);
@@ -2173,12 +2172,11 @@ err:
UNLOCK(&this->lock);
if (pair) {
- if (pair->key)
- free(pair->key);
-
- if (pair == &this->free_pair) {
- this->free_pair_in_use = _gf_false;
- } else {
+ if (pair->key) {
+ GF_FREE(pair->key);
+ pair->key = NULL;
+ }
+ if (pair != &this->free_pair) {
mem_put(pair);
}
}