rculfhash mm plugins: cleanup alloc
[urcu.git] / rculfhash-mm-order.c
1 /*
2 * rculfhash-mm-order.c
3 *
4 * Order based memory management for Lock-Free RCU Hash Table
5 *
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <rculfhash-internal.h>
25
26 static
27 void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
28 {
29 if (order == 0) {
30 ht->tbl_order[0] = calloc(ht->min_nr_alloc_buckets,
31 sizeof(struct cds_lfht_node));
32 assert(ht->tbl_order[0]);
33 } else if (order > ht->min_alloc_buckets_order) {
34 ht->tbl_order[order] = calloc(1UL << (order -1),
35 sizeof(struct cds_lfht_node));
36 assert(ht->tbl_order[order]);
37 }
38 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
39 }
40
41 /*
42 * cds_lfht_free_bucket_table() should be called with decreasing order.
43 * When cds_lfht_free_bucket_table(0) is called, it means the whole
44 * lfht is destroyed.
45 */
46 static
47 void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
48 {
49 if (order == 0)
50 poison_free(ht->tbl_order[0]);
51 else if (order > ht->min_alloc_buckets_order)
52 poison_free(ht->tbl_order[order]);
53 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
54 }
55
56 static
57 struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
58 {
59 unsigned long order;
60
61 if (index < ht->min_nr_alloc_buckets) {
62 dbg_printf("bucket index %lu order 0 aridx 0\n", index);
63 return &ht->tbl_order[0][index];
64 }
65 /*
66 * equivalent to get_count_order_ulong(index + 1), but optimizes
67 * away the non-existing 0 special-case for
68 * get_count_order_ulong.
69 */
70 order = fls_ulong(index);
71 dbg_printf("bucket index %lu order %lu aridx %lu\n",
72 index, order, index & ((1UL << (order - 1)) - 1));
73 return &ht->tbl_order[order][index & ((1UL << (order - 1)) - 1)];
74 }
75
76 static
77 struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
78 unsigned long max_nr_buckets)
79 {
80 struct cds_lfht *ht;
81
82 ht = calloc(1, sizeof(struct cds_lfht));
83 assert(ht);
84
85 ht->bucket_at = bucket_at;
86 ht->mm = &cds_lfht_mm_order;
87 ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
88 ht->min_alloc_buckets_order =
89 get_count_order_ulong(min_nr_alloc_buckets);
90 ht->max_nr_buckets = max_nr_buckets;
91
92 return ht;
93 }
94
95 const struct cds_lfht_mm_type cds_lfht_mm_order = {
96 .alloc_cds_lfht = alloc_cds_lfht,
97 .alloc_bucket_table = cds_lfht_alloc_bucket_table,
98 .free_bucket_table = cds_lfht_free_bucket_table,
99 .bucket_at = bucket_at,
100 };
This page took 0.053547 seconds and 5 git commands to generate.