summaryrefslogtreecommitdiff
path: root/lib/sradix-tree.c
blob: 8d0632917e8b09c53f355c1de7f6f9df02f7c384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/gcd.h>
#include <linux/sradix-tree.h>

static inline int sradix_node_full(struct sradix_tree_root *root, struct sradix_tree_node *node)
{
	return node->fulls == root->stores_size || 
		(node->height == 1 && node->count == root->stores_size);
}

/*
 *	Extend a sradix tree so it can store key @index.
 */
static int sradix_tree_extend(struct sradix_tree_root *root, unsigned long index)
{
	struct sradix_tree_node *node;
	unsigned int height;

	if (unlikely(root->rnode == NULL)) {
		if (!(node = root->alloc()))
			return -ENOMEM;

		node->height = 1;
		root->rnode = node;
		root->height = 1;
	}

	/* Figure out what the height should be.  */
	height = root->height;
	index >>= root->shift * height;

	while (index) {
		index >>= root->shift;
		height++;
	}

	while (height > root->height) {
		unsigned int newheight;
		if (!(node = root->alloc()))
			return -ENOMEM;

		/* Increase the height.  */
		node->stores[0] = root->rnode;
		root->rnode->parent = node;
		if (root->extend)
			root->extend(node, root->rnode);

		newheight = root->height + 1;
		node->height = newheight;
		node->count = 1;
		if (sradix_node_full(root, root->rnode))
			node->fulls = 1;

		root->rnode = node;
		root->height = newheight;
	}

	return 0;
}

/*
 * Search the next item from the current node, that is not NULL
 * and can satify root->iter().
 */
void *sradix_tree_next(struct sradix_tree_root *root,
		       struct sradix_tree_node *node, unsigned long index,
		       int (*iter)(void *item, unsigned long height))
{
	unsigned long offset;
	void *item;

	if (unlikely(node == NULL)) {
		node = root->rnode;
		for (offset = 0; offset < root->stores_size; offset++) {
			item = node->stores[offset];
			if (item && (!iter || iter(item, node->height)))
				break;
		}

		if (unlikely(offset >= root->stores_size))
			return NULL;

		if (node->height == 1)
			return item;
		else
			goto go_down;
	}

	while (node) {
		offset = (index & root->mask) + 1;					
		for (;offset < root->stores_size; offset++) {
			item = node->stores[offset];
			if (item && (!iter || iter(item, node->height)))
				break;
		}

		if (offset < root->stores_size)
			break;

		node = node->parent;
		index >>= root->shift;
	}

	if (!node)
		return NULL;

	while (node->height > 1) {
go_down:
		node = item;
		for (offset = 0; offset < root->stores_size; offset++) {
			item = node->stores[offset];
			if (item && (!iter || iter(item, node->height)))
				break;
		}

		if (unlikely(offset >= root->stores_size))
			return NULL;
	}

	BUG_ON(offset > root->stores_size);

	return item;
}

/*
 * Blindly insert the item to the tree. Typically, we reuse the
 * first empty store item.
 */
int sradix_tree_enter(struct sradix_tree_root *root, void **item, int num)
{
	unsigned long index;
	unsigned int height;
	struct sradix_tree_node *node, *tmp = NULL;
	int offset, offset_saved;
	void **store = NULL;
	int error, i, j, shift;

go_on:
	index = root->min;

	if (root->enter_node && !sradix_node_full(root, root->enter_node)) {
		node = root->enter_node;
		BUG_ON((index >> (root->shift * root->height)));
	} else {
		node = root->rnode;
		if (node == NULL || (index >> (root->shift * root->height))
		    || sradix_node_full(root, node)) {
			error = sradix_tree_extend(root, index);
			if (error)
				return error;

			node = root->rnode;
		}
	}


	height = node->height;
	shift = (height - 1) * root->shift;
	offset = (index >> shift) & root->mask;
	while (shift > 0) {
		offset_saved = offset;
		for (; offset < root->stores_size; offset++) {
			store = &node->stores[offset];
			tmp = *store;

			if (!tmp || !sradix_node_full(root, tmp))
				break;
		}
		BUG_ON(offset >= root->stores_size);

		if (offset != offset_saved) {
			index += (offset - offset_saved) << shift;
			index &= ~((1UL << shift) - 1);
		}

		if (!tmp) {
			if (!(tmp = root->alloc()))
				return -ENOMEM;

			tmp->height = shift / root->shift;
			*store = tmp;
			tmp->parent = node;
			node->count++;
//			if (root->extend)
//				root->extend(node, tmp);
		}

		node = tmp;
		shift -= root->shift;
		offset = (index >> shift) & root->mask;
	}

	BUG_ON(node->height != 1);


	store = &node->stores[offset];
	for (i = 0, j = 0;
	      j < root->stores_size - node->count && 
	      i < root->stores_size - offset && j < num; i++) {
		if (!store[i]) {
			store[i] = item[j];
			if (root->assign)
				root->assign(node, index + i, item[j]);
			j++;
		}
	}

	node->count += j;
	root->num += j;
	num -= j;

	while (sradix_node_full(root, node)) {
		node = node->parent;
		if (!node)
			break;

		node->fulls++;
	}

	if (unlikely(!node)) {
		/* All nodes are full */
		root->min = 1 << (root->height * root->shift);
		root->enter_node = NULL;
	} else {
		root->min = index + i - 1;
		root->min |= (1UL << (node->height - 1)) - 1;
		root->min++;
		root->enter_node = node;
	}

	if (num) {
		item += j;
		goto go_on;
	}

	return 0;
}


