add rculfhash-mm-chunk.c memory management
[urcu.git] / rculfhash-mm-chunk.c
... / ...
CommitLineData
1/*
2 * rculfhash-mm-chunk.c
3 *
4 * Chunk based memory management for Lock-Free RCU Hash Table
5 *
6 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <stddef.h>
24#include <rculfhash-internal.h>
25
26static
27void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
28{
29 if (order == 0) {
30 ht->tbl_chunk[0] = calloc(ht->min_nr_alloc_buckets,
31 sizeof(struct cds_lfht_node));
32 assert(ht->tbl_chunk[0]);
33 } else if (order > ht->min_alloc_buckets_order) {
34 unsigned long i, len = 1UL << (order - 1 - ht->min_alloc_buckets_order);
35
36 for (i = len; i < 2 * len; i++) {
37 ht->tbl_chunk[i] = calloc(ht->min_nr_alloc_buckets,
38 sizeof(struct cds_lfht_node));
39 assert(ht->tbl_chunk[i]);
40 }
41 }
42 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
43}
44
45/*
46 * cds_lfht_free_bucket_table() should be called with decreasing order.
47 * When cds_lfht_free_bucket_table(0) is called, it means the whole
48 * lfht is destroyed.
49 */
50static
51void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
52{
53 if (order == 0)
54 poison_free(ht->tbl_chunk[0]);
55 else if (order > ht->min_alloc_buckets_order) {
56 unsigned long i, len = 1UL << (order - 1 - ht->min_alloc_buckets_order);
57
58 for (i = len; i < 2 * len; i++)
59 poison_free(ht->tbl_chunk[i]);
60 }
61 /* Nothing to do for 0 < order && order <= ht->min_alloc_buckets_order */
62}
63
64static
65struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
66{
67 unsigned long chunk, offset;
68
69 chunk = index >> ht->min_alloc_buckets_order;
70 offset = index & (ht->min_nr_alloc_buckets - 1);
71 return &ht->tbl_chunk[chunk][offset];
72}
73
74static
75struct cds_lfht *alloc_cds_lfht(unsigned long min_nr_alloc_buckets,
76 unsigned long max_nr_buckets)
77{
78 struct cds_lfht *ht;
79 unsigned long nr_chunks, cds_lfht_size;
80
81 min_nr_alloc_buckets = max(min_nr_alloc_buckets,
82 max_nr_buckets / MAX_CHUNK_TABLE);
83
84 nr_chunks = max_nr_buckets / min_nr_alloc_buckets;
85 cds_lfht_size = offsetof(struct cds_lfht, tbl_chunk) +
86 sizeof(ht->tbl_chunk[0]) * nr_chunks;
87 cds_lfht_size = max(cds_lfht_size, sizeof(struct cds_lfht));
88 ht = calloc(1, cds_lfht_size);
89 assert(ht);
90
91 ht->mm = &cds_lfht_mm_chunk;
92
93 ht->min_nr_alloc_buckets = min_nr_alloc_buckets;
94 ht->min_alloc_buckets_order = get_count_order_ulong(min_nr_alloc_buckets);
95 ht->max_nr_buckets = max_nr_buckets;
96
97 ht->bucket_at = bucket_at;
98
99 return ht;
100}
101
102const struct cds_lfht_mm_type cds_lfht_mm_chunk = {
103 .alloc_cds_lfht = alloc_cds_lfht,
104 .alloc_bucket_table = cds_lfht_alloc_bucket_table,
105 .free_bucket_table = cds_lfht_free_bucket_table,
106 .bucket_at = bucket_at,
107};
This page took 0.024381 seconds and 4 git commands to generate.