X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=rculfhash.c;h=675ef382311beafcbb8d62ad284b2032d6db79aa;hp=b67acc820a078011f81963608f7c366fc1a3d2ec;hb=5488222bbdb95680d942c3fb645095b48e96620f;hpb=238cc06e5728e3a642d4d22bd4fc199cfd265110 diff --git a/rculfhash.c b/rculfhash.c index b67acc8..675ef38 100644 --- a/rculfhash.c +++ b/rculfhash.c @@ -241,6 +241,8 @@ struct cds_lfht { struct rcu_table t; cds_lfht_hash_fct hash_fct; cds_lfht_compare_fct compare_fct; + unsigned long min_alloc_order; + unsigned long min_alloc_size; unsigned long hash_seed; int flags; /* @@ -871,12 +873,6 @@ void _cds_lfht_add(struct cds_lfht *ht, assert(!is_dummy(node)); assert(!is_removed(node)); - if (!size) { - assert(dummy); - assert(!unique_ret); - node->p.next = flag_dummy(get_end()); - return; /* Initial first add (head) */ - } lookup = lookup_bucket(ht, size, bit_reverse_ulong(node->p.reverse_hash)); for (;;) { uint32_t chain_len = 0; @@ -1084,16 +1080,17 @@ void init_table_populate_partition(struct cds_lfht *ht, unsigned long i, { unsigned long j; + assert(i > ht->min_alloc_order); ht->cds_lfht_rcu_read_lock(); for (j = start; j < start + len; j++) { struct cds_lfht_node *new_node = (struct cds_lfht_node *) &ht->t.tbl[i]->nodes[j]; dbg_printf("init populate: i %lu j %lu hash %lu\n", - i, j, !i ? 0 : (1UL << (i - 1)) + j); + i, j, (1UL << (i - 1)) + j); new_node->p.reverse_hash = - bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j); - _cds_lfht_add(ht, !i ? 0 : (1UL << (i - 1)), + bit_reverse_ulong((1UL << (i - 1)) + j); + _cds_lfht_add(ht, 1UL << (i - 1), new_node, NULL, 1); } ht->cds_lfht_rcu_read_unlock(); @@ -1121,14 +1118,15 @@ void init_table(struct cds_lfht *ht, dbg_printf("init table: first_order %lu last_order %lu\n", first_order, last_order); + assert(first_order > ht->min_alloc_order); for (i = first_order; i <= last_order; i++) { unsigned long len; - len = !i ? 1 : 1UL << (i - 1); + len = 1UL << (i - 1); dbg_printf("init order %lu len: %lu\n", i, len); /* Stop expand if the resize target changes under us */ - if (CMM_LOAD_SHARED(ht->t.resize_target) < (!i ? 1 : (1UL << i))) + if (CMM_LOAD_SHARED(ht->t.resize_target) < (1UL << i)) break; ht->t.tbl[i] = calloc(1, len * sizeof(struct _cds_lfht_node)); @@ -1144,9 +1142,9 @@ void init_table(struct cds_lfht *ht, * Update table size. */ cmm_smp_wmb(); /* populate data before RCU size */ - CMM_STORE_SHARED(ht->t.size, !i ? 1 : (1UL << i)); + CMM_STORE_SHARED(ht->t.size, 1UL << i); - dbg_printf("init new size: %lu\n", !i ? 1 : (1UL << i)); + dbg_printf("init new size: %lu\n", 1UL << i); if (CMM_LOAD_SHARED(ht->in_progress_destroy)) break; } @@ -1183,17 +1181,17 @@ void remove_table_partition(struct cds_lfht *ht, unsigned long i, { unsigned long j; + assert(i > ht->min_alloc_order); ht->cds_lfht_rcu_read_lock(); for (j = start; j < start + len; j++) { struct cds_lfht_node *fini_node = (struct cds_lfht_node *) &ht->t.tbl[i]->nodes[j]; dbg_printf("remove entry: i %lu j %lu hash %lu\n", - i, j, !i ? 0 : (1UL << (i - 1)) + j); + i, j, (1UL << (i - 1)) + j); fini_node->p.reverse_hash = - bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j); - (void) _cds_lfht_del(ht, !i ? 0 : (1UL << (i - 1)), - fini_node, 1); + bit_reverse_ulong((1UL << (i - 1)) + j); + (void) _cds_lfht_del(ht, 1UL << (i - 1), fini_node, 1); } ht->cds_lfht_rcu_read_unlock(); } @@ -1221,11 +1219,11 @@ void fini_table(struct cds_lfht *ht, dbg_printf("fini table: first_order %lu last_order %lu\n", first_order, last_order); - assert(first_order > 0); + assert(first_order > ht->min_alloc_order); for (i = last_order; i >= first_order; i--) { unsigned long len; - len = !i ? 1 : 1UL << (i - 1); + len = 1UL << (i - 1); dbg_printf("fini order %lu len: %lu\n", i, len); /* Stop shrink if the resize target changes under us */ @@ -1266,10 +1264,54 @@ void fini_table(struct cds_lfht *ht, } } +static +void cds_lfht_create_dummy(struct cds_lfht *ht, unsigned long size) +{ + struct _cds_lfht_node *prev, *node; + unsigned long order, len, i, j; + + ht->t.tbl[0] = calloc(1, ht->min_alloc_size * sizeof(struct _cds_lfht_node)); + assert(ht->t.tbl[0]); + + dbg_printf("create dummy: order %lu index %lu hash %lu\n", 0, 0, 0); + ht->t.tbl[0]->nodes[0].next = flag_dummy(get_end()); + ht->t.tbl[0]->nodes[0].reverse_hash = 0; + + for (order = 1; order < get_count_order_ulong(size) + 1; order++) { + len = 1UL << (order - 1); + if (order <= ht->min_alloc_order) { + ht->t.tbl[order] = (void *)(ht->t.tbl[0]->nodes + len); + } else { + ht->t.tbl[order] = calloc(1, len * sizeof(struct _cds_lfht_node)); + assert(ht->t.tbl[order]); + } + + i = 0; + prev = ht->t.tbl[i]->nodes; + for (j = 0; j < len; j++) { + if (j & (j - 1)) { /* Between power of 2 */ + prev++; + } else if (j) { /* At each power of 2 */ + i++; + prev = ht->t.tbl[i]->nodes; + } + + node = &ht->t.tbl[order]->nodes[j]; + dbg_printf("create dummy: order %lu index %lu hash %lu\n", + order, j, j + len); + node->next = prev->next; + assert(is_dummy(node->next)); + node->reverse_hash = bit_reverse_ulong(j + len); + prev->next = flag_dummy((struct cds_lfht_node *)node); + } + } +} + struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, cds_lfht_compare_fct compare_fct, unsigned long hash_seed, unsigned long init_size, + unsigned long min_alloc_size, int flags, void (*cds_lfht_call_rcu)(struct rcu_head *head, void (*func)(struct rcu_head *head)), @@ -1285,9 +1327,14 @@ struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, struct cds_lfht *ht; unsigned long order; + /* min_alloc_size must be power of two */ + if (!min_alloc_size || (min_alloc_size & (min_alloc_size - 1))) + return NULL; /* init_size must be power of two */ - if (init_size && (init_size & (init_size - 1))) + if (!init_size || (init_size & (init_size - 1))) return NULL; + min_alloc_size = max(min_alloc_size, MIN_TABLE_SIZE); + init_size = max(init_size, min_alloc_size); ht = calloc(1, sizeof(struct cds_lfht)); assert(ht); ht->hash_fct = hash_fct; @@ -1305,14 +1352,13 @@ struct cds_lfht *_cds_lfht_new(cds_lfht_hash_fct hash_fct, ht->percpu_count = alloc_per_cpu_items_count(); /* this mutex should not nest in read-side C.S. */ pthread_mutex_init(&ht->resize_mutex, NULL); - order = get_count_order_ulong(max(init_size, MIN_TABLE_SIZE)); ht->flags = flags; - ht->cds_lfht_rcu_thread_offline(); - pthread_mutex_lock(&ht->resize_mutex); + order = get_count_order_ulong(init_size); ht->t.resize_target = 1UL << order; - init_table(ht, 0, order); - pthread_mutex_unlock(&ht->resize_mutex); - ht->cds_lfht_rcu_thread_online(); + cds_lfht_create_dummy(ht, 1UL << order); + ht->t.size = 1UL << order; + ht->min_alloc_size = min_alloc_size; + ht->min_alloc_order = get_count_order_ulong(min_alloc_size); return ht; } @@ -1530,7 +1576,12 @@ int cds_lfht_delete_dummy(struct cds_lfht *ht) bit_reverse_ulong(ht->t.tbl[order]->nodes[i].reverse_hash)); assert(is_dummy(ht->t.tbl[order]->nodes[i].next)); } - poison_free(ht->t.tbl[order]); + + if (order == ht->min_alloc_order) + poison_free(ht->t.tbl[0]); + else if (order > ht->min_alloc_order) + poison_free(ht->t.tbl[order]); + /* Nothing to delete for order < ht->min_alloc_order */ } return 0; } @@ -1631,7 +1682,7 @@ void _do_cds_lfht_shrink(struct cds_lfht *ht, { unsigned long old_order, new_order; - new_size = max(new_size, MIN_TABLE_SIZE); + new_size = max(new_size, ht->min_alloc_size); old_order = get_count_order_ulong(old_size); new_order = get_count_order_ulong(new_size); dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n", @@ -1681,7 +1732,7 @@ static void resize_target_update_count(struct cds_lfht *ht, unsigned long count) { - count = max(count, MIN_TABLE_SIZE); + count = max(count, ht->min_alloc_size); uatomic_set(&ht->t.resize_target, count); }