2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
7 * Order based memory management for Lock-Free RCU Hash Table
10 #include <rculfhash-internal.h>
13 void lttng_ust_lfht_alloc_bucket_table(struct lttng_ust_lfht
*ht
, unsigned long order
)
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
]);
24 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
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
33 void lttng_ust_lfht_free_bucket_table(struct lttng_ust_lfht
*ht
, unsigned long order
)
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 */
43 struct lttng_ust_lfht_node
*bucket_at(struct lttng_ust_lfht
*ht
, unsigned long index
)
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
];
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.
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)];
63 struct lttng_ust_lfht
*alloc_lttng_ust_lfht(unsigned long min_nr_alloc_buckets
,
64 unsigned long max_nr_buckets
)
66 return __default_alloc_lttng_ust_lfht(
67 <tng_ust_lfht_mm_order
, sizeof(struct lttng_ust_lfht
),
68 min_nr_alloc_buckets
, max_nr_buckets
);
71 const 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
,