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 (1U << 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
76 * Entry for NULL node is at index 8 of the table. It is never encoded
79 #define NODE_INDEX_NULL 8
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
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.
99 #if (CAA_BITS_PER_LONG < 64)
100 /* 32-bit pointers */
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 */
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,
124 ja_type_5_nr_pool_order
= 1,
125 ja_type_6_nr_pool_order
= 2,
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, },
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, },
140 * TODO: Upon node removal below min_child, if child pool is
141 * filled beyond capacity, we need to roll back to pigeon.
143 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= ja_type_7_max_child
, .order
= 10, },
145 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
147 #else /* !(CAA_BITS_PER_LONG < 64) */
148 /* 64-bit pointers */
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,
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,
172 ja_type_5_nr_pool_order
= 1,
173 ja_type_6_nr_pool_order
= 2,
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, },
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, },
188 * TODO: Upon node removal below min_child, if child pool is
189 * filled beyond capacity, we need to roll back to pigeon.
191 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= ja_type_7_max_child
, .order
= 11, },
193 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
195 #endif /* !(BITS_PER_LONG < 64) */
197 static inline __attribute__((unused
))
198 void static_array_size_check(void)
200 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
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.
215 #define DECLARE_LINEAR_NODE(index) \
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]; \
222 #define DECLARE_POOL_NODE(index) \
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]; \
231 struct cds_ja_inode
{
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
;
240 /* Pool configuration */
241 DECLARE_POOL_NODE(5) conf_5
;
242 DECLARE_POOL_NODE(6) conf_6
;
244 /* Pigeon configuration */
246 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
248 /* data aliasing nodes for computed accesses */
249 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
254 struct cds_ja_inode_flag
*ja_node_flag(struct cds_ja_inode
*node
,
257 assert(type
< RCU_JA_NR_TYPES
);
258 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | type
);
262 struct cds_ja_inode
*ja_node_ptr(struct cds_ja_inode_flag
*node
)
264 return (struct cds_ja_inode
*) (((unsigned long) node
) | JA_PTR_MASK
);
268 unsigned int ja_node_type(struct cds_ja_inode_flag
*node
)
272 if (ja_node_ptr(node
) == NULL
) {
273 return NODE_INDEX_NULL
;
275 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
276 assert(type
< RCU_JA_NR_TYPES
);
280 struct cds_ja_inode
*alloc_cds_ja_node(const struct cds_ja_type
*ja_type
)
282 return calloc(1U << ja_type
->order
, sizeof(char));
285 void free_cds_ja_node(struct cds_ja_inode
*node
)
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)
296 uint8_t *align_ptr_size(uint8_t *ptr
)
298 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
302 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
303 struct cds_ja_inode
*node
)
305 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
306 return CMM_LOAD_SHARED(node
->u
.data
[0]);
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.
315 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
316 struct cds_ja_inode
*node
,
321 struct cds_ja_inode_flag
**pointers
;
322 struct cds_ja_inode_flag
*ptr
;
325 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
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
);
332 values
= &node
->u
.data
[1];
333 for (i
= 0; i
< nr_child
; i
++) {
334 if (CMM_LOAD_SHARED(values
[i
]) == n
)
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
);
346 struct cds_ja_inode_flag
*ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
347 struct cds_ja_inode
*node
,
350 struct cds_ja_inode_flag
**iter
)
353 struct cds_ja_inode_flag
**pointers
;
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
));
358 values
= &node
->u
.data
[1];
360 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
365 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
366 struct cds_ja_inode
*node
,
369 struct cds_ja_inode
*linear
;
371 assert(type
->type_class
== RCU_JA_POOL
);
373 * TODO: currently, we select the pool by highest bits. We
374 * should support various encodings.
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
);
382 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
383 struct cds_ja_inode
*node
,
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
];
392 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
393 struct cds_ja_inode
*node
,
396 assert(type
->type_class
== RCU_JA_PIGEON
);
397 return rcu_dereference(((struct cds_ja_inode_flag
**) node
->u
.data
)[n
]);
401 * ja_node_get_nth: get nth item from a node.
402 * node_flag is already rcu_dereference'd.
405 struct cds_ja_inode_flag
* ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
408 unsigned int type_index
;
409 struct cds_ja_inode
*node
;
410 const struct cds_ja_type
*type
;
412 node
= ja_node_ptr(node_flag
);
413 if (caa_unlikely(node
== NULL
))
415 type_index
= ja_node_type(node_flag
);
416 type
= &ja_types
[type_index
];
418 switch (type
->type_class
) {
420 return ja_linear_node_get_nth(type
, node
, n
);
422 return ja_pool_node_get_nth(type
, node
, n
);
424 return ja_pigeon_node_get_nth(type
, node
, n
);
427 return (void *) -1UL;
432 * TODO: use ja_get_nr_child to monitor limits triggering shrink
434 * Also use ja_get_nr_child to make the difference between resize and
435 * pool change of compaction bit(s).
438 unsigned int ja_get_nr_child(struct cds_ja_shadow_node
*shadow_node
)
440 return shadow_node
->nr_child
;
444 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
445 struct cds_ja_inode
*node
,
446 struct cds_ja_shadow_node
*shadow_node
,
448 struct cds_ja_inode_flag
*child_node_flag
)
451 uint8_t *values
, *nr_child_ptr
;
452 struct cds_ja_inode_flag
**pointers
;
455 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
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
);
462 values
= &node
->u
.data
[1];
463 for (i
= 0; i
< nr_child
; i
++) {
467 if (nr_child
>= type
->max_linear_child
) {
468 /* No space left in this node type */
471 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
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);
477 shadow_node
->nr_child
++;
482 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
483 struct cds_ja_inode
*node
,
484 struct cds_ja_shadow_node
*shadow_node
,
486 struct cds_ja_inode_flag
*child_node_flag
)
488 struct cds_ja_inode
*linear
;
490 assert(type
->type_class
== RCU_JA_POOL
);
491 linear
= (struct cds_ja_inode
*)
492 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
493 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
498 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
499 struct cds_ja_inode
*node
,
500 struct cds_ja_shadow_node
*shadow_node
,
502 struct cds_ja_inode_flag
*child_node_flag
)
504 struct cds_ja_inode_flag
**ptr
;
506 assert(type
->type_class
== RCU_JA_PIGEON
);
507 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
510 rcu_assign_pointer(*ptr
, child_node_flag
);
511 shadow_node
->nr_child
++;
516 * _ja_node_set_nth: set nth item within a node. Return an error
517 * (negative error value) if it is already there.
518 * TODO: exclusive access on node.
521 int _ja_node_set_nth(const struct cds_ja_type
*type
,
522 struct cds_ja_inode
*node
,
523 struct cds_ja_shadow_node
*shadow_node
,
525 struct cds_ja_inode_flag
*child_node_flag
)
527 switch (type
->type_class
) {
529 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
532 return ja_pool_node_set_nth(type
, node
, shadow_node
, n
,
535 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
548 * ja_node_recompact_add: recompact a node, adding a new child.
549 * TODO: for pool type, take selection bit(s) into account.
552 int ja_node_recompact_add(struct cds_ja
*ja
,
553 unsigned int old_type_index
,
554 const struct cds_ja_type
*old_type
,
555 struct cds_ja_inode
*old_node
,
556 struct cds_ja_shadow_node
**shadow_node
,
557 struct cds_ja_inode_flag
**old_node_flag
, uint8_t n
,
558 struct cds_ja_inode_flag
*child_node_flag
)
560 unsigned int new_type_index
;
561 struct cds_ja_inode
*new_node
;
562 const struct cds_ja_type
*new_type
;
563 struct cds_ja_inode_flag
*new_node_flag
;
566 if (*shadow_node
== NULL
) {
569 new_type_index
= old_type_index
+ 1;
571 new_type
= &ja_types
[new_type_index
];
572 new_node
= alloc_cds_ja_node(new_type
);
575 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
577 ret
= rcuja_shadow_set(ja
->ht
, new_node
, *shadow_node
);
581 if (*shadow_node
== NULL
) {
582 *shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, new_node
);
583 assert(*shadow_node
);
587 * We need to clear nr_child, because it will be re-incremented
588 * by _ja_node_set_nth().
590 (*shadow_node
)->nr_child
= 0;
592 assert(old_type
->type_class
!= RCU_JA_PIGEON
);
593 switch (old_type
->type_class
) {
597 ja_linear_node_get_nr_child(old_type
, old_node
);
600 for (i
= 0; i
< nr_child
; i
++) {
601 struct cds_ja_inode_flag
*iter
;
604 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
607 ret
= _ja_node_set_nth(new_type
, new_node
, *shadow_node
,
615 unsigned int pool_nr
;
617 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
618 struct cds_ja_inode
*pool
=
619 ja_pool_node_get_ith_pool(old_type
,
622 ja_linear_node_get_nr_child(old_type
, pool
);
625 for (j
= 0; j
< nr_child
; j
++) {
626 struct cds_ja_inode_flag
*iter
;
629 ja_linear_node_get_ith_pos(old_type
, pool
,
633 ret
= _ja_node_set_nth(new_type
, new_node
, *shadow_node
,
647 ret
= _ja_node_set_nth(new_type
, new_node
, *shadow_node
,
650 /* Replace the old node with the new recompacted one */
651 rcu_assign_pointer(*old_node_flag
, new_node_flag
);
652 ret
= rcuja_shadow_clear(ja
->ht
, old_node
,
653 RCUJA_SHADOW_CLEAR_FREE_NODE
);
659 int ja_node_set_nth(struct cds_ja
*ja
,
660 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
661 struct cds_ja_inode_flag
*child_node_flag
)
664 unsigned int type_index
;
665 const struct cds_ja_type
*type
;
666 struct cds_ja_inode
*node
;
667 struct cds_ja_shadow_node
*shadow_node
= NULL
;
669 node
= ja_node_ptr(*node_flag
);
670 type_index
= ja_node_type(*node_flag
);
671 type
= &ja_types
[type_index
];
673 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, node
);
676 ret
= _ja_node_set_nth(type
, node
, shadow_node
,
678 if (ret
== -ENOSPC
) {
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 */
684 rcuja_shadow_unlock(shadow_node
);
688 struct cds_ja_node
*cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
690 unsigned int tree_depth
, i
;
691 struct cds_ja_inode_flag
*node_flag
;
693 if (caa_unlikely(key
> ja
->key_max
))
695 tree_depth
= ja
->tree_depth
;
696 node_flag
= ja
->root
;
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
))
703 key
>>= JA_BITS_PER_BYTE
;
706 /* Last level lookup succeded. We got an actual node. */
707 return (struct cds_ja_node
*) node_flag
;
710 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
711 const struct rcu_flavor_struct
*flavor
)
716 ja
= calloc(sizeof(*ja
), 1);
722 ja
->key_max
= UINT8_MAX
;
725 ja
->key_max
= UINT16_MAX
;
728 ja
->key_max
= UINT32_MAX
;
731 ja
->key_max
= UINT64_MAX
;
737 /* ja->root is NULL */
738 ja
->tree_depth
= key_bits
>> JA_BITS_PER_BYTE
;
739 ja
->ht
= rcuja_create_ht(flavor
);
752 * There should be no more concurrent add to the judy array while it is
753 * being destroyed (ensured by the caller).
755 int cds_ja_destroy(struct cds_ja
*ja
)
759 rcuja_shadow_prune(ja
->ht
,
760 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
);
761 ret
= rcuja_delete_ht(ja
->ht
);