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