| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | * |
| 4 | * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com> |
| 5 | * |
| 6 | * Chunk based memory management for Lock-Free RCU Hash Table |
| 7 | */ |
| 8 | |
| 9 | #include <stddef.h> |
| 10 | #include "rculfhash-internal.h" |
| 11 | |
| 12 | static |
| 13 | void lttng_ust_lfht_alloc_bucket_table(struct lttng_ust_lfht *ht, unsigned long order) |
| 14 | { |
| 15 | if (order == 0) { |
| 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); |
| 21 | |
| 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]); |
| 26 | } |
| 27 | } |
| 28 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ |
| 29 | } |
| 30 | |
| 31 | /* |
| 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 |
| 34 | * lfht is destroyed. |
| 35 | */ |
| 36 | static |
| 37 | void lttng_ust_lfht_free_bucket_table(struct lttng_ust_lfht *ht, unsigned long order) |
| 38 | { |
| 39 | if (order == 0) |
| 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); |
| 43 | |
| 44 | for (i = len; i < 2 * len; i++) |
| 45 | poison_free(ht->tbl_chunk[i]); |
| 46 | } |
| 47 | /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */ |
| 48 | } |
| 49 | |
| 50 | static |
| 51 | struct lttng_ust_lfht_node *bucket_at(struct lttng_ust_lfht *ht, unsigned long index) |
| 52 | { |
| 53 | unsigned long chunk, offset; |
| 54 | |
| 55 | chunk = index >> ht->min_alloc_buckets_order; |
| 56 | offset = index & (ht->min_nr_alloc_buckets - 1); |
| 57 | return &ht->tbl_chunk[chunk][offset]; |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | struct lttng_ust_lfht *alloc_lttng_ust_lfht(unsigned long min_nr_alloc_buckets, |
| 62 | unsigned long max_nr_buckets) |
| 63 | { |
| 64 | unsigned long nr_chunks, lttng_ust_lfht_size; |
| 65 | |
| 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)); |
| 72 | |
| 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); |
| 76 | } |
| 77 | |
| 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, |
| 83 | }; |