diff options
author | Costa Tsaousis <costa@netdata.cloud> | 2023-10-12 12:55:25 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-12 12:55:25 +0300 |
commit | 4c3c0ad33020630182257ee3c655f6a089266c26 (patch) | |
tree | 7c3102b4471716c9494dfbb1a1c65cd22add2866 /libnetdata | |
parent | 756046a30559bf3d99b3ae0bbb3bd2d3e1f64277 (diff) |
facets: do not corrupt the index when doubling the hashtable (#16171)
do not corrupt the index when doubling the hashtable
Diffstat (limited to 'libnetdata')
-rw-r--r-- | libnetdata/facets/facets.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/libnetdata/facets/facets.c b/libnetdata/facets/facets.c index 01ae4e6160..77ecb3e4ff 100644 --- a/libnetdata/facets/facets.c +++ b/libnetdata/facets/facets.c @@ -374,7 +374,7 @@ static inline FACET_VALUE *FACET_VALUE_GET_FROM_INDEX(FACET_KEY *k, FACETS_HASH static void FACET_VALUES_HASHTABLE_DOUBLE(FACET_KEY *k) { // increase the hashtable size freez(k->values.hashtable); - k->values.size *= 4; + k->values.size *= 2; k->values.hashtable = callocz(k->values.size, sizeof(FACET_VALUE *)); for(FACET_VALUE *v = k->values.ll ; v ;v = v->next) { FACET_VALUE **v_ptr = facets_values_hashtable_slot(k, v->hash); @@ -518,7 +518,7 @@ static inline void FACETS_KEYS_INDEX_DESTROY(FACETS *facets) { } static inline FACET_KEY **facets_keys_hashtable_slot(FACETS *facets, FACETS_HASH hash) { - size_t slot = hash % FACETS_KEYS_HASHTABLE_ENTRIES; + size_t slot = hash % facets->keys.size; FACET_KEY **k = &facets->keys.hashtable[slot]; while(*k && (*k)->hash != hash) @@ -530,7 +530,7 @@ static inline FACET_KEY **facets_keys_hashtable_slot(FACETS *facets, FACETS_HASH static void FACET_KEYS_HASHTABLE_DOUBLE(FACETS *facets) { // increase the hashtable size freez(facets->keys.hashtable); - facets->keys.size *= 4; + facets->keys.size *= 2; facets->keys.hashtable = callocz(facets->keys.size, sizeof(FACET_KEY *)); for(FACET_KEY *k = facets->keys.ll ; k ; k = k->next) { FACET_KEY **k_ptr = facets_keys_hashtable_slot(facets, k->hash); @@ -619,9 +619,6 @@ static inline FACET_KEY *FACETS_KEY_CREATE(FACETS *facets, FACETS_HASH hash, con DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(facets->keys.ll, k, prev, next); facets->keys.count++; - if(facets->keys.count > facets->keys.size / 2) - FACET_KEYS_HASHTABLE_DOUBLE(facets); - return k; } @@ -632,8 +629,12 @@ static inline FACET_KEY *FACETS_KEY_ADD_TO_INDEX(FACETS *facets, FACETS_HASH has if(unlikely(!(*k_ptr))) { // we have to add it - *k_ptr = FACETS_KEY_CREATE(facets, hash, name, name_length, options); - return (*k_ptr); + FACET_KEY *k = *k_ptr = FACETS_KEY_CREATE(facets, hash, name, name_length, options); + + if(facets->keys.count > facets->keys.size / 2) + FACET_KEYS_HASHTABLE_DOUBLE(facets); + + return k; } // already in the index |