Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / rculfhash-mm-order.c
CommitLineData
10544ee8 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-or-later
10544ee8 3 *
c0c0989a
MJ
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
10544ee8 6 *
c0c0989a 7 * Order based memory management for Lock-Free RCU Hash Table
10544ee8
MD
8 */
9
10#include <rculfhash-internal.h>
11
12static
13void lttng_ust_lfht_alloc_bucket_table(struct lttng_ust_lfht *ht, unsigned long order)
14{
15 if (order == 0) {
16 ht->tbl_order[0] = calloc(ht->min_nr_alloc_buckets,
17 sizeof(struct lttng_ust_lfht_node));
18 assert(ht->tbl_order[0]);
19 } else if (order > ht->min_alloc_buckets_order) {
20 ht->tbl_order[order] = calloc(1UL << (order -1),
21 sizeof(struct lttng_ust_lfht_node));
22 assert(ht->tbl_order[order]);
23 }
24 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
25}
26
27/*
28 * lttng_ust_lfht_free_bucket_table() should be called with decreasing order.
29 * When lttng_ust_lfht_free_bucket_table(0) is called, it means the whole
30 * lfht is destroyed.
31 */
32static
33void lttng_ust_lfht_free_bucket_table(struct lttng_ust_lfht *ht, unsigned long order)
34{
35 if (order == 0)
36 poison_free(ht->tbl_order[0]);
37 else if (order > ht->min_alloc_buckets_order)
38 poison_free(ht->tbl_order[order]);
39 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
40}
41
42static
43struct lttng_ust_lfht_node *bucket_at(struct lttng_ust_lfht *ht, unsigned long index)
44{
45 unsigned long order;
46
47 if (index < ht->min_nr_alloc_buckets) {
48 dbg_printf("bucket index %lu order 0 aridx 0\n", index);
49 return &ht->tbl_order[0][index];
50 }
51 /*
52 * equivalent to lttng_ust_lfht_get_count_order_ulong(index + 1), but
53 * optimizes away the non-existing 0 special-case for
54 * lttng_ust_lfht_get_count_order_ulong.
55 */
56 order = lttng_ust_lfht_fls_ulong(index);
57 dbg_printf("bucket index %lu order %lu aridx %lu\n",
58 index, order, index & ((1UL << (order - 1)) - 1));
59 return &ht->tbl_order[order][index & ((1UL << (order - 1)) - 1)];
60}
61
62static
63struct lttng_ust_lfht *alloc_lttng_ust_lfht(unsigned long min_nr_alloc_buckets,
64 unsigned long max_nr_buckets)
65{
66 return __default_alloc_lttng_ust_lfht(
67 &lttng_ust_lfht_mm_order, sizeof(struct lttng_ust_lfht),
68 min_nr_alloc_buckets, max_nr_buckets);
69}
70
71const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_order = {
72 .alloc_lttng_ust_lfht = alloc_lttng_ust_lfht,
73 .alloc_bucket_table = lttng_ust_lfht_alloc_bucket_table,
74 .free_bucket_table = lttng_ust_lfht_free_bucket_table,
75 .bucket_at = bucket_at,
76};
This page took 0.02589 seconds and 4 git commands to generate.