vm_radix.h revision 226952
1226645Sattilio/*
2226645Sattilio * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
3226645Sattilio * All rights reserved.
4226645Sattilio *
5226645Sattilio * Redistribution and use in source and binary forms, with or without
6226645Sattilio * modification, are permitted provided that the following conditions
7226645Sattilio * are met:
8226645Sattilio * 1. Redistributions of source code must retain the above copyright
9226645Sattilio *    notice, this list of conditions and the following disclaimer.
10226645Sattilio * 2. Redistributions in binary form must reproduce the above copyright
11226645Sattilio *    notice, this list of conditions and the following disclaimer in the
12226645Sattilio *    documentation and/or other materials provided with the distribution.
13226645Sattilio *
14226645Sattilio * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15226645Sattilio * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16226645Sattilio * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17226645Sattilio * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18226645Sattilio * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19226645Sattilio * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20226645Sattilio * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21226645Sattilio * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22226645Sattilio * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23226645Sattilio * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24226645Sattilio * SUCH DAMAGE.
25226645Sattilio *
26226645Sattilio */
27226645Sattilio
28226645Sattilio#ifndef _VM_RADIX_H_
29226645Sattilio#define _VM_RADIX_H_
30226645Sattilio
31226645Sattilio#include <sys/queue.h>
32226645Sattilio
33226645Sattilio/* Default values of the tree parameters */
34226645Sattilio#define	VM_RADIX_WIDTH	5
35226645Sattilio#define	VM_RADIX_COUNT	(1 << VM_RADIX_WIDTH)
36226645Sattilio#define	VM_RADIX_MASK	(VM_RADIX_COUNT - 1)
37226645Sattilio#define	VM_RADIX_LIMIT	howmany((sizeof(vm_pindex_t) * NBBY), VM_RADIX_WIDTH)
38226930Sjeff#define	VM_RADIX_FLAGS	0x3	/* Flag bits stored in node pointers. */
39226930Sjeff#define	VM_RADIX_BLACK	0x1	/* Black node. (leaf only) */
40226930Sjeff#define	VM_RADIX_RED	0x2	/* Red node. (leaf only) */
41226930Sjeff#define	VM_RADIX_ANY	(VM_RADIX_RED | VM_RADIX_BLACK)
42226930Sjeff#define	VM_RADIX_EMPTY	0x1	/* Empty hint. (internal only) */
43226930Sjeff#define	VM_RADIX_HEIGHT	0xf	/* Bits of height in root */
44226930Sjeff#define	VM_RADIX_STACK	8	/* Nodes to store on stack. */
45226645Sattilio
46226876SjeffCTASSERT(VM_RADIX_HEIGHT >= VM_RADIX_LIMIT);
47226876Sjeff
48226645Sattilio/* Calculates maximum value for a tree of height h. */
49226645Sattilio#define	VM_RADIX_MAX(h)							\
50226645Sattilio	    ((h) == VM_RADIX_LIMIT ? ((vm_pindex_t)-1) :		\
51226645Sattilio	    (((vm_pindex_t)1 << ((h) * VM_RADIX_WIDTH)) - 1))
52226645Sattilio
53226645Sattiliostruct vm_radix_node {
54226930Sjeff	void		*rn_child[VM_RADIX_COUNT];	/* child nodes. */
55226645Sattilio    	uint16_t	rn_count;			/* Valid children. */
56226645Sattilio};
57226645Sattilio
58226876Sjeff/*
59226876Sjeff * Radix tree root.  The height and pointer are set together to permit
60226876Sjeff * coherent lookups while the root is modified.
61226876Sjeff */
62226645Sattiliostruct vm_radix {
63226876Sjeff	uintptr_t	rt_root;		/* root + height */
64226645Sattilio};
65226645Sattilio
66226873Sattiliovoid	vm_radix_init(void);
67226930Sjeff
68226930Sjeff/*
69226930Sjeff * Functions which only work with black nodes. (object lock)
70226930Sjeff */
71226645Sattilioint 	vm_radix_insert(struct vm_radix *, vm_pindex_t, void *);
72226645Sattiliovoid 	vm_radix_shrink(struct vm_radix *);
73226645Sattilio
74226646Sjeff/*
75226930Sjeff * Functions which work on specified colors. (object, vm_page_queue_free locks)
76226930Sjeff */
77226952Sjeffvoid	*vm_radix_color(struct vm_radix *, vm_pindex_t, int);
78226952Sjeffvoid	*vm_radix_lookup(struct vm_radix *, vm_pindex_t, int);
79226952Sjeffint	vm_radix_lookupn(struct vm_radix *, vm_pindex_t, vm_pindex_t, int,
80226952Sjeff	    void **, int, vm_pindex_t *);
81226952Sjeffvoid	*vm_radix_lookup_le(struct vm_radix *, vm_pindex_t, int);
82226952Sjeffvoid	*vm_radix_remove(struct vm_radix *, vm_pindex_t, int);
83226952Sjeffvoid	vm_radix_foreach(struct vm_radix *, vm_pindex_t, vm_pindex_t, int,
84226952Sjeff	    void (*)(void *));
85226930Sjeff
86226930Sjeff/*
87226646Sjeff * Look up any entry at a position greater or equal to index.
88226646Sjeff */
89226646Sjeffstatic inline void *
90226930Sjeffvm_radix_lookup_ge(struct vm_radix *rtree, vm_pindex_t index, int color)
91226646Sjeff{
92226646Sjeff        void *val;
93226646Sjeff
94226930Sjeff        if (vm_radix_lookupn(rtree, index, 0, color, &val, 1, &index))
95226646Sjeff                return (val);
96226646Sjeff        return (NULL);
97226646Sjeff}
98226646Sjeff
99226645Sattilio#endif /* !_VM_RADIX_H_ */
100