4 * Userspace RCU library - RCU Judy Array
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #include <urcu/rcuja.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
31 #include <urcu-pointer.h>
34 #include "rcuja-internal.h"
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 */
50 RCU_JA_NULL
, /* not an encoded type, but keeps code regular */
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 */
64 * Number of least significant pointer bits reserved to represent the
67 #define JA_TYPE_BITS 3
68 #define JA_TYPE_MAX_NR (1UL << JA_TYPE_BITS)
69 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
70 #define JA_PTR_MASK (~JA_TYPE_MASK)
72 #define JA_ENTRY_PER_NODE 256UL
73 #define JA_BITS_PER_BYTE 3
75 #define JA_MAX_DEPTH 5 /* Maximum depth, including leafs */
78 * Entry for NULL node is at index 8 of the table. It is never encoded
81 #define NODE_INDEX_NULL 8
84 * Iteration on the array to find the right node size for the number of
85 * children stops when it reaches .max_child == 256 (this is the largest
86 * possible node size, which contains 256 children).
87 * The min_child overlaps with the previous max_child to provide an
88 * hysteresis loop to reallocation for patterns of cyclic add/removal
89 * within the same node.
90 * The node the index within the following arrays is represented on 3
91 * bits. It identifies the node type, min/max number of children, and
93 * The max_child values for the RCU_JA_POOL below result from
94 * statistical approximation: over million populations, the max_child
95 * covers between 97% and 99% of the populations generated. Therefore, a
96 * fallback should exist to cover the rare extreme population unbalance
97 * cases, but it will not have a major impact on speed nor space
98 * consumption, since those are rare cases.
101 #if (CAA_BITS_PER_LONG < 64)
102 /* 32-bit pointers */
104 ja_type_0_max_child
= 1,
105 ja_type_1_max_child
= 3,
106 ja_type_2_max_child
= 6,
107 ja_type_3_max_child
= 12,
108 ja_type_4_max_child
= 25,
109 ja_type_5_max_child
= 48,
110 ja_type_6_max_child
= 92,
111 ja_type_7_max_child
= 256,
112 ja_type_8_max_child
= 0, /* NULL */
116 ja_type_0_max_linear_child
= 1,
117 ja_type_1_max_linear_child
= 3,
118 ja_type_2_max_linear_child
= 6,
119 ja_type_3_max_linear_child
= 12,
120 ja_type_4_max_linear_child
= 25,
121 ja_type_5_max_linear_child
= 24,
122 ja_type_6_max_linear_child
= 23,
126 ja_type_5_nr_pool_order
= 1,
127 ja_type_6_nr_pool_order
= 2,
130 const struct cds_ja_type ja_types
[] = {
131 { .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, },
132 { .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, },
133 { .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, },
134 { .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, },
135 { .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, },
137 /* Pools may fill sooner than max_child */
138 { .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, },
139 { .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, },
142 * TODO: Upon node removal below min_child, if child pool is
143 * filled beyond capacity, we need to roll back to pigeon.
145 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= ja_type_7_max_child
, .order
= 10, },
147 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
149 #else /* !(CAA_BITS_PER_LONG < 64) */
150 /* 64-bit pointers */
152 ja_type_0_max_child
= 1,
153 ja_type_1_max_child
= 3,
154 ja_type_2_max_child
= 7,
155 ja_type_3_max_child
= 14,
156 ja_type_4_max_child
= 28,
157 ja_type_5_max_child
= 54,
158 ja_type_6_max_child
= 104,
159 ja_type_7_max_child
= 256,
160 ja_type_8_max_child
= 256,
164 ja_type_0_max_linear_child
= 1,
165 ja_type_1_max_linear_child
= 3,
166 ja_type_2_max_linear_child
= 7,
167 ja_type_3_max_linear_child
= 14,
168 ja_type_4_max_linear_child
= 28,
169 ja_type_5_max_linear_child
= 27,
170 ja_type_6_max_linear_child
= 26,
174 ja_type_5_nr_pool_order
= 1,
175 ja_type_6_nr_pool_order
= 2,
178 const struct cds_ja_type ja_types
[] = {
179 { .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, },
180 { .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, },
181 { .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, },
182 { .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, },
183 { .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, },
185 /* Pools may fill sooner than max_child. */
186 { .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, },
187 { .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, },
190 * TODO: Upon node removal below min_child, if child pool is
191 * filled beyond capacity, we need to roll back to pigeon.
193 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= ja_type_7_max_child
, .order
= 11, },
195 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
197 #endif /* !(BITS_PER_LONG < 64) */
199 static inline __attribute__((unused
))
200 void static_array_size_check(void)
202 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
206 * The cds_ja_node contains the compressed node data needed for
207 * read-side. For linear and pool node configurations, it starts with a
208 * byte counting the number of children in the node. Then, the
209 * node-specific data is placed.
210 * The node mutex, if any is needed, protecting concurrent updated of
211 * each node is placed in a separate hash table indexed by node address.
212 * For the pigeon configuration, the number of children is also kept in
213 * a separate hash table, indexed by node address, because it is only
214 * required for updates.
217 #define DECLARE_LINEAR_NODE(index) \
220 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
221 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
224 #define DECLARE_POOL_NODE(index) \
228 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
229 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
230 } linear[1U << ja_type_## index ##_nr_pool_order]; \
233 struct cds_ja_inode
{
235 /* Linear configuration */
236 DECLARE_LINEAR_NODE(0) conf_0
;
237 DECLARE_LINEAR_NODE(1) conf_1
;
238 DECLARE_LINEAR_NODE(2) conf_2
;
239 DECLARE_LINEAR_NODE(3) conf_3
;
240 DECLARE_LINEAR_NODE(4) conf_4
;
242 /* Pool configuration */
243 DECLARE_POOL_NODE(5) conf_5
;
244 DECLARE_POOL_NODE(6) conf_6
;
246 /* Pigeon configuration */
248 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
250 /* data aliasing nodes for computed accesses */
251 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
256 struct cds_ja_inode_flag
*ja_node_flag(struct cds_ja_inode
*node
,
259 assert(type
< (1UL << JA_TYPE_BITS
));
260 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | type
);
264 struct cds_ja_inode
*ja_node_ptr(struct cds_ja_inode_flag
*node
)
266 return (struct cds_ja_inode
*) (((unsigned long) node
) & JA_PTR_MASK
);
270 unsigned long ja_node_type(struct cds_ja_inode_flag
*node
)
274 if (ja_node_ptr(node
) == NULL
) {
275 return NODE_INDEX_NULL
;
277 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
278 assert(type
< (1UL << JA_TYPE_BITS
));
282 struct cds_ja_inode
*alloc_cds_ja_node(const struct cds_ja_type
*ja_type
)
284 return calloc(1U << ja_type
->order
, sizeof(char));
287 void free_cds_ja_node(struct cds_ja_inode
*node
)
292 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
293 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
294 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
295 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
298 uint8_t *align_ptr_size(uint8_t *ptr
)
300 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
304 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
305 struct cds_ja_inode
*node
)
307 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
308 return CMM_LOAD_SHARED(node
->u
.data
[0]);
312 * The order in which values and pointers are does does not matter: if
313 * a value is missing, we return NULL. If a value is there, but its
314 * associated pointers is still NULL, we return NULL too.
317 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
318 struct cds_ja_inode
*node
,
319 struct cds_ja_inode_flag
***child_node_flag_ptr
,
324 struct cds_ja_inode_flag
**pointers
;
325 struct cds_ja_inode_flag
*ptr
;
328 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
330 nr_child
= ja_linear_node_get_nr_child(type
, node
);
331 cmm_smp_rmb(); /* read nr_child before values and pointers */
332 assert(nr_child
<= type
->max_linear_child
);
333 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
335 values
= &node
->u
.data
[1];
336 for (i
= 0; i
< nr_child
; i
++) {
337 if (CMM_LOAD_SHARED(values
[i
]) == n
)
342 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
343 if (caa_unlikely(child_node_flag_ptr
))
344 *child_node_flag_ptr
= &pointers
[i
];
345 ptr
= rcu_dereference(pointers
[i
]);
346 assert(ja_node_ptr(ptr
) != NULL
);
351 void ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
352 struct cds_ja_inode
*node
,
355 struct cds_ja_inode_flag
**iter
)
358 struct cds_ja_inode_flag
**pointers
;
360 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
361 assert(i
< ja_linear_node_get_nr_child(type
, node
));
363 values
= &node
->u
.data
[1];
365 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
370 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
371 struct cds_ja_inode
*node
,
372 struct cds_ja_inode_flag
***child_node_flag_ptr
,
375 struct cds_ja_inode
*linear
;
377 assert(type
->type_class
== RCU_JA_POOL
);
379 * TODO: currently, we select the pool by highest bits. We
380 * should support various encodings.
382 linear
= (struct cds_ja_inode
*)
383 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
384 return ja_linear_node_get_nth(type
, linear
, child_node_flag_ptr
, n
);
388 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
389 struct cds_ja_inode
*node
,
392 assert(type
->type_class
== RCU_JA_POOL
);
393 return (struct cds_ja_inode
*)
394 &node
->u
.data
[(unsigned int) i
<< type
->pool_size_order
];
398 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
399 struct cds_ja_inode
*node
,
400 struct cds_ja_inode_flag
***child_node_flag_ptr
,
403 struct cds_ja_inode_flag
**child_node_flag
;
405 assert(type
->type_class
== RCU_JA_PIGEON
);
406 child_node_flag
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
407 if (caa_unlikely(child_node_flag_ptr
))
408 *child_node_flag_ptr
= child_node_flag
;
409 return rcu_dereference(*child_node_flag
);
413 * ja_node_get_nth: get nth item from a node.
414 * node_flag is already rcu_dereference'd.
417 struct cds_ja_inode_flag
* ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
418 struct cds_ja_inode_flag
***child_node_flag_ptr
,
421 unsigned int type_index
;
422 struct cds_ja_inode
*node
;
423 const struct cds_ja_type
*type
;
425 node
= ja_node_ptr(node_flag
);
426 assert(node
!= NULL
);
427 type_index
= ja_node_type(node_flag
);
428 type
= &ja_types
[type_index
];
430 switch (type
->type_class
) {
432 return ja_linear_node_get_nth(type
, node
,
433 child_node_flag_ptr
, n
);
435 return ja_pool_node_get_nth(type
, node
,
436 child_node_flag_ptr
, n
);
438 return ja_pigeon_node_get_nth(type
, node
,
439 child_node_flag_ptr
, n
);
442 return (void *) -1UL;
447 * TODO: use ja_get_nr_child to monitor limits triggering shrink
449 * Also use ja_get_nr_child to make the difference between resize and
450 * pool change of compaction bit(s).
453 unsigned int ja_get_nr_child(struct cds_ja_shadow_node
*shadow_node
)
455 return shadow_node
->nr_child
;
459 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
460 struct cds_ja_inode
*node
,
461 struct cds_ja_shadow_node
*shadow_node
,
463 struct cds_ja_inode_flag
*child_node_flag
)
466 uint8_t *values
, *nr_child_ptr
;
467 struct cds_ja_inode_flag
**pointers
;
470 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
472 nr_child_ptr
= &node
->u
.data
[0];
473 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr
);
474 nr_child
= *nr_child_ptr
;
475 assert(nr_child
<= type
->max_linear_child
);
477 values
= &node
->u
.data
[1];
478 for (i
= 0; i
< nr_child
; i
++) {
482 if (nr_child
>= type
->max_linear_child
) {
483 /* No space left in this node type */
486 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
487 assert(pointers
[nr_child
] == NULL
);
488 rcu_assign_pointer(pointers
[nr_child
], child_node_flag
);
489 CMM_STORE_SHARED(values
[nr_child
], n
);
490 cmm_smp_wmb(); /* write value and pointer before nr_child */
491 CMM_STORE_SHARED(*nr_child_ptr
, nr_child
+ 1);
492 shadow_node
->nr_child
++;
493 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
494 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
495 (unsigned int) shadow_node
->nr_child
,
502 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
503 struct cds_ja_inode
*node
,
504 struct cds_ja_shadow_node
*shadow_node
,
506 struct cds_ja_inode_flag
*child_node_flag
)
508 struct cds_ja_inode
*linear
;
510 assert(type
->type_class
== RCU_JA_POOL
);
511 linear
= (struct cds_ja_inode
*)
512 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
513 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
518 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
519 struct cds_ja_inode
*node
,
520 struct cds_ja_shadow_node
*shadow_node
,
522 struct cds_ja_inode_flag
*child_node_flag
)
524 struct cds_ja_inode_flag
**ptr
;
526 assert(type
->type_class
== RCU_JA_PIGEON
);
527 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
530 rcu_assign_pointer(*ptr
, child_node_flag
);
531 shadow_node
->nr_child
++;
536 * _ja_node_set_nth: set nth item within a node. Return an error
537 * (negative error value) if it is already there.
538 * TODO: exclusive access on node.
541 int _ja_node_set_nth(const struct cds_ja_type
*type
,
542 struct cds_ja_inode
*node
,
543 struct cds_ja_shadow_node
*shadow_node
,
545 struct cds_ja_inode_flag
*child_node_flag
)
547 switch (type
->type_class
) {
549 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
552 return ja_pool_node_set_nth(type
, node
, shadow_node
, n
,
555 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
568 * ja_node_recompact_add: recompact a node, adding a new child.
569 * TODO: for pool type, take selection bit(s) into account.
570 * Return 0 on success, -ENOENT if need to retry, or other negative
571 * error value otherwise.
574 int ja_node_recompact_add(struct cds_ja
*ja
,
575 unsigned int old_type_index
,
576 const struct cds_ja_type
*old_type
,
577 struct cds_ja_inode
*old_node
,
578 struct cds_ja_shadow_node
*shadow_node
,
579 struct cds_ja_inode_flag
**old_node_flag
, uint8_t n
,
580 struct cds_ja_inode_flag
*child_node_flag
)
582 unsigned int new_type_index
;
583 struct cds_ja_inode
*new_node
;
584 const struct cds_ja_type
*new_type
;
585 struct cds_ja_inode_flag
*new_node_flag
;
589 if (!shadow_node
|| old_type_index
== NODE_INDEX_NULL
) {
592 new_type_index
= old_type_index
+ 1;
594 dbg_printf("Recompact to type %d\n", new_type_index
);
596 new_type
= &ja_types
[new_type_index
];
597 new_node
= alloc_cds_ja_node(new_type
);
600 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
602 dbg_printf("Recompact inherit lock from %p\n", shadow_node
);
603 ret
= rcuja_shadow_set(ja
->ht
, new_node
, shadow_node
);
610 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, new_node
);
616 * We need to clear nr_child, because it will be re-incremented
617 * by _ja_node_set_nth().
619 shadow_node
->nr_child
= 0;
621 assert(old_type
->type_class
!= RCU_JA_PIGEON
);
622 switch (old_type
->type_class
) {
626 ja_linear_node_get_nr_child(old_type
, old_node
);
629 for (i
= 0; i
< nr_child
; i
++) {
630 struct cds_ja_inode_flag
*iter
;
633 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
636 ret
= _ja_node_set_nth(new_type
, new_node
, shadow_node
,
644 unsigned int pool_nr
;
646 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
647 struct cds_ja_inode
*pool
=
648 ja_pool_node_get_ith_pool(old_type
,
651 ja_linear_node_get_nr_child(old_type
, pool
);
654 for (j
= 0; j
< nr_child
; j
++) {
655 struct cds_ja_inode_flag
*iter
;
658 ja_linear_node_get_ith_pos(old_type
, pool
,
662 ret
= _ja_node_set_nth(new_type
, new_node
, shadow_node
,
670 /* Nothing to copy */
676 goto unlock_new_shadow
;
680 ret
= _ja_node_set_nth(new_type
, new_node
, shadow_node
,
683 /* Return pointer to new recompacted new through old_node_flag */
684 *old_node_flag
= new_node_flag
;
686 ret
= rcuja_shadow_clear(ja
->ht
, old_node
, shadow_node
,
687 RCUJA_SHADOW_CLEAR_FREE_NODE
);
695 rcuja_shadow_unlock(shadow_node
);
700 * Return 0 on success, -ENOENT if need to retry, or other negative
701 * error value otherwise.
704 int ja_node_set_nth(struct cds_ja
*ja
,
705 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
706 struct cds_ja_inode_flag
*child_node_flag
,
707 struct cds_ja_shadow_node
*shadow_node
)
710 unsigned int type_index
;
711 const struct cds_ja_type
*type
;
712 struct cds_ja_inode
*node
;
714 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
715 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
717 node
= ja_node_ptr(*node_flag
);
718 type_index
= ja_node_type(*node_flag
);
719 type
= &ja_types
[type_index
];
720 ret
= _ja_node_set_nth(type
, node
, shadow_node
,
722 if (ret
== -ENOSPC
) {
723 /* Not enough space in node, need to recompact. */
724 ret
= ja_node_recompact_add(ja
, type_index
, type
, node
,
725 shadow_node
, node_flag
, n
, child_node_flag
);
730 struct cds_hlist_head
*cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
732 unsigned int tree_depth
, i
;
733 struct cds_ja_inode_flag
*node_flag
;
735 if (caa_unlikely(key
> ja
->key_max
))
737 tree_depth
= ja
->tree_depth
;
738 node_flag
= rcu_dereference(ja
->root
);
740 /* level 0: root node */
741 if (!ja_node_ptr(node_flag
))
744 for (i
= 1; i
< tree_depth
; i
++) {
745 node_flag
= ja_node_get_nth(node_flag
, NULL
,
746 (unsigned char) key
);
747 if (!ja_node_ptr(node_flag
))
749 key
>>= JA_BITS_PER_BYTE
;
752 /* Last level lookup succeded. We got an actual match. */
753 return (struct cds_hlist_head
*) node_flag
;
757 * We reached an unpopulated node. Create it and the children we need,
758 * and then attach the entire branch to the current node. This may
759 * trigger recompaction of the current node. Locks needed: node lock
760 * (for add), and, possibly, parent node lock (to update pointer due to
761 * node recompaction).
763 * First take node lock, check if recompaction is needed, then take
764 * parent lock (if needed). Then we can proceed to create the new
765 * branch. Publish the new branch, and release locks.
766 * TODO: we currently always take the parent lock even when not needed.
769 int ja_attach_node(struct cds_ja
*ja
,
770 struct cds_ja_inode_flag
**node_flag_ptr
,
771 struct cds_ja_inode_flag
*node_flag
,
772 struct cds_ja_inode_flag
*parent_node_flag
,
775 struct cds_ja_node
*child_node
)
777 struct cds_ja_shadow_node
*shadow_node
= NULL
,
778 *parent_shadow_node
= NULL
;
779 struct cds_ja_inode
*node
= ja_node_ptr(node_flag
);
780 struct cds_ja_inode
*parent_node
= ja_node_ptr(parent_node_flag
);
781 struct cds_hlist_head head
;
782 struct cds_ja_inode_flag
*iter_node_flag
, *iter_dest_node_flag
;
784 struct cds_ja_inode_flag
*created_nodes
[JA_MAX_DEPTH
];
785 int nr_created_nodes
= 0;
787 dbg_printf("Attach node at depth %u\n", depth
);
790 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, node
);
796 parent_shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
798 if (!parent_shadow_node
) {
804 /* Create new branch, starting from bottom */
805 CDS_INIT_HLIST_HEAD(&head
);
806 cds_hlist_add_head_rcu(&child_node
->list
, &head
);
807 iter_node_flag
= (struct cds_ja_inode_flag
*) head
.next
;
809 /* Create shadow node for the leaf node */
810 dbg_printf("leaf shadow node creation\n");
811 ret
= rcuja_shadow_set(ja
->ht
, ja_node_ptr(iter_node_flag
), NULL
);
814 created_nodes
[nr_created_nodes
++] = iter_node_flag
;
816 for (i
= ja
->tree_depth
- 1; i
>= (int) depth
; i
--) {
817 dbg_printf("branch creation level %d, key %" PRIu64
"\n",
818 i
, key
>> (JA_BITS_PER_BYTE
* (i
- 2)));
819 iter_dest_node_flag
= NULL
;
820 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
821 key
>> (JA_BITS_PER_BYTE
* (i
- 2)),
826 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
827 iter_node_flag
= iter_dest_node_flag
;
831 /* We need to use set_nth on the previous level. */
832 iter_dest_node_flag
= node_flag
;
833 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
834 key
>> (JA_BITS_PER_BYTE
* (depth
- 2)),
839 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
840 iter_node_flag
= iter_dest_node_flag
;
843 /* Publish new branch */
844 dbg_printf("Publish branch %p, replacing %p\n",
845 iter_node_flag
, *node_flag_ptr
);
846 rcu_assign_pointer(*node_flag_ptr
, iter_node_flag
);
853 for (i
= 0; i
< nr_created_nodes
; i
++) {
857 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
859 flags
|= RCUJA_SHADOW_CLEAR_FREE_NODE
;
860 tmpret
= rcuja_shadow_clear(ja
->ht
,
861 ja_node_ptr(created_nodes
[i
]),
867 if (parent_shadow_node
)
868 rcuja_shadow_unlock(parent_shadow_node
);
871 rcuja_shadow_unlock(shadow_node
);
877 * Lock the hlist head shadow node mutex, and add node to list of
878 * duplicates. Failure can happen if concurrent removal removes the last
879 * node with same key before we get the lock.
880 * Return 0 on success, negative error value on failure.
883 int ja_chain_node(struct cds_ja
*ja
,
884 struct cds_hlist_head
*head
,
885 struct cds_ja_node
*node
)
887 struct cds_ja_shadow_node
*shadow_node
;
889 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
890 (struct cds_ja_inode
*) head
);
893 cds_hlist_add_head_rcu(&node
->list
, head
);
894 rcuja_shadow_unlock(shadow_node
);
898 int cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
899 struct cds_ja_node
*new_node
)
901 unsigned int tree_depth
, i
;
903 struct cds_ja_inode_flag
**node_flag_ptr
; /* in parent */
904 struct cds_ja_inode_flag
*node_flag
,
909 if (caa_unlikely(key
> ja
->key_max
))
911 tree_depth
= ja
->tree_depth
;
914 dbg_printf("cds_ja_add attempt: key %" PRIu64
", node %p\n",
917 parent2_node_flag
= NULL
;
919 (struct cds_ja_inode_flag
*) &ja
->root
; /* Use root ptr address as key for mutex */
920 node_flag_ptr
= &ja
->root
;
921 node_flag
= rcu_dereference(*node_flag_ptr
);
923 /* Iterate on all internal levels */
924 for (i
= 1; i
< tree_depth
; i
++) {
925 if (!ja_node_ptr(node_flag
)) {
926 ret
= ja_attach_node(ja
, node_flag_ptr
,
927 parent_node_flag
, parent2_node_flag
,
929 if (ret
== -ENOENT
|| ret
== -EEXIST
)
934 parent2_node_flag
= parent_node_flag
;
935 parent_node_flag
= node_flag
;
936 node_flag
= ja_node_get_nth(node_flag
,
938 (unsigned char) iter_key
);
939 iter_key
>>= JA_BITS_PER_BYTE
;
943 * We reached bottom of tree, simply add node to last internal
944 * level, or chain it if key is already present.
946 if (!ja_node_ptr(node_flag
)) {
947 ret
= ja_attach_node(ja
, node_flag_ptr
, parent_node_flag
,
948 parent2_node_flag
, key
, i
, new_node
);
950 ret
= ja_chain_node(ja
,
951 (struct cds_hlist_head
*) ja_node_ptr(node_flag
),
960 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
961 const struct rcu_flavor_struct
*flavor
)
966 ja
= calloc(sizeof(*ja
), 1);
972 ja
->key_max
= UINT8_MAX
;
975 ja
->key_max
= UINT16_MAX
;
978 ja
->key_max
= UINT32_MAX
;
981 ja
->key_max
= UINT64_MAX
;
987 /* ja->root is NULL */
988 /* tree_depth 0 is for pointer to root node */
989 ja
->tree_depth
= (key_bits
>> JA_BITS_PER_BYTE
) + 1;
990 assert(ja
->tree_depth
<= JA_MAX_DEPTH
);
991 ja
->ht
= rcuja_create_ht(flavor
);
996 * Note: we should not free this node until judy array destroy.
998 ret
= rcuja_shadow_set(ja
->ht
,
999 ja_node_ptr((struct cds_ja_inode_flag
*) &ja
->root
),
1007 ret
= rcuja_delete_ht(ja
->ht
);
1017 * There should be no more concurrent add to the judy array while it is
1018 * being destroyed (ensured by the caller).
1020 int cds_ja_destroy(struct cds_ja
*ja
)
1024 rcuja_shadow_prune(ja
->ht
,
1025 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
);
1026 ret
= rcuja_delete_ht(ja
->ht
);