Commit | Line | Data |
---|---|---|
0b6aa001 LJ |
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 | ||
01477510 | 24 | #include <urcu/assert.h> |
0b6aa001 LJ |
25 | #include <rculfhash-internal.h> |
26 | ||
27 | static | |
28 | void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order) | |
29 | { | |
30 | if (order == 0) { | |
31 | ht->tbl_order[0] = calloc(ht->min_nr_alloc_buckets, | |
32 | sizeof(struct cds_lfht_node)); | |
01477510 | 33 | urcu_posix_assert(ht->tbl_order[0]); |
0b6aa001 LJ |
34 | } else if (order > ht->min_alloc_buckets_order) { |
35 | ht->tbl_order[order] = calloc(1UL << (order -1), | |
36 | sizeof(struct cds_lfht_node)); | |
01477510 | 37 | urcu_posix_assert(ht->tbl_order[order]); |
0b6aa001 LJ |
38 | } |
39 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ | |
40 | } | |
41 | ||
42 | /* | |
43 | * cds_lfht_free_bucket_table() should be called with decreasing order. | |
44 | * When cds_lfht_free_bucket_table(0) is called, it means the whole | |
45 | * lfht is destroyed. | |
46 | */ | |
47 | static | |
48 | void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order) | |
49 | { | |
50 | if (order == 0) | |
51 | poison_free(ht->tbl_order[0]); | |
52 | else if (order > ht->min_alloc_buckets_order) | |
53 | poison_free(ht->tbl_order[order]); | |
54 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ | |
55 | } | |
56 | ||
57 | static | |
58 | struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index) | |
59 | { | |
60 | unsigned long order; | |
61 | ||
62 | if (index < ht->min_nr_alloc_buckets) { | |
63 | dbg_printf("bucket index %lu order 0 aridx 0\n", index); | |
64 | return &ht->tbl_order[0][index]; | |
65 | } | |
66 | /* | |
5bc6b66f MD |
67 | * equivalent to cds_lfht_get_count_order_ulong(index + 1), but |
68 | * optimizes away the non-existing 0 special-case for | |
69 | * cds_lfht_get_count_order_ulong. | |
0b6aa001 | 70 | */ |
5bc6b66f | 71 | order = cds_lfht_fls_ulong(index); |
0b6aa001 LJ |
72 | dbg_printf("bucket index %lu order %lu aridx %lu\n", |
73 | index, order, index & ((1UL << (order - 1)) - 1)); | |
74 | return &ht->tbl_order[order][index & ((1UL << (order - 1)) - 1)]; | |
75 | } | |
76 | ||
77 | static | |
78 | struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets, | |
79 | unsigned long max_nr_buckets) | |
80 | { | |
1228af1c LJ |
81 | return __default_alloc_cds_lfht( |
82 | &cds_lfht_mm_order, sizeof(struct cds_lfht), | |
83 | min_nr_alloc_buckets, max_nr_buckets); | |
0b6aa001 LJ |
84 | } |
85 | ||
86 | const struct cds_lfht_mm_type cds_lfht_mm_order = { | |
87 | .alloc_cds_lfht = alloc_cds_lfht, | |
88 | .alloc_bucket_table = cds_lfht_alloc_bucket_table, | |
89 | .free_bucket_table = cds_lfht_free_bucket_table, | |
90 | .bucket_at = bucket_at, | |
91 | }; |