Commit | Line | Data |
---|---|---|
61009379 MD |
1 | /* |
2 | * rcuja/rcuja.c | |
3 | * | |
4 | * Userspace RCU library - RCU Judy Array | |
5 | * | |
6 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.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 | ||
195e72d3 | 23 | #define _LGPL_SOURCE |
e5227865 | 24 | #include <stdint.h> |
8e519e3c | 25 | #include <errno.h> |
d68c6810 | 26 | #include <limits.h> |
61009379 | 27 | #include <urcu/rcuja.h> |
d68c6810 MD |
28 | #include <urcu/compiler.h> |
29 | #include <urcu/arch.h> | |
30 | #include <assert.h> | |
8e519e3c | 31 | #include <urcu-pointer.h> |
b4540e8a | 32 | #include <stdint.h> |
8e519e3c | 33 | |
61009379 | 34 | #include "rcuja-internal.h" |
d68c6810 | 35 | #include "bitfield.h" |
61009379 | 36 | |
d96bfb0d | 37 | enum cds_ja_type_class { |
e5227865 | 38 | RCU_JA_LINEAR = 0, /* Type A */ |
fd800776 MD |
39 | /* 32-bit: 1 to 25 children, 8 to 128 bytes */ |
40 | /* 64-bit: 1 to 28 children, 16 to 256 bytes */ | |
41 | RCU_JA_POOL = 1, /* Type B */ | |
42 | /* 32-bit: 26 to 100 children, 256 to 512 bytes */ | |
43 | /* 64-bit: 29 to 112 children, 512 to 1024 bytes */ | |
e5227865 | 44 | RCU_JA_PIGEON = 2, /* Type C */ |
fd800776 MD |
45 | /* 32-bit: 101 to 256 children, 1024 bytes */ |
46 | /* 64-bit: 113 to 256 children, 2048 bytes */ | |
e5227865 | 47 | /* Leaf nodes are implicit from their height in the tree */ |
1db4943c | 48 | RCU_JA_NR_TYPES, |
e1db2db5 MD |
49 | |
50 | RCU_JA_NULL, /* not an encoded type, but keeps code regular */ | |
e5227865 MD |
51 | }; |
52 | ||
d96bfb0d MD |
53 | struct cds_ja_type { |
54 | enum cds_ja_type_class type_class; | |
8e519e3c MD |
55 | uint16_t min_child; /* minimum number of children: 1 to 256 */ |
56 | uint16_t max_child; /* maximum number of children: 1 to 256 */ | |
57 | uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */ | |
58 | uint16_t order; /* node size is (1 << order), in bytes */ | |
fd800776 MD |
59 | uint16_t nr_pool_order; /* number of pools */ |
60 | uint16_t pool_size_order; /* pool size */ | |
e5227865 MD |
61 | }; |
62 | ||
d68c6810 MD |
63 | /* |
64 | * Number of least significant pointer bits reserved to represent the | |
65 | * child type. | |
66 | */ | |
67 | #define JA_TYPE_BITS 3 | |
68 | #define JA_TYPE_MAX_NR (1U << JA_TYPE_BITS) | |
69 | #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1) | |
70 | #define JA_PTR_MASK (~JA_TYPE_MASK) | |
71 | ||
72 | #define JA_ENTRY_PER_NODE 256UL | |
b4540e8a | 73 | #define JA_BITS_PER_BYTE 3 |
d68c6810 | 74 | |
e1db2db5 MD |
75 | /* |
76 | * Entry for NULL node is at index 8 of the table. It is never encoded | |
77 | * in flags. | |
78 | */ | |
79 | #define NODE_INDEX_NULL 8 | |
80 | ||
e5227865 MD |
81 | /* |
82 | * Iteration on the array to find the right node size for the number of | |
d68c6810 | 83 | * children stops when it reaches .max_child == 256 (this is the largest |
e5227865 | 84 | * possible node size, which contains 256 children). |
d68c6810 MD |
85 | * The min_child overlaps with the previous max_child to provide an |
86 | * hysteresis loop to reallocation for patterns of cyclic add/removal | |
87 | * within the same node. | |
88 | * The node the index within the following arrays is represented on 3 | |
89 | * bits. It identifies the node type, min/max number of children, and | |
90 | * the size order. | |
3d45251f MD |
91 | * The max_child values for the RCU_JA_POOL below result from |
92 | * statistical approximation: over million populations, the max_child | |
93 | * covers between 97% and 99% of the populations generated. Therefore, a | |
94 | * fallback should exist to cover the rare extreme population unbalance | |
95 | * cases, but it will not have a major impact on speed nor space | |
96 | * consumption, since those are rare cases. | |
e5227865 | 97 | */ |
e5227865 | 98 | |
d68c6810 MD |
99 | #if (CAA_BITS_PER_LONG < 64) |
100 | /* 32-bit pointers */ | |
1db4943c MD |
101 | enum { |
102 | ja_type_0_max_child = 1, | |
103 | ja_type_1_max_child = 3, | |
104 | ja_type_2_max_child = 6, | |
105 | ja_type_3_max_child = 12, | |
106 | ja_type_4_max_child = 25, | |
107 | ja_type_5_max_child = 48, | |
108 | ja_type_6_max_child = 92, | |
109 | ja_type_7_max_child = 256, | |
e1db2db5 | 110 | ja_type_8_max_child = 0, /* NULL */ |
1db4943c MD |
111 | }; |
112 | ||
8e519e3c MD |
113 | enum { |
114 | ja_type_0_max_linear_child = 1, | |
115 | ja_type_1_max_linear_child = 3, | |
116 | ja_type_2_max_linear_child = 6, | |
117 | ja_type_3_max_linear_child = 12, | |
118 | ja_type_4_max_linear_child = 25, | |
119 | ja_type_5_max_linear_child = 24, | |
120 | ja_type_6_max_linear_child = 23, | |
121 | }; | |
122 | ||
1db4943c MD |
123 | enum { |
124 | ja_type_5_nr_pool_order = 1, | |
125 | ja_type_6_nr_pool_order = 2, | |
126 | }; | |
127 | ||
d96bfb0d | 128 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
129 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_0_max_child, .max_linear_child = ja_type_0_max_linear_child, .order = 3, }, |
130 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_1_max_child, .max_linear_child = ja_type_1_max_linear_child, .order = 4, }, | |
131 | { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = ja_type_2_max_child, .max_linear_child = ja_type_2_max_linear_child, .order = 5, }, | |
132 | { .type_class = RCU_JA_LINEAR, .min_child = 4, .max_child = ja_type_3_max_child, .max_linear_child = ja_type_3_max_linear_child, .order = 6, }, | |
133 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = ja_type_4_max_child, .max_linear_child = ja_type_4_max_linear_child, .order = 7, }, | |
e5227865 | 134 | |
fd800776 | 135 | /* Pools may fill sooner than max_child */ |
8e519e3c MD |
136 | { .type_class = RCU_JA_POOL, .min_child = 20, .max_child = ja_type_5_max_child, .max_linear_child = ja_type_5_max_linear_child, .order = 8, .nr_pool_order = ja_type_5_nr_pool_order, .pool_size_order = 7, }, |
137 | { .type_class = RCU_JA_POOL, .min_child = 45, .max_child = ja_type_6_max_child, .max_linear_child = ja_type_6_max_linear_child, .order = 9, .nr_pool_order = ja_type_6_nr_pool_order, .pool_size_order = 7, }, | |
3d45251f MD |
138 | |
139 | /* | |
140 | * TODO: Upon node removal below min_child, if child pool is | |
141 | * filled beyond capacity, we need to roll back to pigeon. | |
142 | */ | |
1db4943c | 143 | { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, }, |
e1db2db5 MD |
144 | |
145 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
d68c6810 | 146 | }; |
d68c6810 MD |
147 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
148 | /* 64-bit pointers */ | |
1db4943c MD |
149 | enum { |
150 | ja_type_0_max_child = 1, | |
151 | ja_type_1_max_child = 3, | |
152 | ja_type_2_max_child = 7, | |
153 | ja_type_3_max_child = 14, | |
154 | ja_type_4_max_child = 28, | |
155 | ja_type_5_max_child = 54, | |
156 | ja_type_6_max_child = 104, | |
157 | ja_type_7_max_child = 256, | |
e1db2db5 | 158 | ja_type_8_max_child = 256, |
1db4943c MD |
159 | }; |
160 | ||
8e519e3c MD |
161 | enum { |
162 | ja_type_0_max_linear_child = 1, | |
163 | ja_type_1_max_linear_child = 3, | |
164 | ja_type_2_max_linear_child = 7, | |
165 | ja_type_3_max_linear_child = 14, | |
166 | ja_type_4_max_linear_child = 28, | |
167 | ja_type_5_max_linear_child = 27, | |
168 | ja_type_6_max_linear_child = 26, | |
169 | }; | |
170 | ||
1db4943c MD |
171 | enum { |
172 | ja_type_5_nr_pool_order = 1, | |
173 | ja_type_6_nr_pool_order = 2, | |
174 | }; | |
175 | ||
d96bfb0d | 176 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
177 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_0_max_child, .max_linear_child = ja_type_0_max_linear_child, .order = 4, }, |
178 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_1_max_child, .max_linear_child = ja_type_1_max_linear_child, .order = 5, }, | |
179 | { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = ja_type_2_max_child, .max_linear_child = ja_type_2_max_linear_child, .order = 6, }, | |
180 | { .type_class = RCU_JA_LINEAR, .min_child = 5, .max_child = ja_type_3_max_child, .max_linear_child = ja_type_3_max_linear_child, .order = 7, }, | |
181 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = ja_type_4_max_child, .max_linear_child = ja_type_4_max_linear_child, .order = 8, }, | |
e5227865 | 182 | |
3d45251f | 183 | /* Pools may fill sooner than max_child. */ |
8e519e3c MD |
184 | { .type_class = RCU_JA_POOL, .min_child = 22, .max_child = ja_type_5_max_child, .max_linear_child = ja_type_5_max_linear_child, .order = 9, .nr_pool_order = ja_type_5_nr_pool_order, .pool_size_order = 8, }, |
185 | { .type_class = RCU_JA_POOL, .min_child = 51, .max_child = ja_type_6_max_child, .max_linear_child = ja_type_6_max_linear_child, .order = 10, .nr_pool_order = ja_type_6_nr_pool_order, .pool_size_order = 8, }, | |
e5227865 | 186 | |
3d45251f MD |
187 | /* |
188 | * TODO: Upon node removal below min_child, if child pool is | |
189 | * filled beyond capacity, we need to roll back to pigeon. | |
190 | */ | |
1db4943c | 191 | { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, }, |
e1db2db5 MD |
192 | |
193 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
e5227865 | 194 | }; |
d68c6810 | 195 | #endif /* !(BITS_PER_LONG < 64) */ |
e5227865 | 196 | |
1db4943c MD |
197 | static inline __attribute__((unused)) |
198 | void static_array_size_check(void) | |
199 | { | |
e1db2db5 | 200 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR); |
1db4943c MD |
201 | } |
202 | ||
e5227865 | 203 | /* |
d96bfb0d | 204 | * The cds_ja_node contains the compressed node data needed for |
1db4943c MD |
205 | * read-side. For linear and pool node configurations, it starts with a |
206 | * byte counting the number of children in the node. Then, the | |
207 | * node-specific data is placed. | |
208 | * The node mutex, if any is needed, protecting concurrent updated of | |
209 | * each node is placed in a separate hash table indexed by node address. | |
210 | * For the pigeon configuration, the number of children is also kept in | |
211 | * a separate hash table, indexed by node address, because it is only | |
212 | * required for updates. | |
e5227865 | 213 | */ |
1db4943c | 214 | |
ff38c745 MD |
215 | #define DECLARE_LINEAR_NODE(index) \ |
216 | struct { \ | |
217 | uint8_t nr_child; \ | |
218 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 219 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
220 | } |
221 | ||
222 | #define DECLARE_POOL_NODE(index) \ | |
223 | struct { \ | |
224 | struct { \ | |
225 | uint8_t nr_child; \ | |
226 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 227 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
228 | } linear[1U << ja_type_## index ##_nr_pool_order]; \ |
229 | } | |
1db4943c | 230 | |
b4540e8a | 231 | struct cds_ja_inode { |
1db4943c MD |
232 | union { |
233 | /* Linear configuration */ | |
234 | DECLARE_LINEAR_NODE(0) conf_0; | |
235 | DECLARE_LINEAR_NODE(1) conf_1; | |
236 | DECLARE_LINEAR_NODE(2) conf_2; | |
237 | DECLARE_LINEAR_NODE(3) conf_3; | |
238 | DECLARE_LINEAR_NODE(4) conf_4; | |
239 | ||
240 | /* Pool configuration */ | |
241 | DECLARE_POOL_NODE(5) conf_5; | |
242 | DECLARE_POOL_NODE(6) conf_6; | |
243 | ||
244 | /* Pigeon configuration */ | |
245 | struct { | |
b4540e8a | 246 | struct cds_ja_inode_flag *child[ja_type_7_max_child]; |
1db4943c MD |
247 | } conf_7; |
248 | /* data aliasing nodes for computed accesses */ | |
b4540e8a | 249 | uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child]; |
1db4943c | 250 | } u; |
e5227865 MD |
251 | }; |
252 | ||
d68c6810 | 253 | static |
b4540e8a | 254 | struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node, |
8e519e3c | 255 | unsigned int type) |
d68c6810 | 256 | { |
1db4943c | 257 | assert(type < RCU_JA_NR_TYPES); |
b4540e8a | 258 | return (struct cds_ja_inode_flag *) (((unsigned long) node) | type); |
d68c6810 MD |
259 | } |
260 | ||
e1db2db5 | 261 | static |
b4540e8a | 262 | struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node) |
e1db2db5 | 263 | { |
b4540e8a | 264 | return (struct cds_ja_inode *) (((unsigned long) node) | JA_PTR_MASK); |
e1db2db5 MD |
265 | } |
266 | ||
d68c6810 | 267 | static |
b4540e8a | 268 | unsigned int ja_node_type(struct cds_ja_inode_flag *node) |
d68c6810 MD |
269 | { |
270 | unsigned int type; | |
271 | ||
e1db2db5 MD |
272 | if (ja_node_ptr(node) == NULL) { |
273 | return NODE_INDEX_NULL; | |
274 | } | |
d68c6810 | 275 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); |
1db4943c | 276 | assert(type < RCU_JA_NR_TYPES); |
d68c6810 MD |
277 | return type; |
278 | } | |
279 | ||
b4540e8a | 280 | struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type) |
e5227865 | 281 | { |
1db4943c | 282 | return calloc(1U << ja_type->order, sizeof(char)); |
e5227865 MD |
283 | } |
284 | ||
b4540e8a | 285 | void free_cds_ja_node(struct cds_ja_inode *node) |
e5227865 MD |
286 | { |
287 | free(node); | |
288 | } | |
289 | ||
d68c6810 MD |
290 | #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask)) |
291 | #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1) | |
292 | #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask)) | |
293 | #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1) | |
294 | ||
295 | static | |
1db4943c | 296 | uint8_t *align_ptr_size(uint8_t *ptr) |
d68c6810 | 297 | { |
1db4943c | 298 | return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *)); |
d68c6810 MD |
299 | } |
300 | ||
11c5e016 | 301 | static |
d96bfb0d | 302 | uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type, |
b4540e8a | 303 | struct cds_ja_inode *node) |
11c5e016 MD |
304 | { |
305 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
306 | return CMM_LOAD_SHARED(node->u.data[0]); | |
307 | } | |
308 | ||
13a7f5a6 MD |
309 | /* |
310 | * The order in which values and pointers are does does not matter: if | |
311 | * a value is missing, we return NULL. If a value is there, but its | |
312 | * associated pointers is still NULL, we return NULL too. | |
313 | */ | |
d68c6810 | 314 | static |
b4540e8a MD |
315 | struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type, |
316 | struct cds_ja_inode *node, | |
8e519e3c | 317 | uint8_t n) |
d68c6810 MD |
318 | { |
319 | uint8_t nr_child; | |
320 | uint8_t *values; | |
b4540e8a MD |
321 | struct cds_ja_inode_flag **pointers; |
322 | struct cds_ja_inode_flag *ptr; | |
d68c6810 MD |
323 | unsigned int i; |
324 | ||
8e519e3c | 325 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); |
d68c6810 | 326 | |
11c5e016 | 327 | nr_child = ja_linear_node_get_nr_child(type, node); |
13a7f5a6 | 328 | cmm_smp_rmb(); /* read nr_child before values and pointers */ |
8e519e3c MD |
329 | assert(nr_child <= type->max_linear_child); |
330 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
d68c6810 | 331 | |
1db4943c | 332 | values = &node->u.data[1]; |
d68c6810 | 333 | for (i = 0; i < nr_child; i++) { |
13a7f5a6 | 334 | if (CMM_LOAD_SHARED(values[i]) == n) |
d68c6810 MD |
335 | break; |
336 | } | |
337 | if (i >= nr_child) | |
338 | return NULL; | |
b4540e8a | 339 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
13a7f5a6 | 340 | ptr = rcu_dereference(pointers[i]); |
d68c6810 MD |
341 | assert(ja_node_ptr(ptr) != NULL); |
342 | return ptr; | |
343 | } | |
344 | ||
11c5e016 | 345 | static |
b4540e8a MD |
346 | struct cds_ja_inode_flag *ja_linear_node_get_ith_pos(const struct cds_ja_type *type, |
347 | struct cds_ja_inode *node, | |
11c5e016 MD |
348 | uint8_t i, |
349 | uint8_t *v, | |
b4540e8a | 350 | struct cds_ja_inode_flag **iter) |
11c5e016 MD |
351 | { |
352 | uint8_t *values; | |
b4540e8a | 353 | struct cds_ja_inode_flag **pointers; |
11c5e016 MD |
354 | |
355 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
356 | assert(i < ja_linear_node_get_nr_child(type, node)); | |
357 | ||
358 | values = &node->u.data[1]; | |
359 | *v = values[i]; | |
b4540e8a | 360 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
11c5e016 MD |
361 | *iter = pointers[i]; |
362 | } | |
363 | ||
d68c6810 | 364 | static |
b4540e8a MD |
365 | struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type, |
366 | struct cds_ja_inode *node, | |
8e519e3c | 367 | uint8_t n) |
d68c6810 | 368 | { |
b4540e8a | 369 | struct cds_ja_inode *linear; |
d68c6810 | 370 | |
fd800776 | 371 | assert(type->type_class == RCU_JA_POOL); |
e1db2db5 MD |
372 | /* |
373 | * TODO: currently, we select the pool by highest bits. We | |
374 | * should support various encodings. | |
375 | */ | |
b4540e8a | 376 | linear = (struct cds_ja_inode *) |
1db4943c | 377 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
8e519e3c | 378 | return ja_linear_node_get_nth(type, linear, n); |
d68c6810 MD |
379 | } |
380 | ||
11c5e016 | 381 | static |
b4540e8a MD |
382 | struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type, |
383 | struct cds_ja_inode *node, | |
11c5e016 MD |
384 | uint8_t i) |
385 | { | |
386 | assert(type->type_class == RCU_JA_POOL); | |
b4540e8a | 387 | return (struct cds_ja_inode *) |
11c5e016 MD |
388 | &node->u.data[(unsigned int) i << type->pool_size_order]; |
389 | } | |
390 | ||
d68c6810 | 391 | static |
b4540e8a MD |
392 | struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type, |
393 | struct cds_ja_inode *node, | |
8e519e3c | 394 | uint8_t n) |
d68c6810 MD |
395 | { |
396 | assert(type->type_class == RCU_JA_PIGEON); | |
b4540e8a | 397 | return rcu_dereference(((struct cds_ja_inode_flag **) node->u.data)[n]); |
d68c6810 MD |
398 | } |
399 | ||
13a7f5a6 MD |
400 | /* |
401 | * ja_node_get_nth: get nth item from a node. | |
402 | * node_flag is already rcu_dereference'd. | |
403 | */ | |
d68c6810 | 404 | static |
41975c12 | 405 | struct cds_ja_inode_flag * ja_node_get_nth(struct cds_ja_inode_flag *node_flag, |
8e519e3c | 406 | uint8_t n) |
d68c6810 MD |
407 | { |
408 | unsigned int type_index; | |
b4540e8a | 409 | struct cds_ja_inode *node; |
d96bfb0d | 410 | const struct cds_ja_type *type; |
d68c6810 | 411 | |
d68c6810 | 412 | node = ja_node_ptr(node_flag); |
41975c12 MD |
413 | if (caa_unlikely(node == NULL)) |
414 | return NULL; | |
d68c6810 MD |
415 | type_index = ja_node_type(node_flag); |
416 | type = &ja_types[type_index]; | |
417 | ||
418 | switch (type->type_class) { | |
419 | case RCU_JA_LINEAR: | |
420 | return ja_linear_node_get_nth(type, node, n); | |
fd800776 MD |
421 | case RCU_JA_POOL: |
422 | return ja_pool_node_get_nth(type, node, n); | |
d68c6810 MD |
423 | case RCU_JA_PIGEON: |
424 | return ja_pigeon_node_get_nth(type, node, n); | |
425 | default: | |
426 | assert(0); | |
427 | return (void *) -1UL; | |
428 | } | |
429 | } | |
430 | ||
335d8b18 MD |
431 | /* |
432 | * TODO: use ja_get_nr_child to monitor limits triggering shrink | |
433 | * recompaction. | |
434 | * Also use ja_get_nr_child to make the difference between resize and | |
435 | * pool change of compaction bit(s). | |
436 | */ | |
e1db2db5 | 437 | static |
d96bfb0d | 438 | unsigned int ja_get_nr_child(struct cds_ja_shadow_node *shadow_node) |
e1db2db5 MD |
439 | { |
440 | return shadow_node->nr_child; | |
441 | } | |
442 | ||
8e519e3c | 443 | static |
d96bfb0d | 444 | int ja_linear_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 445 | struct cds_ja_inode *node, |
d96bfb0d | 446 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 447 | uint8_t n, |
b4540e8a | 448 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c MD |
449 | { |
450 | uint8_t nr_child; | |
451 | uint8_t *values, *nr_child_ptr; | |
b4540e8a | 452 | struct cds_ja_inode_flag **pointers; |
8e519e3c MD |
453 | unsigned int i; |
454 | ||
455 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
456 | ||
457 | nr_child_ptr = &node->u.data[0]; | |
458 | nr_child = *nr_child_ptr; | |
459 | assert(nr_child <= type->max_linear_child); | |
460 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
461 | ||
462 | values = &node->u.data[1]; | |
463 | for (i = 0; i < nr_child; i++) { | |
464 | if (values[i] == n) | |
465 | return -EEXIST; | |
466 | } | |
467 | if (nr_child >= type->max_linear_child) { | |
468 | /* No space left in this node type */ | |
469 | return -ENOSPC; | |
470 | } | |
b4540e8a | 471 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
13a7f5a6 MD |
472 | assert(pointers[nr_child] == NULL); |
473 | rcu_assign_pointer(pointers[nr_child], child_node_flag); | |
474 | CMM_STORE_SHARED(values[nr_child], n); | |
475 | cmm_smp_wmb(); /* write value and pointer before nr_child */ | |
476 | CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1); | |
e1db2db5 | 477 | shadow_node->nr_child++; |
8e519e3c MD |
478 | return 0; |
479 | } | |
480 | ||
481 | static | |
d96bfb0d | 482 | int ja_pool_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 483 | struct cds_ja_inode *node, |
d96bfb0d | 484 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 485 | uint8_t n, |
b4540e8a | 486 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 487 | { |
b4540e8a | 488 | struct cds_ja_inode *linear; |
8e519e3c MD |
489 | |
490 | assert(type->type_class == RCU_JA_POOL); | |
b4540e8a | 491 | linear = (struct cds_ja_inode *) |
8e519e3c | 492 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
e1db2db5 MD |
493 | return ja_linear_node_set_nth(type, linear, shadow_node, |
494 | n, child_node_flag); | |
8e519e3c MD |
495 | } |
496 | ||
497 | static | |
d96bfb0d | 498 | int ja_pigeon_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 499 | struct cds_ja_inode *node, |
d96bfb0d | 500 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 501 | uint8_t n, |
b4540e8a | 502 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 503 | { |
b4540e8a | 504 | struct cds_ja_inode_flag **ptr; |
8e519e3c MD |
505 | |
506 | assert(type->type_class == RCU_JA_PIGEON); | |
b4540e8a | 507 | ptr = &((struct cds_ja_inode_flag **) node->u.data)[n]; |
8e519e3c MD |
508 | if (*ptr != NULL) |
509 | return -EEXIST; | |
510 | rcu_assign_pointer(*ptr, child_node_flag); | |
e1db2db5 | 511 | shadow_node->nr_child++; |
8e519e3c MD |
512 | return 0; |
513 | } | |
514 | ||
d68c6810 | 515 | /* |
7a0b2331 | 516 | * _ja_node_set_nth: set nth item within a node. Return an error |
8e519e3c MD |
517 | * (negative error value) if it is already there. |
518 | * TODO: exclusive access on node. | |
d68c6810 | 519 | */ |
8e519e3c | 520 | static |
d96bfb0d | 521 | int _ja_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 522 | struct cds_ja_inode *node, |
d96bfb0d | 523 | struct cds_ja_shadow_node *shadow_node, |
e1db2db5 | 524 | uint8_t n, |
b4540e8a | 525 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 526 | { |
8e519e3c MD |
527 | switch (type->type_class) { |
528 | case RCU_JA_LINEAR: | |
e1db2db5 | 529 | return ja_linear_node_set_nth(type, node, shadow_node, n, |
8e519e3c MD |
530 | child_node_flag); |
531 | case RCU_JA_POOL: | |
e1db2db5 | 532 | return ja_pool_node_set_nth(type, node, shadow_node, n, |
8e519e3c MD |
533 | child_node_flag); |
534 | case RCU_JA_PIGEON: | |
e1db2db5 | 535 | return ja_pigeon_node_set_nth(type, node, shadow_node, n, |
8e519e3c | 536 | child_node_flag); |
e1db2db5 MD |
537 | case RCU_JA_NULL: |
538 | return -ENOSPC; | |
8e519e3c MD |
539 | default: |
540 | assert(0); | |
541 | return -EINVAL; | |
542 | } | |
543 | ||
544 | return 0; | |
545 | } | |
7a0b2331 MD |
546 | |
547 | /* | |
548 | * ja_node_recompact_add: recompact a node, adding a new child. | |
e1db2db5 | 549 | * TODO: for pool type, take selection bit(s) into account. |
7a0b2331 MD |
550 | */ |
551 | static | |
d96bfb0d | 552 | int ja_node_recompact_add(struct cds_ja *ja, |
e1db2db5 | 553 | unsigned int old_type_index, |
d96bfb0d | 554 | const struct cds_ja_type *old_type, |
b4540e8a | 555 | struct cds_ja_inode *old_node, |
d96bfb0d | 556 | struct cds_ja_shadow_node **shadow_node, |
b4540e8a MD |
557 | struct cds_ja_inode_flag **old_node_flag, uint8_t n, |
558 | struct cds_ja_inode_flag *child_node_flag) | |
7a0b2331 | 559 | { |
e1db2db5 | 560 | unsigned int new_type_index; |
b4540e8a | 561 | struct cds_ja_inode *new_node; |
d96bfb0d | 562 | const struct cds_ja_type *new_type; |
b4540e8a | 563 | struct cds_ja_inode_flag *new_node_flag; |
7a0b2331 MD |
564 | int ret; |
565 | ||
e1db2db5 | 566 | if (*shadow_node == NULL) { |
7a0b2331 MD |
567 | new_type_index = 0; |
568 | } else { | |
7a0b2331 MD |
569 | new_type_index = old_type_index + 1; |
570 | } | |
571 | new_type = &ja_types[new_type_index]; | |
d96bfb0d | 572 | new_node = alloc_cds_ja_node(new_type); |
7a0b2331 MD |
573 | if (!new_node) |
574 | return -ENOMEM; | |
575 | new_node_flag = ja_node_flag(new_node, new_type_index); | |
576 | ||
e1db2db5 MD |
577 | ret = rcuja_shadow_set(ja->ht, new_node, *shadow_node); |
578 | if (ret) | |
579 | return ret; | |
580 | ||
581 | if (*shadow_node == NULL) { | |
582 | *shadow_node = rcuja_shadow_lookup_lock(ja->ht, new_node); | |
583 | assert(*shadow_node); | |
584 | } | |
585 | ||
11c5e016 MD |
586 | /* |
587 | * We need to clear nr_child, because it will be re-incremented | |
588 | * by _ja_node_set_nth(). | |
589 | */ | |
e1db2db5 | 590 | (*shadow_node)->nr_child = 0; |
11c5e016 MD |
591 | |
592 | assert(old_type->type_class != RCU_JA_PIGEON); | |
593 | switch (old_type->type_class) { | |
594 | case RCU_JA_LINEAR: | |
595 | { | |
596 | uint8_t nr_child = | |
597 | ja_linear_node_get_nr_child(old_type, old_node); | |
598 | unsigned int i; | |
599 | ||
600 | for (i = 0; i < nr_child; i++) { | |
b4540e8a | 601 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
602 | uint8_t v; |
603 | ||
604 | ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter); | |
605 | if (!iter) | |
606 | continue; | |
607 | ret = _ja_node_set_nth(new_type, new_node, *shadow_node, | |
608 | v, iter); | |
609 | assert(!ret); | |
610 | } | |
611 | break; | |
612 | } | |
613 | case RCU_JA_POOL: | |
614 | { | |
615 | unsigned int pool_nr; | |
616 | ||
617 | for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) { | |
b4540e8a | 618 | struct cds_ja_inode *pool = |
11c5e016 MD |
619 | ja_pool_node_get_ith_pool(old_type, |
620 | old_node, pool_nr); | |
621 | uint8_t nr_child = | |
622 | ja_linear_node_get_nr_child(old_type, pool); | |
623 | unsigned int j; | |
624 | ||
625 | for (j = 0; j < nr_child; j++) { | |
b4540e8a | 626 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
627 | uint8_t v; |
628 | ||
629 | ja_linear_node_get_ith_pos(old_type, pool, | |
630 | j, &v, &iter); | |
631 | if (!iter) | |
632 | continue; | |
633 | ret = _ja_node_set_nth(new_type, new_node, *shadow_node, | |
634 | v, iter); | |
635 | assert(!ret); | |
636 | } | |
637 | } | |
638 | break; | |
7a0b2331 | 639 | } |
11c5e016 MD |
640 | case RCU_JA_PIGEON: |
641 | default: | |
642 | assert(0); | |
643 | return -EINVAL; | |
644 | } | |
645 | ||
7a0b2331 | 646 | /* add node */ |
e1db2db5 MD |
647 | ret = _ja_node_set_nth(new_type, new_node, *shadow_node, |
648 | n, child_node_flag); | |
7a0b2331 | 649 | assert(!ret); |
8eba1a4b MD |
650 | /* Replace the old node with the new recompacted one */ |
651 | rcu_assign_pointer(*old_node_flag, new_node_flag); | |
e1db2db5 MD |
652 | ret = rcuja_shadow_clear(ja->ht, old_node, |
653 | RCUJA_SHADOW_CLEAR_FREE_NODE); | |
654 | assert(!ret); | |
7a0b2331 MD |
655 | return 0; |
656 | } | |
657 | ||
658 | static | |
d96bfb0d | 659 | int ja_node_set_nth(struct cds_ja *ja, |
b4540e8a MD |
660 | struct cds_ja_inode_flag **node_flag, uint8_t n, |
661 | struct cds_ja_inode_flag *child_node_flag) | |
7a0b2331 MD |
662 | { |
663 | int ret; | |
e1db2db5 | 664 | unsigned int type_index; |
d96bfb0d | 665 | const struct cds_ja_type *type; |
b4540e8a | 666 | struct cds_ja_inode *node; |
d96bfb0d | 667 | struct cds_ja_shadow_node *shadow_node = NULL; |
7a0b2331 | 668 | |
e1db2db5 MD |
669 | node = ja_node_ptr(*node_flag); |
670 | type_index = ja_node_type(*node_flag); | |
671 | type = &ja_types[type_index]; | |
672 | if (node != NULL) { | |
673 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, node); | |
674 | assert(shadow_node); | |
675 | } | |
676 | ret = _ja_node_set_nth(type, node, shadow_node, | |
677 | n, child_node_flag); | |
7a0b2331 | 678 | if (ret == -ENOSPC) { |
e1db2db5 MD |
679 | /* Not enough space in node, need to recompact. */ |
680 | ret = ja_node_recompact_add(ja, type_index, type, node, | |
681 | &shadow_node, node_flag, n, child_node_flag); | |
682 | /* recompact always leave shadow_node locked */ | |
7a0b2331 | 683 | } |
e1db2db5 | 684 | rcuja_shadow_unlock(shadow_node); |
7a0b2331 MD |
685 | return ret; |
686 | } | |
be9a7474 | 687 | |
41975c12 | 688 | struct cds_ja_node *cds_ja_lookup(struct cds_ja *ja, uint64_t key) |
b4540e8a | 689 | { |
41975c12 MD |
690 | unsigned int tree_depth, i; |
691 | struct cds_ja_inode_flag *node_flag; | |
692 | ||
693 | if (caa_unlikely(key > ja->key_max)) | |
694 | return NULL; | |
695 | tree_depth = ja->tree_depth; | |
696 | node_flag = ja->root; | |
697 | ||
698 | for (i = 0; i < tree_depth; i++) { | |
699 | node_flag = ja_node_get_nth(node_flag, | |
700 | (unsigned char) key); | |
701 | if (!ja_node_ptr(node_flag)) | |
702 | return NULL; | |
703 | key >>= JA_BITS_PER_BYTE; | |
704 | } | |
705 | ||
706 | /* Last level lookup succeded. We got an actual node. */ | |
707 | return (struct cds_ja_node *) node_flag; | |
b4540e8a MD |
708 | } |
709 | ||
710 | struct cds_ja *_cds_ja_new(unsigned int key_bits, | |
711 | const struct rcu_flavor_struct *flavor) | |
be9a7474 MD |
712 | { |
713 | struct cds_ja *ja; | |
714 | ||
b4540e8a | 715 | |
be9a7474 MD |
716 | ja = calloc(sizeof(*ja), 1); |
717 | if (!ja) | |
718 | goto ja_error; | |
b4540e8a MD |
719 | |
720 | switch (key_bits) { | |
721 | case 8: | |
722 | ja->key_max = UINT8_MAX; | |
723 | break; | |
724 | case 16: | |
725 | ja->key_max = UINT16_MAX; | |
726 | break; | |
727 | case 32: | |
728 | ja->key_max = UINT32_MAX; | |
729 | break; | |
730 | case 64: | |
731 | ja->key_max = UINT64_MAX; | |
732 | break; | |
733 | default: | |
734 | goto check_error; | |
735 | } | |
736 | ||
be9a7474 | 737 | /* ja->root is NULL */ |
b4540e8a | 738 | ja->tree_depth = key_bits >> JA_BITS_PER_BYTE; |
be9a7474 MD |
739 | ja->ht = rcuja_create_ht(flavor); |
740 | if (!ja->ht) | |
741 | goto ht_error; | |
742 | return ja; | |
743 | ||
744 | ht_error: | |
b4540e8a | 745 | check_error: |
be9a7474 MD |
746 | free(ja); |
747 | ja_error: | |
748 | return NULL; | |
749 | } | |
750 | ||
751 | /* | |
752 | * There should be no more concurrent add to the judy array while it is | |
753 | * being destroyed (ensured by the caller). | |
754 | */ | |
755 | int cds_ja_destroy(struct cds_ja *ja) | |
756 | { | |
b4540e8a MD |
757 | int ret; |
758 | ||
be9a7474 MD |
759 | rcuja_shadow_prune(ja->ht, |
760 | RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK); | |
b4540e8a MD |
761 | ret = rcuja_delete_ht(ja->ht); |
762 | if (ret) | |
763 | return ret; | |
764 | free(ja); | |
41975c12 | 765 | return 0; |
be9a7474 | 766 | } |