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