add rculfhash-mm-mmap.c memory management
[urcu.git] / rculfhash-internal.h
1 #ifndef _URCU_RCULFHASH_INTERNAL_H
2 #define _URCU_RCULFHASH_INTERNAL_H
3
4 /*
5 * urcu/rculfhash-internal.h
6 *
7 * Internal header for Lock-Free RCU Hash Table
8 *
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 */
26
27 #include <urcu/rculfhash.h>
28
29 #ifdef DEBUG
30 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
31 #else
32 #define dbg_printf(fmt, args...)
33 #endif
34
35 #if (CAA_BITS_PER_LONG == 32)
36 #define MAX_TABLE_ORDER 32
37 #else
38 #define MAX_TABLE_ORDER 64
39 #endif
40
41 #define MAX_CHUNK_TABLE (1UL << 10)
42
43 #ifndef min
44 #define min(a, b) ((a) < (b) ? (a) : (b))
45 #endif
46
47 #ifndef max
48 #define max(a, b) ((a) > (b) ? (a) : (b))
49 #endif
50
51 struct ht_items_count;
52
53 /*
54 * cds_lfht: Top-level data structure representing a lock-free hash
55 * table. Defined in the implementation file to make it be an opaque
56 * cookie to users.
57 */
58 struct cds_lfht {
59 unsigned long size; /* always a power of 2, shared (RCU) */
60 int flags;
61
62 /*
63 * We need to put the work threads offline (QSBR) when taking this
64 * mutex, because we use synchronize_rcu within this mutex critical
65 * section, which waits on read-side critical sections, and could
66 * therefore cause grace-period deadlock if we hold off RCU G.P.
67 * completion.
68 */
69 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
70 pthread_attr_t *resize_attr; /* Resize threads attributes */
71 unsigned int in_progress_resize, in_progress_destroy;
72 unsigned long resize_target;
73 int resize_initiated;
74 const struct rcu_flavor_struct *flavor;
75
76 long count; /* global approximate item count */
77 struct ht_items_count *split_count; /* split item count */
78
79 /* memory management related fields are located at the end */
80 const struct cds_lfht_mm_type *mm;
81
82 unsigned long min_alloc_buckets_order;
83 unsigned long min_nr_alloc_buckets;
84 unsigned long max_nr_buckets;
85
86 struct cds_lfht_node *(*bucket_at)(struct cds_lfht *ht,
87 unsigned long index);
88
89 union {
90 /*
91 * Contains the per order-index-level bucket node table.
92 * The size of each bucket node table is half the number
93 * of hashes contained in this order (except for order 0).
94 * The minimum allocation buckets size parameter allows
95 * combining the bucket node arrays of the lowermost
96 * levels to improve cache locality for small index orders.
97 */
98 struct cds_lfht_node *tbl_order[MAX_TABLE_ORDER];
99
100 /*
101 * Contains the bucket node chunks. The size of each
102 * bucket node chunk is ->min_alloc_size (we avoid to
103 * allocate chunks with different size). Chunks improve
104 * cache locality for small index orders, and are more
105 * friendly with environments where allocation of large
106 * contiguous memory areas is challenging due to memory
107 * fragmentation concerns or inability to use virtual
108 * memory addressing.
109 */
110 struct cds_lfht_node *tbl_chunk[0];
111
112 /*
113 * Memory mapping with room for all possible buckets.
114 * Their memory is allocated when needed.
115 */
116 struct cds_lfht_node *tbl_mmap;
117 };
118 };
119
120 extern unsigned int fls_ulong(unsigned long x);
121 extern int get_count_order_ulong(unsigned long x);
122
123 #ifdef POISON_FREE
124 #define poison_free(ptr) \
125 do { \
126 if (ptr) { \
127 memset(ptr, 0x42, sizeof(*(ptr))); \
128 free(ptr); \
129 } \
130 } while (0)
131 #else
132 #define poison_free(ptr) free(ptr)
133 #endif
134
135 #endif /* _URCU_RCULFHASH_INTERNAL_H */
This page took 0.044329 seconds and 5 git commands to generate.