X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=rculfhash.c;h=d733d6bd53da2cab4b849e343656ce53df4326f7;hb=960c9e4ff8e6028f5a0d4b1c1d747b20f08c5177;hp=fe8beedd7a9119d6b88e9a5e3fddec4267fd22a9;hpb=920f8ef668b03c65ca414bd6d53db314da920d1f;p=urcu.git diff --git a/rculfhash.c b/rculfhash.c index fe8beed..d733d6b 100644 --- a/rculfhash.c +++ b/rculfhash.c @@ -209,7 +209,7 @@ struct ht_items_count { } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); struct rcu_level { - struct rcu_head head; + /* Note: manually update allocation length when adding a field */ struct _cds_lfht_node nodes[0]; }; @@ -717,11 +717,19 @@ unsigned long _uatomic_max(unsigned long *ptr, unsigned long v) } static -void cds_lfht_free_level(struct rcu_head *head) +struct _cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size, + unsigned long hash) { - struct rcu_level *l = - caa_container_of(head, struct rcu_level, head); - poison_free(l); + unsigned long index, order; + + assert(size > 0); + index = hash & (size - 1); + order = get_count_order_ulong(index + 1); + + dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n", + hash, index, order, index & (!order ? 0 : ((1UL << (order - 1)) - 1))); + + return &ht->t.tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))]; } /* @@ -780,7 +788,6 @@ int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size, struct cds_lfht_node *dummy, *old_next; struct _cds_lfht_node *lookup; int flagged = 0; - unsigned long hash, index, order; if (!old_node) /* Return -ENOENT if asked to replace NULL node */ goto end; @@ -825,11 +832,7 @@ int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size, * lookup for the node, and remove it (along with any other * logically removed node) if found. */ - hash = bit_reverse_ulong(old_node->p.reverse_hash); - assert(size > 0); - index = hash & (size - 1); - order = get_count_order_ulong(index + 1); - lookup = &ht->t.tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))]; + lookup = lookup_bucket(ht, size, bit_reverse_ulong(old_node->p.reverse_hash)); dummy = (struct cds_lfht_node *) lookup; _cds_lfht_gc_bucket(dummy, new_node); end: @@ -852,9 +855,8 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, enum add_mode mode, int dummy) { struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next, - *dummy_node, *return_node; + *return_node; struct _cds_lfht_node *lookup; - unsigned long hash, index, order; assert(!is_dummy(node)); assert(!is_removed(node)); @@ -863,7 +865,7 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, node->p.next = flag_dummy(get_end()); return node; /* Initial first add (head) */ } - hash = bit_reverse_ulong(node->p.reverse_hash); + lookup = lookup_bucket(ht, size, bit_reverse_ulong(node->p.reverse_hash)); for (;;) { uint32_t chain_len = 0; @@ -871,9 +873,6 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, * iter_prev points to the non-removed node prior to the * insert location. */ - index = hash & (size - 1); - order = get_count_order_ulong(index + 1); - lookup = &ht->t.tbl[order]->nodes[index & ((!order ? 0 : (1UL << (order - 1))) - 1)]; iter_prev = (struct cds_lfht_node *) lookup; /* We can always skip the dummy node initially */ iter = rcu_dereference(iter_prev->p.next); @@ -925,7 +924,7 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, return_node = NULL; else /* ADD_DEFAULT and ADD_UNIQUE */ return_node = node; - goto gc_end; + goto end; } replace: @@ -947,13 +946,6 @@ struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next); /* retry */ } -gc_end: - /* Garbage collect logically removed nodes in the bucket */ - index = hash & (size - 1); - order = get_count_order_ulong(index + 1); - lookup = &ht->t.tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))]; - dummy_node = (struct cds_lfht_node *) lookup; - _cds_lfht_gc_bucket(dummy_node, node); end: return return_node; } @@ -966,7 +958,6 @@ int _cds_lfht_del(struct cds_lfht *ht, unsigned long size, struct cds_lfht_node *dummy, *next, *old; struct _cds_lfht_node *lookup; int flagged = 0; - unsigned long hash, index, order; if (!node) /* Return -ENOENT if asked to delete NULL node */ goto end; @@ -997,11 +988,7 @@ int _cds_lfht_del(struct cds_lfht *ht, unsigned long size, * the node, and remove it (along with any other logically removed node) * if found. */ - hash = bit_reverse_ulong(node->p.reverse_hash); - assert(size > 0); - index = hash & (size - 1); - order = get_count_order_ulong(index + 1); - lookup = &ht->t.tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1)) - 1))]; + lookup = lookup_bucket(ht, size, bit_reverse_ulong(node->p.reverse_hash)); dummy = (struct cds_lfht_node *) lookup; _cds_lfht_gc_bucket(dummy, node); end: @@ -1135,8 +1122,7 @@ void init_table(struct cds_lfht *ht, if (CMM_LOAD_SHARED(ht->t.resize_target) < (!i ? 1 : (1UL << i))) break; - ht->t.tbl[i] = calloc(1, sizeof(struct rcu_level) - + (len * sizeof(struct _cds_lfht_node))); + ht->t.tbl[i] = calloc(1, len * sizeof(struct _cds_lfht_node)); assert(ht->t.tbl[i]); /* @@ -1222,6 +1208,7 @@ void fini_table(struct cds_lfht *ht, unsigned long first_order, unsigned long len_order) { long i, end_order; + void *free_by_rcu = NULL; dbg_printf("fini table: first_order %lu end_order %lu\n", first_order, first_order + len_order); @@ -1247,6 +1234,8 @@ void fini_table(struct cds_lfht *ht, * return a logically removed node as insert position. */ ht->cds_lfht_synchronize_rcu(); + if (free_by_rcu) + free(free_by_rcu); /* * Set "removed" flag in dummy nodes about to be removed. @@ -1256,12 +1245,17 @@ void fini_table(struct cds_lfht *ht, */ remove_table(ht, i, len); - ht->cds_lfht_call_rcu(&ht->t.tbl[i]->head, cds_lfht_free_level); + free_by_rcu = ht->t.tbl[i]; dbg_printf("fini new size: %lu\n", 1UL << i); if (CMM_LOAD_SHARED(ht->in_progress_destroy)) break; } + + if (free_by_rcu) { + ht->cds_lfht_synchronize_rcu(); + free(free_by_rcu); + } } struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, @@ -1319,17 +1313,13 @@ void cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len, { struct cds_lfht_node *node, *next, *dummy_node; struct _cds_lfht_node *lookup; - unsigned long hash, reverse_hash, index, order, size; + unsigned long hash, reverse_hash, size; hash = ht->hash_fct(key, key_len, ht->hash_seed); reverse_hash = bit_reverse_ulong(hash); size = rcu_dereference(ht->t.size); - index = hash & (size - 1); - order = get_count_order_ulong(index + 1); - lookup = &ht->t.tbl[order]->nodes[index & (!order ? 0 : ((1UL << (order - 1))) - 1)]; - dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n", - hash, index, order, index & (!order ? 0 : ((1UL << (order - 1)) - 1))); + lookup = lookup_bucket(ht, size, hash); dummy_node = (struct cds_lfht_node *) lookup; /* We can always skip the dummy node initially */ node = rcu_dereference(dummy_node->p.next);