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
25 #include <urcu/rcuja.h>
26 #include <urcu/compiler.h>
27 #include <urcu/arch.h>
29 #include "rcuja-internal.h"
32 enum rcu_ja_type_class
{
33 RCU_JA_LINEAR
= 0, /* Type A */
34 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
35 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
36 RCU_JA_POOL
= 1, /* Type B */
37 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
38 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
39 RCU_JA_PIGEON
= 2, /* Type C */
40 /* 32-bit: 101 to 256 children, 1024 bytes */
41 /* 64-bit: 113 to 256 children, 2048 bytes */
42 /* Leaf nodes are implicit from their height in the tree */
46 enum rcu_ja_type_class type_class
;
47 uint16_t min_child
; /* minimum number of children: 1 to 256 */
48 uint16_t max_child
; /* maximum number of children: 1 to 256 */
49 uint16_t order
; /* node size is (1 << order), in bytes */
50 uint16_t nr_pool_order
; /* number of pools */
51 uint16_t pool_size_order
; /* pool size */
55 * Number of least significant pointer bits reserved to represent the
58 #define JA_TYPE_BITS 3
59 #define JA_TYPE_MAX_NR (1U << JA_TYPE_BITS)
60 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
61 #define JA_PTR_MASK (~JA_TYPE_MASK)
63 #define JA_ENTRY_PER_NODE 256UL
66 * Iteration on the array to find the right node size for the number of
67 * children stops when it reaches .max_child == 256 (this is the largest
68 * possible node size, which contains 256 children).
69 * The min_child overlaps with the previous max_child to provide an
70 * hysteresis loop to reallocation for patterns of cyclic add/removal
71 * within the same node.
72 * The node the index within the following arrays is represented on 3
73 * bits. It identifies the node type, min/max number of children, and
77 #if (CAA_BITS_PER_LONG < 64)
79 const struct rcu_ja_type ja_types
[] = {
80 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 1, .order
= 3, },
81 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 3, .order
= 4, },
82 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= 6, .order
= 5, },
83 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= 12, .order
= 6, },
84 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= 25, .order
= 7, },
86 /* Pools may fill sooner than max_child */
87 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= 50, .order
= 8, .nr_pool_order
= 1, .pool_size_order
= 7, },
88 { .type_class
= RCU_JA_POOL
, .min_child
= 42, .max_child
= 100, .order
= 9, .nr_pool_order
= 2, .pool_size_order
= 7, },
90 /* TODO: Upon downsize, if at least one pool is filled, we need to keep pigeon */
91 { .type_class
= RCU_JA_PIGEON
, .min_child
= 90, .max_child
= 256, .order
= 10, },
93 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) > JA_TYPE_MAX_NR
);
94 #else /* !(CAA_BITS_PER_LONG < 64) */
96 const struct rcu_ja_type ja_types
[] = {
97 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 1, .order
= 4, },
98 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 3, .order
= 5, },
99 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= 7, .order
= 6, },
100 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= 14, .order
= 7, },
101 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= 28, .order
= 8, },
103 /* Pools may fill sooner than max_child */
104 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= 56, .order
= 9, .nr_pool_order
= 1, .pool_size_order
= 8, },
105 { .type_class
= RCU_JA_POOL
, .min_child
= 44, .max_child
= 112, .order
= 10, .nr_pool_order
= 2, .pool_size_order
= 8, },
107 /* TODO: Upon downsize, if at least one pool is filled, we need to keep pigeon */
108 { .type_class
= RCU_JA_PIGEON
, .min_child
= 100, .max_child
= 256, .order
= 11, },
110 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) > JA_TYPE_MAX_NR
);
111 #endif /* !(BITS_PER_LONG < 64) */
114 * The rcu_ja_node starts with a byte counting the number of children in
115 * the node. Then, the node-specific data is placed.
116 * TODO: where should we put the mutex for the node ?
117 * -> mutex could be a 0-value node count.
118 * TODO: where do we keep nr children for pigeon ?
124 /* Never declared. Opaque type used to store flagged node pointers. */
125 struct rcu_ja_node_flag
;
128 struct rcu_ja_node_flag
*ja_node_flag(struct rcu_ja_node
*node
, unsigned int type
)
130 assert(type
< JA_TYPE_NR
);
131 return (struct rcu_ja_node_flag
*) (((unsigned long) node
) | type
);
135 unsigned int ja_node_type(struct rcu_ja_node_flag
*node
)
139 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
140 assert(type
< JA_TYPE_NR
);
145 struct rcu_ja_node
*ja_node_ptr(struct rcu_ja_node_flag
*node
)
147 return (struct rcu_ja_node
*) (((unsigned long) node
) | JA_PTR_MASK
);
150 struct rcu_ja_node
*alloc_rcu_ja_node(struct rcu_ja_type
*ja_type
)
152 return zmalloc(1 << ja_type
->node_order
);
155 void free_rcu_ja_node(struct rcu_ja_node
*node
)
160 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
161 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
162 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
163 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
166 char *align_ptr_size(char *ptr
)
168 return JA_ALIGN(ptr
, sizeof(ptr
));
172 struct rcu_ja_node_flag
*ja_linear_node_get_nth(const struct rcu_ja_type
*type
,
173 struct rcu_ja_node
*node
,
178 struct rcu_ja_node_flag
*pointers
;
179 struct rcu_ja_node_flag
*ptr
;
182 assert(!type
|| type
->type_class
== RCU_JA_LINEAR
);
184 nr_child
= node
->data
[0];
185 cmm_smp_rmb(); /* read nr_child before values */
186 assert(!type
|| nr_child
<= type
->max_child
);
187 assert(!type
|| nr_child
>= type
->min_child
);
190 for (i
= 0; i
< nr_child
; i
++) {
196 cmm_smp_rmb(); /* read values before pointer */
197 pointers
= align_ptr_size(&values
[nr_child
]);
199 assert(ja_node_ptr(ptr
) != NULL
);
204 struct rcu_ja_node_flag
*ja_pool_node_get_nth(const struct rcu_ja_type
*type
,
205 struct rcu_ja_node
*node
,
208 struct rcu_ja_node_flag
*ptr
;
209 struct rcu_ja_node
*linear
;
211 assert(type
->type_class
== RCU_JA_POOL
);
212 linear
= (struct rcu_ja_node
*)
213 &node
->data
[(n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
214 return ja_linear_node_get_nth(NULL
, linear
, n
);
218 struct rcu_ja_node_flag
*ja_pigeon_node_get_nth(const struct rcu_ja_type
*type
,
219 struct rcu_ja_node
*node
,
222 assert(type
->type_class
== RCU_JA_PIGEON
);
223 return ((struct rcu_ja_node_flag
*) node
->data
)[n
];
226 /* ja_node_get_nth: get nth item from a node */
228 struct rcu_ja_node_flag
*ja_node_get_nth(struct rcu_ja_node_flag
*node_flag
,
231 unsigned int type_index
;
232 struct rcu_ja_node
*node
;
233 const struct rcu_ja_type
*type
;
235 node_flag
= rcu_dereference(node_flag
);
236 node
= ja_node_ptr(node_flag
);
237 assert(node
!= NULL
);
238 type_index
= ja_node_type(node_flag
);
239 type
= &ja_types
[type_index
];
241 switch (type
->type_class
) {
243 return ja_linear_node_get_nth(type
, node
, n
);
245 return ja_pool_node_get_nth(type
, node
, n
);
247 return ja_pigeon_node_get_nth(type
, node
, n
);
250 return (void *) -1UL;
255 * ja_node_set_nth: set nth item within a node. asserts that it is not