2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
6 * Chunk 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_chunk
[0] = calloc(ht
->min_nr_alloc_buckets
,
17 sizeof(struct lttng_ust_lfht_node
));
18 assert(ht
->tbl_chunk
[0]);
19 } else if (order
> ht
->min_alloc_buckets_order
) {
20 unsigned long i
, len
= 1UL << (order
- 1 - ht
->min_alloc_buckets_order
);
22 for (i
= len
; i
< 2 * len
; i
++) {
23 ht
->tbl_chunk
[i
] = calloc(ht
->min_nr_alloc_buckets
,
24 sizeof(struct lttng_ust_lfht_node
));
25 assert(ht
->tbl_chunk
[i
]);
28 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
32 * lttng_ust_lfht_free_bucket_table() should be called with decreasing order.
33 * When lttng_ust_lfht_free_bucket_table(0) is called, it means the whole
37 void lttng_ust_lfht_free_bucket_table(struct lttng_ust_lfht
*ht
, unsigned long order
)
40 poison_free(ht
->tbl_chunk
[0]);
41 else if (order
> ht
->min_alloc_buckets_order
) {
42 unsigned long i
, len
= 1UL << (order
- 1 - ht
->min_alloc_buckets_order
);
44 for (i
= len
; i
< 2 * len
; i
++)
45 poison_free(ht
->tbl_chunk
[i
]);
47 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
51 struct lttng_ust_lfht_node
*bucket_at(struct lttng_ust_lfht
*ht
, unsigned long index
)
53 unsigned long chunk
, offset
;
55 chunk
= index
>> ht
->min_alloc_buckets_order
;
56 offset
= index
& (ht
->min_nr_alloc_buckets
- 1);
57 return &ht
->tbl_chunk
[chunk
][offset
];
61 struct lttng_ust_lfht
*alloc_lttng_ust_lfht(unsigned long min_nr_alloc_buckets
,
62 unsigned long max_nr_buckets
)
64 unsigned long nr_chunks
, lttng_ust_lfht_size
;
66 min_nr_alloc_buckets
= max(min_nr_alloc_buckets
,
67 max_nr_buckets
/ MAX_CHUNK_TABLE
);
68 nr_chunks
= max_nr_buckets
/ min_nr_alloc_buckets
;
69 lttng_ust_lfht_size
= offsetof(struct lttng_ust_lfht
, tbl_chunk
) +
70 sizeof(struct lttng_ust_lfht_node
*) * nr_chunks
;
71 lttng_ust_lfht_size
= max(lttng_ust_lfht_size
, sizeof(struct lttng_ust_lfht
));
73 return __default_alloc_lttng_ust_lfht(
74 <tng_ust_lfht_mm_chunk
, lttng_ust_lfht_size
,
75 min_nr_alloc_buckets
, max_nr_buckets
);
78 const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_chunk
= {
79 .alloc_lttng_ust_lfht
= alloc_lttng_ust_lfht
,
80 .alloc_bucket_table
= lttng_ust_lfht_alloc_bucket_table
,
81 .free_bucket_table
= lttng_ust_lfht_free_bucket_table
,
82 .bucket_at
= bucket_at
,