rcuja: rename cds_ja_node to cds_ja_inode
[userspace-rcu.git] / rcuja / rcuja.c
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
23 #define _LGPL_SOURCE
24 #include <stdint.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <urcu/rcuja.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
30 #include <assert.h>
31 #include <urcu-pointer.h>
32 #include <stdint.h>
33
34 #include "rcuja-internal.h"
35 #include "bitfield.h"
36
37 enum cds_ja_type_class {
38 RCU_JA_LINEAR = 0, /* Type A */
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 */
44 RCU_JA_PIGEON = 2, /* Type C */
45 /* 32-bit: 101 to 256 children, 1024 bytes */
46 /* 64-bit: 113 to 256 children, 2048 bytes */
47 /* Leaf nodes are implicit from their height in the tree */
48 RCU_JA_NR_TYPES,
49
50 RCU_JA_NULL, /* not an encoded type, but keeps code regular */
51 };
52
53 struct cds_ja_type {
54 enum cds_ja_type_class type_class;
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 */
59 uint16_t nr_pool_order; /* number of pools */
60 uint16_t pool_size_order; /* pool size */
61 };
62
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
73 #define JA_BITS_PER_BYTE 3
74
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
81 /*
82 * Iteration on the array to find the right node size for the number of
83 * children stops when it reaches .max_child == 256 (this is the largest
84 * possible node size, which contains 256 children).
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.
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.
97 */
98
99 #if (CAA_BITS_PER_LONG < 64)
100 /* 32-bit pointers */
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,
110 ja_type_8_max_child = 0, /* NULL */
111 };
112
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
123 enum {
124 ja_type_5_nr_pool_order = 1,
125 ja_type_6_nr_pool_order = 2,
126 };
127
128 const struct cds_ja_type ja_types[] = {
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, },
134
135 /* Pools may fill sooner than max_child */
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, },
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 */
143 { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, },
144
145 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
146 };
147 #else /* !(CAA_BITS_PER_LONG < 64) */
148 /* 64-bit pointers */
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,
158 ja_type_8_max_child = 256,
159 };
160
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
171 enum {
172 ja_type_5_nr_pool_order = 1,
173 ja_type_6_nr_pool_order = 2,
174 };
175
176 const struct cds_ja_type ja_types[] = {
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, },
182
183 /* Pools may fill sooner than max_child. */
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, },
186
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 */
191 { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, },
192
193 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
194 };
195 #endif /* !(BITS_PER_LONG < 64) */
196
197 static inline __attribute__((unused))
198 void static_array_size_check(void)
199 {
200 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR);
201 }
202
203 /*
204 * The cds_ja_node contains the compressed node data needed for
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.
213 */
214
215 #define DECLARE_LINEAR_NODE(index) \
216 struct { \
217 uint8_t nr_child; \
218 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
219 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
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]; \
227 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
228 } linear[1U << ja_type_## index ##_nr_pool_order]; \
229 }
230
231 struct cds_ja_inode {
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 {
246 struct cds_ja_inode_flag *child[ja_type_7_max_child];
247 } conf_7;
248 /* data aliasing nodes for computed accesses */
249 uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child];
250 } u;
251 };
252
253 static
254 struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node,
255 unsigned int type)
256 {
257 assert(type < RCU_JA_NR_TYPES);
258 return (struct cds_ja_inode_flag *) (((unsigned long) node) | type);
259 }
260
261 static
262 struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node)
263 {
264 return (struct cds_ja_inode *) (((unsigned long) node) | JA_PTR_MASK);
265 }
266
267 static
268 unsigned int ja_node_type(struct cds_ja_inode_flag *node)
269 {
270 unsigned int type;
271
272 if (ja_node_ptr(node) == NULL) {
273 return NODE_INDEX_NULL;
274 }
275 type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK);
276 assert(type < RCU_JA_NR_TYPES);
277 return type;
278 }
279
280 struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type)
281 {
282 return calloc(1U << ja_type->order, sizeof(char));
283 }
284
285 void free_cds_ja_node(struct cds_ja_inode *node)
286 {
287 free(node);
288 }
289
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
296 uint8_t *align_ptr_size(uint8_t *ptr)
297 {
298 return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *));
299 }
300
301 static
302 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type,
303 struct cds_ja_inode *node)
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
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 */
314 static
315 struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type,
316 struct cds_ja_inode *node,
317 uint8_t n)
318 {
319 uint8_t nr_child;
320 uint8_t *values;
321 struct cds_ja_inode_flag **pointers;
322 struct cds_ja_inode_flag *ptr;
323 unsigned int i;
324
325 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
326
327 nr_child = ja_linear_node_get_nr_child(type, node);
328 cmm_smp_rmb(); /* read nr_child before values and pointers */
329 assert(nr_child <= type->max_linear_child);
330 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
331
332 values = &node->u.data[1];
333 for (i = 0; i < nr_child; i++) {
334 if (CMM_LOAD_SHARED(values[i]) == n)
335 break;
336 }
337 if (i >= nr_child)
338 return NULL;
339 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
340 ptr = rcu_dereference(pointers[i]);
341 assert(ja_node_ptr(ptr) != NULL);
342 return ptr;
343 }
344
345 static
346 struct cds_ja_inode_flag *ja_linear_node_get_ith_pos(const struct cds_ja_type *type,
347 struct cds_ja_inode *node,
348 uint8_t i,
349 uint8_t *v,
350 struct cds_ja_inode_flag **iter)
351 {
352 uint8_t *values;
353 struct cds_ja_inode_flag **pointers;
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];
360 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
361 *iter = pointers[i];
362 }
363
364 static
365 struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type,
366 struct cds_ja_inode *node,
367 uint8_t n)
368 {
369 struct cds_ja_inode *linear;
370
371 assert(type->type_class == RCU_JA_POOL);
372 /*
373 * TODO: currently, we select the pool by highest bits. We
374 * should support various encodings.
375 */
376 linear = (struct cds_ja_inode *)
377 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
378 return ja_linear_node_get_nth(type, linear, n);
379 }
380
381 static
382 struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type,
383 struct cds_ja_inode *node,
384 uint8_t i)
385 {
386 assert(type->type_class == RCU_JA_POOL);
387 return (struct cds_ja_inode *)
388 &node->u.data[(unsigned int) i << type->pool_size_order];
389 }
390
391 static
392 struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type,
393 struct cds_ja_inode *node,
394 uint8_t n)
395 {
396 assert(type->type_class == RCU_JA_PIGEON);
397 return rcu_dereference(((struct cds_ja_inode_flag **) node->u.data)[n]);
398 }
399
400 /*
401 * ja_node_get_nth: get nth item from a node.
402 * node_flag is already rcu_dereference'd.
403 */
404 static
405 struct cds_ja_inode_flag *ja_node_get_nth(struct cds_ja_inode_flag *node_flag,
406 uint8_t n)
407 {
408 unsigned int type_index;
409 struct cds_ja_inode *node;
410 const struct cds_ja_type *type;
411
412 node = ja_node_ptr(node_flag);
413 assert(node != NULL);
414 type_index = ja_node_type(node_flag);
415 type = &ja_types[type_index];
416
417 switch (type->type_class) {
418 case RCU_JA_LINEAR:
419 return ja_linear_node_get_nth(type, node, n);
420 case RCU_JA_POOL:
421 return ja_pool_node_get_nth(type, node, n);
422 case RCU_JA_PIGEON:
423 return ja_pigeon_node_get_nth(type, node, n);
424 default:
425 assert(0);
426 return (void *) -1UL;
427 }
428 }
429
430 /*
431 * TODO: use ja_get_nr_child to monitor limits triggering shrink
432 * recompaction.
433 * Also use ja_get_nr_child to make the difference between resize and
434 * pool change of compaction bit(s).
435 */
436 static
437 unsigned int ja_get_nr_child(struct cds_ja_shadow_node *shadow_node)
438 {
439 return shadow_node->nr_child;
440 }
441
442 static
443 int ja_linear_node_set_nth(const struct cds_ja_type *type,
444 struct cds_ja_inode *node,
445 struct cds_ja_shadow_node *shadow_node,
446 uint8_t n,
447 struct cds_ja_inode_flag *child_node_flag)
448 {
449 uint8_t nr_child;
450 uint8_t *values, *nr_child_ptr;
451 struct cds_ja_inode_flag **pointers;
452 unsigned int i;
453
454 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
455
456 nr_child_ptr = &node->u.data[0];
457 nr_child = *nr_child_ptr;
458 assert(nr_child <= type->max_linear_child);
459 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
460
461 values = &node->u.data[1];
462 for (i = 0; i < nr_child; i++) {
463 if (values[i] == n)
464 return -EEXIST;
465 }
466 if (nr_child >= type->max_linear_child) {
467 /* No space left in this node type */
468 return -ENOSPC;
469 }
470 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
471 assert(pointers[nr_child] == NULL);
472 rcu_assign_pointer(pointers[nr_child], child_node_flag);
473 CMM_STORE_SHARED(values[nr_child], n);
474 cmm_smp_wmb(); /* write value and pointer before nr_child */
475 CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
476 shadow_node->nr_child++;
477 return 0;
478 }
479
480 static
481 int ja_pool_node_set_nth(const struct cds_ja_type *type,
482 struct cds_ja_inode *node,
483 struct cds_ja_shadow_node *shadow_node,
484 uint8_t n,
485 struct cds_ja_inode_flag *child_node_flag)
486 {
487 struct cds_ja_inode *linear;
488
489 assert(type->type_class == RCU_JA_POOL);
490 linear = (struct cds_ja_inode *)
491 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
492 return ja_linear_node_set_nth(type, linear, shadow_node,
493 n, child_node_flag);
494 }
495
496 static
497 int ja_pigeon_node_set_nth(const struct cds_ja_type *type,
498 struct cds_ja_inode *node,
499 struct cds_ja_shadow_node *shadow_node,
500 uint8_t n,
501 struct cds_ja_inode_flag *child_node_flag)
502 {
503 struct cds_ja_inode_flag **ptr;
504
505 assert(type->type_class == RCU_JA_PIGEON);
506 ptr = &((struct cds_ja_inode_flag **) node->u.data)[n];
507 if (*ptr != NULL)
508 return -EEXIST;
509 rcu_assign_pointer(*ptr, child_node_flag);
510 shadow_node->nr_child++;
511 return 0;
512 }
513
514 /*
515 * _ja_node_set_nth: set nth item within a node. Return an error
516 * (negative error value) if it is already there.
517 * TODO: exclusive access on node.
518 */
519 static
520 int _ja_node_set_nth(const struct cds_ja_type *type,
521 struct cds_ja_inode *node,
522 struct cds_ja_shadow_node *shadow_node,
523 uint8_t n,
524 struct cds_ja_inode_flag *child_node_flag)
525 {
526 switch (type->type_class) {
527 case RCU_JA_LINEAR:
528 return ja_linear_node_set_nth(type, node, shadow_node, n,
529 child_node_flag);
530 case RCU_JA_POOL:
531 return ja_pool_node_set_nth(type, node, shadow_node, n,
532 child_node_flag);
533 case RCU_JA_PIGEON:
534 return ja_pigeon_node_set_nth(type, node, shadow_node, n,
535 child_node_flag);
536 case RCU_JA_NULL:
537 return -ENOSPC;
538 default:
539 assert(0);
540 return -EINVAL;
541 }
542
543 return 0;
544 }
545
546 /*
547 * ja_node_recompact_add: recompact a node, adding a new child.
548 * TODO: for pool type, take selection bit(s) into account.
549 */
550 static
551 int ja_node_recompact_add(struct cds_ja *ja,
552 unsigned int old_type_index,
553 const struct cds_ja_type *old_type,
554 struct cds_ja_inode *old_node,
555 struct cds_ja_shadow_node **shadow_node,
556 struct cds_ja_inode_flag **old_node_flag, uint8_t n,
557 struct cds_ja_inode_flag *child_node_flag)
558 {
559 unsigned int new_type_index;
560 struct cds_ja_inode *new_node;
561 const struct cds_ja_type *new_type;
562 struct cds_ja_inode_flag *new_node_flag;
563 int ret;
564
565 if (*shadow_node == NULL) {
566 new_type_index = 0;
567 } else {
568 new_type_index = old_type_index + 1;
569 }
570 new_type = &ja_types[new_type_index];
571 new_node = alloc_cds_ja_node(new_type);
572 if (!new_node)
573 return -ENOMEM;
574 new_node_flag = ja_node_flag(new_node, new_type_index);
575
576 ret = rcuja_shadow_set(ja->ht, new_node, *shadow_node);
577 if (ret)
578 return ret;
579
580 if (*shadow_node == NULL) {
581 *shadow_node = rcuja_shadow_lookup_lock(ja->ht, new_node);
582 assert(*shadow_node);
583 }
584
585 /*
586 * We need to clear nr_child, because it will be re-incremented
587 * by _ja_node_set_nth().
588 */
589 (*shadow_node)->nr_child = 0;
590
591 assert(old_type->type_class != RCU_JA_PIGEON);
592 switch (old_type->type_class) {
593 case RCU_JA_LINEAR:
594 {
595 uint8_t nr_child =
596 ja_linear_node_get_nr_child(old_type, old_node);
597 unsigned int i;
598
599 for (i = 0; i < nr_child; i++) {
600 struct cds_ja_inode_flag *iter;
601 uint8_t v;
602
603 ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter);
604 if (!iter)
605 continue;
606 ret = _ja_node_set_nth(new_type, new_node, *shadow_node,
607 v, iter);
608 assert(!ret);
609 }
610 break;
611 }
612 case RCU_JA_POOL:
613 {
614 unsigned int pool_nr;
615
616 for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) {
617 struct cds_ja_inode *pool =
618 ja_pool_node_get_ith_pool(old_type,
619 old_node, pool_nr);
620 uint8_t nr_child =
621 ja_linear_node_get_nr_child(old_type, pool);
622 unsigned int j;
623
624 for (j = 0; j < nr_child; j++) {
625 struct cds_ja_inode_flag *iter;
626 uint8_t v;
627
628 ja_linear_node_get_ith_pos(old_type, pool,
629 j, &v, &iter);
630 if (!iter)
631 continue;
632 ret = _ja_node_set_nth(new_type, new_node, *shadow_node,
633 v, iter);
634 assert(!ret);
635 }
636 }
637 break;
638 }
639 case RCU_JA_PIGEON:
640 default:
641 assert(0);
642 return -EINVAL;
643 }
644
645 /* add node */
646 ret = _ja_node_set_nth(new_type, new_node, *shadow_node,
647 n, child_node_flag);
648 assert(!ret);
649 /* Replace the old node with the new recompacted one */
650 rcu_assign_pointer(*old_node_flag, new_node_flag);
651 ret = rcuja_shadow_clear(ja->ht, old_node,
652 RCUJA_SHADOW_CLEAR_FREE_NODE);
653 assert(!ret);
654 return 0;
655 }
656
657 static
658 int ja_node_set_nth(struct cds_ja *ja,
659 struct cds_ja_inode_flag **node_flag, uint8_t n,
660 struct cds_ja_inode_flag *child_node_flag)
661 {
662 int ret;
663 unsigned int type_index;
664 const struct cds_ja_type *type;
665 struct cds_ja_inode *node;
666 struct cds_ja_shadow_node *shadow_node = NULL;
667
668 node = ja_node_ptr(*node_flag);
669 type_index = ja_node_type(*node_flag);
670 type = &ja_types[type_index];
671 if (node != NULL) {
672 shadow_node = rcuja_shadow_lookup_lock(ja->ht, node);
673 assert(shadow_node);
674 }
675 ret = _ja_node_set_nth(type, node, shadow_node,
676 n, child_node_flag);
677 if (ret == -ENOSPC) {
678 /* Not enough space in node, need to recompact. */
679 ret = ja_node_recompact_add(ja, type_index, type, node,
680 &shadow_node, node_flag, n, child_node_flag);
681 /* recompact always leave shadow_node locked */
682 }
683 rcuja_shadow_unlock(shadow_node);
684 return ret;
685 }
686
687 struct cds_ja_node *cds_ja_lookup(uint64_t key)
688 {
689 /* TODO */
690 }
691
692 struct cds_ja *_cds_ja_new(unsigned int key_bits,
693 const struct rcu_flavor_struct *flavor)
694 {
695 struct cds_ja *ja;
696
697
698 ja = calloc(sizeof(*ja), 1);
699 if (!ja)
700 goto ja_error;
701
702 switch (key_bits) {
703 case 8:
704 ja->key_max = UINT8_MAX;
705 break;
706 case 16:
707 ja->key_max = UINT16_MAX;
708 break;
709 case 32:
710 ja->key_max = UINT32_MAX;
711 break;
712 case 64:
713 ja->key_max = UINT64_MAX;
714 break;
715 default:
716 goto check_error;
717 }
718
719 /* ja->root is NULL */
720 ja->tree_depth = key_bits >> JA_BITS_PER_BYTE;
721 ja->ht = rcuja_create_ht(flavor);
722 if (!ja->ht)
723 goto ht_error;
724 return ja;
725
726 ht_error:
727 check_error:
728 free(ja);
729 ja_error:
730 return NULL;
731 }
732
733 /*
734 * There should be no more concurrent add to the judy array while it is
735 * being destroyed (ensured by the caller).
736 */
737 int cds_ja_destroy(struct cds_ja *ja)
738 {
739 int ret;
740
741 rcuja_shadow_prune(ja->ht,
742 RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK);
743 ret = rcuja_delete_ht(ja->ht);
744 if (ret)
745 return ret;
746 free(ja);
747 }
This page took 0.050684 seconds and 5 git commands to generate.