1 // SPDX-FileCopyrightText: 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
4 // SPDX-License-Identifier: LGPL-2.1-or-later
7 * Order based memory management for Lock-Free RCU Hash Table
10 #include <urcu/assert.h>
11 #include "rculfhash-internal.h"
14 void cds_lfht_alloc_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
17 ht
->tbl_order
[0] = ht
->alloc
->calloc(ht
->alloc
->state
,
18 ht
->min_nr_alloc_buckets
, sizeof(struct cds_lfht_node
));
19 urcu_posix_assert(ht
->tbl_order
[0]);
20 } else if (order
> ht
->min_alloc_buckets_order
) {
21 ht
->tbl_order
[order
] = ht
->alloc
->calloc(ht
->alloc
->state
,
22 1UL << (order
-1), sizeof(struct cds_lfht_node
));
23 urcu_posix_assert(ht
->tbl_order
[order
]);
25 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
29 * cds_lfht_free_bucket_table() should be called with decreasing order.
30 * When cds_lfht_free_bucket_table(0) is called, it means the whole
34 void cds_lfht_free_bucket_table(struct cds_lfht
*ht
, unsigned long order
)
37 poison_free(ht
->alloc
, ht
->tbl_order
[0]);
38 else if (order
> ht
->min_alloc_buckets_order
)
39 poison_free(ht
->alloc
, ht
->tbl_order
[order
]);
40 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
44 struct cds_lfht_node
*bucket_at(struct cds_lfht
*ht
, unsigned long index
)
48 if (index
< ht
->min_nr_alloc_buckets
) {
49 dbg_printf("bucket index %lu order 0 aridx 0\n", index
);
50 return &ht
->tbl_order
[0][index
];
53 * equivalent to cds_lfht_get_count_order_ulong(index + 1), but
54 * optimizes away the non-existing 0 special-case for
55 * cds_lfht_get_count_order_ulong.
57 order
= cds_lfht_fls_ulong(index
);
58 dbg_printf("bucket index %lu order %lu aridx %lu\n",
59 index
, order
, index
& ((1UL << (order
- 1)) - 1));
60 return &ht
->tbl_order
[order
][index
& ((1UL << (order
- 1)) - 1)];
64 struct cds_lfht
*alloc_cds_lfht(unsigned long min_nr_alloc_buckets
,
65 unsigned long max_nr_buckets
, const struct cds_lfht_alloc
*alloc
)
67 return __default_alloc_cds_lfht(
68 &cds_lfht_mm_order
, alloc
, sizeof(struct cds_lfht
),
69 min_nr_alloc_buckets
, max_nr_buckets
);
72 const struct cds_lfht_mm_type cds_lfht_mm_order
= {
73 .alloc_cds_lfht
= alloc_cds_lfht
,
74 .alloc_bucket_table
= cds_lfht_alloc_bucket_table
,
75 .free_bucket_table
= cds_lfht_free_bucket_table
,
76 .bucket_at
= bucket_at
,