Commit | Line | Data |
---|---|---|
61009379 MD |
1 | /* |
2 | * rcuja/rcuja.c | |
3 | * | |
4 | * Userspace RCU library - RCU Judy Array | |
5 | * | |
6 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
e5227865 | 23 | #include <stdint.h> |
8e519e3c | 24 | #include <errno.h> |
d68c6810 | 25 | #include <limits.h> |
61009379 | 26 | #include <urcu/rcuja.h> |
d68c6810 MD |
27 | #include <urcu/compiler.h> |
28 | #include <urcu/arch.h> | |
29 | #include <assert.h> | |
8e519e3c MD |
30 | #include <urcu-pointer.h> |
31 | ||
61009379 | 32 | #include "rcuja-internal.h" |
d68c6810 | 33 | #include "bitfield.h" |
61009379 | 34 | |
d68c6810 | 35 | enum rcu_ja_type_class { |
e5227865 | 36 | RCU_JA_LINEAR = 0, /* Type A */ |
fd800776 MD |
37 | /* 32-bit: 1 to 25 children, 8 to 128 bytes */ |
38 | /* 64-bit: 1 to 28 children, 16 to 256 bytes */ | |
39 | RCU_JA_POOL = 1, /* Type B */ | |
40 | /* 32-bit: 26 to 100 children, 256 to 512 bytes */ | |
41 | /* 64-bit: 29 to 112 children, 512 to 1024 bytes */ | |
e5227865 | 42 | RCU_JA_PIGEON = 2, /* Type C */ |
fd800776 MD |
43 | /* 32-bit: 101 to 256 children, 1024 bytes */ |
44 | /* 64-bit: 113 to 256 children, 2048 bytes */ | |
e5227865 | 45 | /* Leaf nodes are implicit from their height in the tree */ |
1db4943c | 46 | RCU_JA_NR_TYPES, |
e5227865 MD |
47 | }; |
48 | ||
d68c6810 MD |
49 | struct rcu_ja_type { |
50 | enum rcu_ja_type_class type_class; | |
8e519e3c MD |
51 | uint16_t min_child; /* minimum number of children: 1 to 256 */ |
52 | uint16_t max_child; /* maximum number of children: 1 to 256 */ | |
53 | uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */ | |
54 | uint16_t order; /* node size is (1 << order), in bytes */ | |
fd800776 MD |
55 | uint16_t nr_pool_order; /* number of pools */ |
56 | uint16_t pool_size_order; /* pool size */ | |
e5227865 MD |
57 | }; |
58 | ||
d68c6810 MD |
59 | /* |
60 | * Number of least significant pointer bits reserved to represent the | |
61 | * child type. | |
62 | */ | |
63 | #define JA_TYPE_BITS 3 | |
64 | #define JA_TYPE_MAX_NR (1U << JA_TYPE_BITS) | |
65 | #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1) | |
66 | #define JA_PTR_MASK (~JA_TYPE_MASK) | |
67 | ||
68 | #define JA_ENTRY_PER_NODE 256UL | |
69 | ||
e5227865 MD |
70 | /* |
71 | * Iteration on the array to find the right node size for the number of | |
d68c6810 | 72 | * children stops when it reaches .max_child == 256 (this is the largest |
e5227865 | 73 | * possible node size, which contains 256 children). |
d68c6810 MD |
74 | * The min_child overlaps with the previous max_child to provide an |
75 | * hysteresis loop to reallocation for patterns of cyclic add/removal | |
76 | * within the same node. | |
77 | * The node the index within the following arrays is represented on 3 | |
78 | * bits. It identifies the node type, min/max number of children, and | |
79 | * the size order. | |
3d45251f MD |
80 | * The max_child values for the RCU_JA_POOL below result from |
81 | * statistical approximation: over million populations, the max_child | |
82 | * covers between 97% and 99% of the populations generated. Therefore, a | |
83 | * fallback should exist to cover the rare extreme population unbalance | |
84 | * cases, but it will not have a major impact on speed nor space | |
85 | * consumption, since those are rare cases. | |
e5227865 | 86 | */ |
e5227865 | 87 | |
d68c6810 MD |
88 | #if (CAA_BITS_PER_LONG < 64) |
89 | /* 32-bit pointers */ | |
1db4943c MD |
90 | enum { |
91 | ja_type_0_max_child = 1, | |
92 | ja_type_1_max_child = 3, | |
93 | ja_type_2_max_child = 6, | |
94 | ja_type_3_max_child = 12, | |
95 | ja_type_4_max_child = 25, | |
96 | ja_type_5_max_child = 48, | |
97 | ja_type_6_max_child = 92, | |
98 | ja_type_7_max_child = 256, | |
99 | }; | |
100 | ||
8e519e3c MD |
101 | enum { |
102 | ja_type_0_max_linear_child = 1, | |
103 | ja_type_1_max_linear_child = 3, | |
104 | ja_type_2_max_linear_child = 6, | |
105 | ja_type_3_max_linear_child = 12, | |
106 | ja_type_4_max_linear_child = 25, | |
107 | ja_type_5_max_linear_child = 24, | |
108 | ja_type_6_max_linear_child = 23, | |
109 | }; | |
110 | ||
1db4943c MD |
111 | enum { |
112 | ja_type_5_nr_pool_order = 1, | |
113 | ja_type_6_nr_pool_order = 2, | |
114 | }; | |
115 | ||
d68c6810 | 116 | const struct rcu_ja_type ja_types[] = { |
8e519e3c MD |
117 | { .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, }, |
118 | { .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, }, | |
119 | { .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, }, | |
120 | { .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, }, | |
121 | { .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 | 122 | |
fd800776 | 123 | /* Pools may fill sooner than max_child */ |
8e519e3c MD |
124 | { .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, }, |
125 | { .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 |
126 | |
127 | /* | |
128 | * TODO: Upon node removal below min_child, if child pool is | |
129 | * filled beyond capacity, we need to roll back to pigeon. | |
130 | */ | |
1db4943c | 131 | { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, }, |
d68c6810 | 132 | }; |
d68c6810 MD |
133 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
134 | /* 64-bit pointers */ | |
1db4943c MD |
135 | enum { |
136 | ja_type_0_max_child = 1, | |
137 | ja_type_1_max_child = 3, | |
138 | ja_type_2_max_child = 7, | |
139 | ja_type_3_max_child = 14, | |
140 | ja_type_4_max_child = 28, | |
141 | ja_type_5_max_child = 54, | |
142 | ja_type_6_max_child = 104, | |
143 | ja_type_7_max_child = 256, | |
144 | }; | |
145 | ||
8e519e3c MD |
146 | enum { |
147 | ja_type_0_max_linear_child = 1, | |
148 | ja_type_1_max_linear_child = 3, | |
149 | ja_type_2_max_linear_child = 7, | |
150 | ja_type_3_max_linear_child = 14, | |
151 | ja_type_4_max_linear_child = 28, | |
152 | ja_type_5_max_linear_child = 27, | |
153 | ja_type_6_max_linear_child = 26, | |
154 | }; | |
155 | ||
1db4943c MD |
156 | enum { |
157 | ja_type_5_nr_pool_order = 1, | |
158 | ja_type_6_nr_pool_order = 2, | |
159 | }; | |
160 | ||
d68c6810 | 161 | const struct rcu_ja_type ja_types[] = { |
8e519e3c MD |
162 | { .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, }, |
163 | { .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, }, | |
164 | { .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, }, | |
165 | { .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, }, | |
166 | { .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 | 167 | |
3d45251f | 168 | /* Pools may fill sooner than max_child. */ |
8e519e3c MD |
169 | { .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, }, |
170 | { .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 | 171 | |
3d45251f MD |
172 | /* |
173 | * TODO: Upon node removal below min_child, if child pool is | |
174 | * filled beyond capacity, we need to roll back to pigeon. | |
175 | */ | |
1db4943c | 176 | { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, }, |
e5227865 | 177 | }; |
d68c6810 | 178 | #endif /* !(BITS_PER_LONG < 64) */ |
e5227865 | 179 | |
1db4943c MD |
180 | static inline __attribute__((unused)) |
181 | void static_array_size_check(void) | |
182 | { | |
183 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) > JA_TYPE_MAX_NR); | |
184 | } | |
185 | ||
e5227865 | 186 | /* |
1db4943c MD |
187 | * The rcu_ja_node contains the compressed node data needed for |
188 | * read-side. For linear and pool node configurations, it starts with a | |
189 | * byte counting the number of children in the node. Then, the | |
190 | * node-specific data is placed. | |
191 | * The node mutex, if any is needed, protecting concurrent updated of | |
192 | * each node is placed in a separate hash table indexed by node address. | |
193 | * For the pigeon configuration, the number of children is also kept in | |
194 | * a separate hash table, indexed by node address, because it is only | |
195 | * required for updates. | |
e5227865 | 196 | */ |
1db4943c | 197 | |
ff38c745 MD |
198 | #define DECLARE_LINEAR_NODE(index) \ |
199 | struct { \ | |
200 | uint8_t nr_child; \ | |
201 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
202 | struct rcu_ja_node_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ | |
203 | } | |
204 | ||
205 | #define DECLARE_POOL_NODE(index) \ | |
206 | struct { \ | |
207 | struct { \ | |
208 | uint8_t nr_child; \ | |
209 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
210 | struct rcu_ja_node_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ | |
211 | } linear[1U << ja_type_## index ##_nr_pool_order]; \ | |
212 | } | |
1db4943c | 213 | |
e5227865 | 214 | struct rcu_ja_node { |
1db4943c MD |
215 | union { |
216 | /* Linear configuration */ | |
217 | DECLARE_LINEAR_NODE(0) conf_0; | |
218 | DECLARE_LINEAR_NODE(1) conf_1; | |
219 | DECLARE_LINEAR_NODE(2) conf_2; | |
220 | DECLARE_LINEAR_NODE(3) conf_3; | |
221 | DECLARE_LINEAR_NODE(4) conf_4; | |
222 | ||
223 | /* Pool configuration */ | |
224 | DECLARE_POOL_NODE(5) conf_5; | |
225 | DECLARE_POOL_NODE(6) conf_6; | |
226 | ||
227 | /* Pigeon configuration */ | |
228 | struct { | |
229 | struct rcu_ja_node_flag *child[ja_type_7_max_child]; | |
230 | } conf_7; | |
231 | /* data aliasing nodes for computed accesses */ | |
232 | uint8_t data[sizeof(struct rcu_ja_node_flag *) * ja_type_7_max_child]; | |
233 | } u; | |
e5227865 MD |
234 | }; |
235 | ||
d68c6810 | 236 | static |
8e519e3c MD |
237 | struct rcu_ja_node_flag *ja_node_flag(struct rcu_ja_node *node, |
238 | unsigned int type) | |
d68c6810 | 239 | { |
1db4943c | 240 | assert(type < RCU_JA_NR_TYPES); |
d68c6810 MD |
241 | return (struct rcu_ja_node_flag *) (((unsigned long) node) | type); |
242 | } | |
243 | ||
244 | static | |
245 | unsigned int ja_node_type(struct rcu_ja_node_flag *node) | |
246 | { | |
247 | unsigned int type; | |
248 | ||
249 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); | |
1db4943c | 250 | assert(type < RCU_JA_NR_TYPES); |
d68c6810 MD |
251 | return type; |
252 | } | |
253 | ||
254 | static | |
255 | struct rcu_ja_node *ja_node_ptr(struct rcu_ja_node_flag *node) | |
256 | { | |
257 | return (struct rcu_ja_node *) (((unsigned long) node) | JA_PTR_MASK); | |
258 | } | |
259 | ||
7a0b2331 | 260 | struct rcu_ja_node *alloc_rcu_ja_node(const struct rcu_ja_type *ja_type) |
e5227865 | 261 | { |
1db4943c | 262 | return calloc(1U << ja_type->order, sizeof(char)); |
e5227865 MD |
263 | } |
264 | ||
265 | void free_rcu_ja_node(struct rcu_ja_node *node) | |
266 | { | |
267 | free(node); | |
268 | } | |
269 | ||
d68c6810 MD |
270 | #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask)) |
271 | #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1) | |
272 | #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask)) | |
273 | #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1) | |
274 | ||
275 | static | |
1db4943c | 276 | uint8_t *align_ptr_size(uint8_t *ptr) |
d68c6810 | 277 | { |
1db4943c | 278 | return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *)); |
d68c6810 MD |
279 | } |
280 | ||
13a7f5a6 MD |
281 | /* |
282 | * The order in which values and pointers are does does not matter: if | |
283 | * a value is missing, we return NULL. If a value is there, but its | |
284 | * associated pointers is still NULL, we return NULL too. | |
285 | */ | |
d68c6810 MD |
286 | static |
287 | struct rcu_ja_node_flag *ja_linear_node_get_nth(const struct rcu_ja_type *type, | |
8e519e3c MD |
288 | struct rcu_ja_node *node, |
289 | uint8_t n) | |
d68c6810 MD |
290 | { |
291 | uint8_t nr_child; | |
292 | uint8_t *values; | |
1db4943c | 293 | struct rcu_ja_node_flag **pointers; |
d68c6810 MD |
294 | struct rcu_ja_node_flag *ptr; |
295 | unsigned int i; | |
296 | ||
8e519e3c | 297 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); |
d68c6810 | 298 | |
13a7f5a6 MD |
299 | nr_child = CMM_LOAD_SHARED(node->u.data[0]); |
300 | cmm_smp_rmb(); /* read nr_child before values and pointers */ | |
8e519e3c MD |
301 | assert(nr_child <= type->max_linear_child); |
302 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
d68c6810 | 303 | |
1db4943c | 304 | values = &node->u.data[1]; |
d68c6810 | 305 | for (i = 0; i < nr_child; i++) { |
13a7f5a6 | 306 | if (CMM_LOAD_SHARED(values[i]) == n) |
d68c6810 MD |
307 | break; |
308 | } | |
309 | if (i >= nr_child) | |
310 | return NULL; | |
8e519e3c | 311 | pointers = (struct rcu_ja_node_flag **) align_ptr_size(&values[type->max_linear_child]); |
13a7f5a6 | 312 | ptr = rcu_dereference(pointers[i]); |
d68c6810 MD |
313 | assert(ja_node_ptr(ptr) != NULL); |
314 | return ptr; | |
315 | } | |
316 | ||
d68c6810 | 317 | static |
fd800776 | 318 | struct rcu_ja_node_flag *ja_pool_node_get_nth(const struct rcu_ja_type *type, |
8e519e3c MD |
319 | struct rcu_ja_node *node, |
320 | uint8_t n) | |
d68c6810 | 321 | { |
fd800776 | 322 | struct rcu_ja_node *linear; |
d68c6810 | 323 | |
fd800776 MD |
324 | assert(type->type_class == RCU_JA_POOL); |
325 | linear = (struct rcu_ja_node *) | |
1db4943c | 326 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
8e519e3c | 327 | return ja_linear_node_get_nth(type, linear, n); |
d68c6810 MD |
328 | } |
329 | ||
330 | static | |
331 | struct rcu_ja_node_flag *ja_pigeon_node_get_nth(const struct rcu_ja_type *type, | |
8e519e3c MD |
332 | struct rcu_ja_node *node, |
333 | uint8_t n) | |
d68c6810 MD |
334 | { |
335 | assert(type->type_class == RCU_JA_PIGEON); | |
13a7f5a6 | 336 | return rcu_dereference(((struct rcu_ja_node_flag **) node->u.data)[n]); |
d68c6810 MD |
337 | } |
338 | ||
13a7f5a6 MD |
339 | /* |
340 | * ja_node_get_nth: get nth item from a node. | |
341 | * node_flag is already rcu_dereference'd. | |
342 | */ | |
d68c6810 MD |
343 | static |
344 | struct rcu_ja_node_flag *ja_node_get_nth(struct rcu_ja_node_flag *node_flag, | |
8e519e3c | 345 | uint8_t n) |
d68c6810 MD |
346 | { |
347 | unsigned int type_index; | |
348 | struct rcu_ja_node *node; | |
349 | const struct rcu_ja_type *type; | |
350 | ||
d68c6810 MD |
351 | node = ja_node_ptr(node_flag); |
352 | assert(node != NULL); | |
353 | type_index = ja_node_type(node_flag); | |
354 | type = &ja_types[type_index]; | |
355 | ||
356 | switch (type->type_class) { | |
357 | case RCU_JA_LINEAR: | |
358 | return ja_linear_node_get_nth(type, node, n); | |
fd800776 MD |
359 | case RCU_JA_POOL: |
360 | return ja_pool_node_get_nth(type, node, n); | |
d68c6810 MD |
361 | case RCU_JA_PIGEON: |
362 | return ja_pigeon_node_get_nth(type, node, n); | |
363 | default: | |
364 | assert(0); | |
365 | return (void *) -1UL; | |
366 | } | |
367 | } | |
368 | ||
8e519e3c MD |
369 | static |
370 | int ja_linear_node_set_nth(const struct rcu_ja_type *type, | |
371 | struct rcu_ja_node *node, | |
372 | uint8_t n, | |
373 | struct rcu_ja_node_flag *child_node_flag) | |
374 | { | |
375 | uint8_t nr_child; | |
376 | uint8_t *values, *nr_child_ptr; | |
377 | struct rcu_ja_node_flag **pointers; | |
378 | unsigned int i; | |
379 | ||
380 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
381 | ||
382 | nr_child_ptr = &node->u.data[0]; | |
383 | nr_child = *nr_child_ptr; | |
384 | assert(nr_child <= type->max_linear_child); | |
385 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
386 | ||
387 | values = &node->u.data[1]; | |
388 | for (i = 0; i < nr_child; i++) { | |
389 | if (values[i] == n) | |
390 | return -EEXIST; | |
391 | } | |
392 | if (nr_child >= type->max_linear_child) { | |
393 | /* No space left in this node type */ | |
394 | return -ENOSPC; | |
395 | } | |
396 | pointers = (struct rcu_ja_node_flag **) align_ptr_size(&values[type->max_linear_child]); | |
13a7f5a6 MD |
397 | assert(pointers[nr_child] == NULL); |
398 | rcu_assign_pointer(pointers[nr_child], child_node_flag); | |
399 | CMM_STORE_SHARED(values[nr_child], n); | |
400 | cmm_smp_wmb(); /* write value and pointer before nr_child */ | |
401 | CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1); | |
8e519e3c MD |
402 | return 0; |
403 | } | |
404 | ||
405 | static | |
406 | int ja_pool_node_set_nth(const struct rcu_ja_type *type, | |
407 | struct rcu_ja_node *node, | |
408 | uint8_t n, | |
409 | struct rcu_ja_node_flag *child_node_flag) | |
410 | { | |
411 | struct rcu_ja_node *linear; | |
412 | ||
413 | assert(type->type_class == RCU_JA_POOL); | |
414 | linear = (struct rcu_ja_node *) | |
415 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; | |
416 | return ja_linear_node_set_nth(type, linear, n, child_node_flag); | |
417 | } | |
418 | ||
419 | static | |
420 | int ja_pigeon_node_set_nth(const struct rcu_ja_type *type, | |
421 | struct rcu_ja_node *node, | |
422 | uint8_t n, | |
423 | struct rcu_ja_node_flag *child_node_flag) | |
424 | { | |
425 | struct rcu_ja_node_flag **ptr; | |
426 | ||
427 | assert(type->type_class == RCU_JA_PIGEON); | |
428 | ptr = &((struct rcu_ja_node_flag **) node->u.data)[n]; | |
429 | if (*ptr != NULL) | |
430 | return -EEXIST; | |
431 | rcu_assign_pointer(*ptr, child_node_flag); | |
432 | return 0; | |
433 | } | |
434 | ||
d68c6810 | 435 | /* |
7a0b2331 | 436 | * _ja_node_set_nth: set nth item within a node. Return an error |
8e519e3c MD |
437 | * (negative error value) if it is already there. |
438 | * TODO: exclusive access on node. | |
d68c6810 | 439 | */ |
8e519e3c | 440 | static |
7a0b2331 | 441 | int _ja_node_set_nth(struct rcu_ja_node_flag *node_flag, uint8_t n, |
8e519e3c MD |
442 | struct rcu_ja_node_flag *child_node_flag) |
443 | { | |
444 | unsigned int type_index; | |
445 | struct rcu_ja_node *node; | |
446 | const struct rcu_ja_type *type; | |
e5227865 | 447 | |
8e519e3c MD |
448 | node = ja_node_ptr(node_flag); |
449 | assert(node != NULL); | |
450 | type_index = ja_node_type(node_flag); | |
451 | type = &ja_types[type_index]; | |
452 | ||
453 | switch (type->type_class) { | |
454 | case RCU_JA_LINEAR: | |
455 | return ja_linear_node_set_nth(type, node, n, | |
456 | child_node_flag); | |
457 | case RCU_JA_POOL: | |
458 | return ja_pool_node_set_nth(type, node, n, | |
459 | child_node_flag); | |
460 | case RCU_JA_PIGEON: | |
461 | return ja_pigeon_node_set_nth(type, node, n, | |
462 | child_node_flag); | |
463 | default: | |
464 | assert(0); | |
465 | return -EINVAL; | |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
7a0b2331 MD |
470 | |
471 | /* | |
472 | * ja_node_recompact_add: recompact a node, adding a new child. | |
473 | */ | |
474 | static | |
475 | int ja_node_recompact_add(struct rcu_ja_node_flag **old_node_flag, uint8_t n, | |
476 | struct rcu_ja_node_flag *child_node_flag) | |
477 | ||
478 | { | |
479 | unsigned int old_type_index, new_type_index; | |
480 | struct rcu_ja_node *old_node, *new_node; | |
481 | const struct rcu_ja_type *old_type, *new_type; | |
482 | struct rcu_ja_node_flag *new_node_flag; | |
483 | unsigned int i; | |
484 | int ret; | |
485 | ||
486 | old_node = ja_node_ptr(*old_node_flag); | |
487 | if (old_node == NULL) { | |
488 | new_type_index = 0; | |
489 | } else { | |
490 | old_type_index = ja_node_type(*old_node_flag); | |
491 | old_type = &ja_types[old_type_index]; | |
492 | new_type_index = old_type_index + 1; | |
493 | } | |
494 | new_type = &ja_types[new_type_index]; | |
495 | new_node = alloc_rcu_ja_node(new_type); | |
496 | if (!new_node) | |
497 | return -ENOMEM; | |
498 | new_node_flag = ja_node_flag(new_node, new_type_index); | |
499 | ||
500 | for (i = 0; i < old_type->max_child; i++) { | |
501 | struct rcu_ja_node_flag *iter; | |
502 | ||
503 | iter = ja_node_get_nth(*old_node_flag, i); | |
504 | if (!iter) | |
505 | continue; | |
506 | ret = _ja_node_set_nth(new_node_flag, i, iter); | |
507 | assert(!ret); | |
508 | } | |
509 | /* add node */ | |
510 | ret = _ja_node_set_nth(new_node_flag, n, child_node_flag); | |
511 | assert(!ret); | |
8eba1a4b MD |
512 | /* Replace the old node with the new recompacted one */ |
513 | rcu_assign_pointer(*old_node_flag, new_node_flag); | |
514 | /* TODO: free old_node (call_rcu) */ | |
7a0b2331 MD |
515 | return 0; |
516 | } | |
517 | ||
518 | static | |
519 | int ja_node_set_nth(struct rcu_ja_node_flag **node_flag, uint8_t n, | |
520 | struct rcu_ja_node_flag *child_node_flag) | |
521 | { | |
522 | int ret; | |
523 | ||
524 | ret = _ja_node_set_nth(*node_flag, n, child_node_flag); | |
525 | if (ret == -ENOSPC) { | |
526 | /* Not enough space in node, need to recompact */ | |
527 | ret = ja_node_recompact_add(node_flag, n, | |
528 | child_node_flag); | |
529 | if (ret < 0) | |
530 | return ret; | |
531 | } | |
532 | return ret; | |
533 | } |