/**
 *	sradix_tree_shrink    -    shrink height of a sradix tree to minimal
 *      @root		sradix tree root
 *  
 */
static inline void sradix_tree_shrink(struct sradix_tree_root *root)
{
	/* try to shrink tree height */
	while (root->height > 1) {
		struct sradix_tree_node *to_free = root->rnode;

		/*
		 * The candidate node has more than one child, or its child
		 * is not at the leftmost store, we cannot shrink.
		 */
		if (to_free->count != 1 || !to_free->stores[0])
			break;

		root->rnode = to_free->stores[0];
		root->rnode->parent = NULL;
		root->height--;
		if (unlikely(root->enter_node == to_free)) {
			root->enter_node = NULL;
		}
		root->free(to_free);
	}
}

/*
 * Del the item on the known leaf node and index
 */
void sradix_tree_delete_from_leaf(struct sradix_tree_root *root, 
				  struct sradix_tree_node *node, unsigned long index)
{
	unsigned int offset;
	struct sradix_tree_node *start, *end;

	BUG_ON(node->height != 1);

	start = node;
	while (node && !(--node->count))
		node = node->parent;

	end = node;
	if (!node) {
		root->rnode = NULL;
		root->height = 0;
		root->min = 0;
		root->num = 0;
		root->enter_node = NULL;
	} else {
		offset = (index >> (root->shift * (node->height - 1))) & root->mask;
		if (root->rm)
			root->rm(node, offset);
		node->stores[offset] = NULL;
		root->num--;
		if (root->min > index) {
			root->min = index;
			root->enter_node = node;
		}
	}

	if (start != end) {
		do {
			node = start;
			start = start->parent;
			if (unlikely(root->enter_node == node))
				root->enter_node = end;
			root->free(node);
		} while (start != end);

		/*
		 * Note that shrink may free "end", so enter_node still need to
		 * be checked inside.
		 */
		sradix_tree_shrink(root);
	} else if (node->count == root->stores_size - 1) {
		/* It WAS a full leaf node. Update the ancestors */
		node = node->parent;
		while (node) {
			node->fulls--;
			if (node->fulls != root->stores_size - 1)
				break;

			node = node->parent;
		}
	}
}

void *sradix_tree_lookup(struct sradix_tree_root *root, unsigned long index)
{
	unsigned int height, offset;
	struct sradix_tree_node *node;
	int shift;

	node = root->rnode;
	if (node == NULL || (index >> (root->shift * root->height)))
		return NULL;

	height = root->height;
	shift = (height - 1) * root->shift;

	do {
		offset = (index >> shift) & root->mask;
		node = node->stores[offset];
		if (!node)
			return NULL;

		shift -= root->shift;
	} while (shift >= 0);

	return node;
}

/*
 * Return the item if it exists, otherwise create it in place
 * and return the created item.
 */
void *sradix_tree_lookup_create(struct sradix_tree_root *root, 
			unsigned long index, void *(*item_alloc)(void))
{
	unsigned int height, offset;
	struct sradix_tree_node *node, *tmp;
	void *item;
	int shift, error;

	if (root->rnode == NULL || (index >> (root->shift * root->height))) {
		if (item_alloc) {
			error = sradix_tree_extend(root, index);
			if (error)
				return NULL;
		} else {
			return NULL;
		}
	}

	node = root->rnode;
	height = root->height;
	shift = (height - 1) * root->shift;

	do {
		offset = (index >> shift) & root->mask;
		if (!node->stores[offset]) {
			if (!(tmp = root->alloc()))
				return NULL;

			tmp->height = shift / root->shift;
			node->stores[offset] = tmp;
			tmp->parent = node;
			node->count++;
			node = tmp;
		} else {
			node = node->stores[offset];
		}

		shift -= root->shift;
	} while (shift > 0);

	BUG_ON(node->height != 1);
	offset = index & root->mask;
	if (node->stores[offset]) {
		return node->stores[offset];
	} else if (item_alloc) {
		if (!(item = item_alloc()))
			return NULL;

		node->stores[offset] = item;

		/*
		 * NOTE: we do NOT call root->assign here, since this item is
		 * newly created by us having no meaning. Caller can call this
		 * if it's necessary to do so.
		 */

		node->count++;
		root->num++;

		while (sradix_node_full(root, node)) {
			node = node->parent;
			if (!node)
				break;

			node->fulls++;
		}

		if (unlikely(!node)) {
			/* All nodes are full */
			root->min = 1 << (root->height * root->shift);
		} else {
			if (root->min == index) {
				root->min |= (1UL << (node->height - 1)) - 1;
				root->min++;
				root->enter_node = node;
			}
		}

		return item;
	} else {
		return NULL;
	}

}

int sradix_tree_delete(struct sradix_tree_root *root, unsigned long index)
{
	unsigned int height, offset;
	struct sradix_tree_node *node;
	int shift;

	node = root->rnode;
	if (node == NULL || (index >> (root->shift * root->height)))
		return -ENOENT;

	height = root->height;
	shift = (height - 1) * root->shift;

	do {
		offset = (index >> shift) & root->mask;
		node = node->stores[offset];
		if (!node)
			return -ENOENT;

		shift -= root->shift;
	} while (shift > 0);

	offset = index & root->mask;
	if (!node->stores[offset])
		return -ENOENT;

	sradix_tree_delete_from_leaf(root, node, index);

	return 0;
}