+ * TODO: use ja_get_nr_child to monitor limits triggering shrink
+ * recompaction.
+ * Also use ja_get_nr_child to make the difference between resize and
+ * pool change of compaction bit(s).
+ */
+static
+unsigned int ja_get_nr_child(struct rcu_ja_shadow_node *shadow_node)
+{
+ return shadow_node->nr_child;
+}
+
+static
+int ja_linear_node_set_nth(const struct rcu_ja_type *type,
+ struct rcu_ja_node *node,
+ struct rcu_ja_shadow_node *shadow_node,
+ uint8_t n,
+ struct rcu_ja_node_flag *child_node_flag)
+{
+ uint8_t nr_child;
+ uint8_t *values, *nr_child_ptr;
+ struct rcu_ja_node_flag **pointers;
+ unsigned int i;
+
+ assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
+
+ nr_child_ptr = &node->u.data[0];
+ nr_child = *nr_child_ptr;
+ assert(nr_child <= type->max_linear_child);
+ assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
+
+ values = &node->u.data[1];
+ for (i = 0; i < nr_child; i++) {
+ if (values[i] == n)
+ return -EEXIST;
+ }
+ if (nr_child >= type->max_linear_child) {
+ /* No space left in this node type */
+ return -ENOSPC;
+ }
+ pointers = (struct rcu_ja_node_flag **) align_ptr_size(&values[type->max_linear_child]);
+ assert(pointers[nr_child] == NULL);
+ rcu_assign_pointer(pointers[nr_child], child_node_flag);
+ CMM_STORE_SHARED(values[nr_child], n);
+ cmm_smp_wmb(); /* write value and pointer before nr_child */
+ CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
+ shadow_node->nr_child++;
+ return 0;
+}
+
+static
+int ja_pool_node_set_nth(const struct rcu_ja_type *type,
+ struct rcu_ja_node *node,
+ struct rcu_ja_shadow_node *shadow_node,
+ uint8_t n,
+ struct rcu_ja_node_flag *child_node_flag)
+{
+ struct rcu_ja_node *linear;
+
+ assert(type->type_class == RCU_JA_POOL);
+ linear = (struct rcu_ja_node *)
+ &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
+ return ja_linear_node_set_nth(type, linear, shadow_node,
+ n, child_node_flag);
+}
+
+static
+int ja_pigeon_node_set_nth(const struct rcu_ja_type *type,
+ struct rcu_ja_node *node,
+ struct rcu_ja_shadow_node *shadow_node,
+ uint8_t n,
+ struct rcu_ja_node_flag *child_node_flag)
+{
+ struct rcu_ja_node_flag **ptr;
+
+ assert(type->type_class == RCU_JA_PIGEON);
+ ptr = &((struct rcu_ja_node_flag **) node->u.data)[n];
+ if (*ptr != NULL)
+ return -EEXIST;
+ rcu_assign_pointer(*ptr, child_node_flag);
+ shadow_node->nr_child++;
+ return 0;
+}
+
+/*
+ * _ja_node_set_nth: set nth item within a node. Return an error
+ * (negative error value) if it is already there.
+ * TODO: exclusive access on node.
+ */
+static
+int _ja_node_set_nth(const struct rcu_ja_type *type,
+ struct rcu_ja_node *node,
+ struct rcu_ja_shadow_node *shadow_node,
+ uint8_t n,
+ struct rcu_ja_node_flag *child_node_flag)
+{
+ switch (type->type_class) {
+ case RCU_JA_LINEAR:
+ return ja_linear_node_set_nth(type, node, shadow_node, n,
+ child_node_flag);
+ case RCU_JA_POOL:
+ return ja_pool_node_set_nth(type, node, shadow_node, n,
+ child_node_flag);
+ case RCU_JA_PIGEON:
+ return ja_pigeon_node_set_nth(type, node, shadow_node, n,
+ child_node_flag);
+ case RCU_JA_NULL:
+ return -ENOSPC;
+ default:
+ assert(0);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/*
+ * ja_node_recompact_add: recompact a node, adding a new child.
+ * TODO: for pool type, take selection bit(s) into account.